• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

通过self来增删改查文本文档的内容

武飞扬头像
cosmosForMe
帮助1

见2021 week12 task1

建立字典来承载文本文档的内容,此段代码要放在dictionary.py文件里。

通过从hash_table文件引入已经编译好的LinearProbeHashTable,同时也要从python内置的typing库里引入Tuple,还要再引用timeit来计时。

  1.  
    from hash_table import LinearProbeHashTable
  2.  
    from typing import Tuple
  3.  
    import timeit
  4.  
     
  5.  
     
  6.  
    class Dictionary:
  7.  
    DEFAULT_ENCODING = 'utf-8'
  8.  
     
  9.  
    def __init__(self, hash_base: int, table_size: int) -> None:
  10.  
    self.hash_table = LinearProbeHashTable(hash_base, table_size)
  11.  
     
  12.  
    def load_dictionary(self, filename: str, time_limit: int = None) -> int:
  13.  
    # self.table = LinearProbeHashTable(self.hash_base, self.table_size)
  14.  
    start_time = timeit.default_timer()
  15.  
     
  16.  
    words = 0
  17.  
    with open(filename, 'r', encoding=Dictionary.DEFAULT_ENCODING) as file:
  18.  
    line = file.readline()
  19.  
    while line:
  20.  
    line = line.strip()
  21.  
    self.hash_table[line] = 1
  22.  
    if time_limit is not None and timeit.default_timer() - start_time > time_limit:
  23.  
    raise TimeoutError("Exceeded time limit: " str(time_limit))
  24.  
    words = 1
  25.  
    line = file.readline()
  26.  
     
  27.  
    return words
  28.  
     
  29.  
    def add_word(self, word: str) -> None:
  30.  
    self.hash_table[word.lower()] = 1
  31.  
     
  32.  
    def find_word(self, word: str) -> bool:
  33.  
    return word.lower() in self.hash_table
  34.  
     
  35.  
    def delete_word(self, word: str) -> None:
  36.  
    del self.hash_table[word.lower()]
  37.  
     
  38.  
     
  39.  
    def process_option(dictionary: Dictionary, method_name: str) -> None:
  40.  
    """ Helper code for processing menu options."""
  41.  
    if method_name == 'read_file':
  42.  
    filename = input('Enter filename: ')
  43.  
    try:
  44.  
    dictionary.load_dictionary(filename)
  45.  
    print('Successfully read file')
  46.  
    except FileNotFoundError as e:
  47.  
    print(e)
  48.  
    else:
  49.  
    word = input('Enter word: ')
  50.  
    if method_name == 'add_word':
  51.  
    dictionary.add_word(word)
  52.  
    try:
  53.  
    dictionary.add_word(word)
  54.  
    print('[{}] {}'.format(word, 'Successfully added'))
  55.  
    except IndexError as e:
  56.  
    print('[{}] {}'.format(word, e))
  57.  
    elif method_name == 'find_word':
  58.  
    if dictionary.find_word(word):
  59.  
    print('[{}] {}'.format(word, 'Found in dictionary'))
  60.  
    else:
  61.  
    print('[{}] {}'.format(word, 'Not found in dictionary'))
  62.  
    elif method_name == 'delete_word':
  63.  
    try:
  64.  
    dictionary.delete_word(word)
  65.  
    print('[{}] {}'.format(word, 'Deleted from dictionary'))
  66.  
    except KeyError:
  67.  
    print('[{}] {}'.format(word, 'Not found in dictionary'))
  68.  
     
  69.  
     
  70.  
    def menu(dictionary: Dictionary):
  71.  
    """ Wrapper for using the dictionary. """
  72.  
    option = None
  73.  
    menu_options = {'read_file': 'Read File',
  74.  
    'add_word': 'Add Word',
  75.  
    'find_word': 'Find Word',
  76.  
    'delete_word': 'Delete Word',
  77.  
    'exit': 'Exit'}
  78.  
     
  79.  
    exit_option = list(menu_options.keys()).index('exit') 1
  80.  
     
  81.  
    while option != exit_option:
  82.  
    print('---------------------')
  83.  
    opt = 1
  84.  
    for menu_option in menu_options.values():
  85.  
    print('{}. {}'.format(opt, menu_option))
  86.  
    opt = 1
  87.  
    print('---------------------')
  88.  
    try:
  89.  
    option = int(input("Enter option: "))
  90.  
    if option < 1 or option > exit_option:
  91.  
    raise ValueError('Option must be between 1 and ' str(exit_option))
  92.  
    except ValueError as e:
  93.  
    print('[{}] {}'.format('menu', e))
  94.  
    else:
  95.  
    if option != exit_option:
  96.  
    process_option(dictionary, list(menu_options.keys())[option - 1])
  97.  
    print("---------------------")
  98.  
     
  99.  
     
  100.  
    if __name__ == '__main__':
  101.  
    dictionary = Dictionary(31, 250727)
  102.  
    menu(dictionary)

将此段代码放在frequency.py文件里:

  1.  
    from enum import Enum
  2.  
    from string import punctuation
  3.  
    from dictionary import Dictionary
  4.  
    from hash_table import LinearProbeHashTable
  5.  
     
  6.  
     
  7.  
    class Rarity(Enum):
  8.  
    COMMON = 0
  9.  
    UNCOMMON = 1
  10.  
    RARE = 2
  11.  
    MISSPELT = 3
  12.  
     
  13.  
     
  14.  
    class Frequency:
  15.  
    # TODO
  16.  
    # raise NotImplementedError
  17.  
    def __init__(self) -> None:
  18.  
    self.hash_base = 27183
  19.  
    self.table_size = 250727
  20.  
    self.hash_table = LinearProbeHashTable(self.hash_base, self.table_size)
  21.  
    self.dictionary = Dictionary(self.hash_base, self.table_size)
  22.  
    self.dictionary.load_dictionary('english_large.txt', 10)
  23.  
    self.max_word = ('', 0)
  24.  
     
  25.  
    # O(N)
  26.  
    def add_file(self, filename: str) -> None:
  27.  
    with open(filename, mode = 'r', encoding = 'utf-8') as f:
  28.  
    content = f.read().split() # split words in text
  29.  
    for word in content:
  30.  
    word = word.strip(punctuation).lower()
  31.  
    if self.dictionary.find_word(word):
  32.  
    if word in self.hash_table:
  33.  
    t = self.hash_table[word]
  34.  
    self.hash_table[word] = t 1
  35.  
    if self.max_word[1] < t 1:
  36.  
    self.max_word = (word, t 1)
  37.  
    else:
  38.  
    self.hash_table.insert(word, 1)
  39.  
     
  40.  
    # O(1)
  41.  
    def rarity(self, word: str) -> Rarity:
  42.  
    cnt = self.hash_table[word]
  43.  
    if cnt >= max(self.max_word[1] / 100, 1):
  44.  
    return Rarity.COMMON
  45.  
    elif cnt >= max(self.max_word[1] / 1000, 1):
  46.  
    return Rarity.UNCOMMON
  47.  
    elif cnt != 0:
  48.  
    return Rarity.RARE
  49.  
    else:
  50.  
    return Rarity.MISSPELT
  51.  
     
  52.  
     
  53.  
    def frequency_analysis() -> None:
  54.  
    # TODO
  55.  
    pass
  56.  
     
  57.  
     
  58.  
    if __name__ == '__main__':
  59.  
    frequency_analysis()

有几个点需要注意,在#TODO部份是根据要求新添加的功能。例如,raise NotImplementedError

        self.hash_base = 27183

        self.table_size = 250727

定义好hash_base和table_size的大小。

定义__init__后,执行实例化的过程须变成Frequency(arg1),新建的实例本身,连带其中的参数,会一并传给__init__函数自动并执行它。所以__init__函数的参数列表会在开头多出一项,它永远指代新建的那个实例对象,Python语法要求这个参数必须要有,而名称随意,习惯上就命为self

接下来要使用已经定义好的hash_table.py, list_adt, referential_array.py。

test_frequency.py文件来测试字典对于文本文档内容的承载以及修改是否成功,通过定义不同的参数来测试方法定义是否成功。

  1.  
    """Unit Testing for Task 1 and 2"""
  2.  
     
  3.  
    import unittest
  4.  
    import sys
  5.  
    from hash_table import LinearProbeHashTable
  6.  
    from frequency import Frequency, Rarity
  7.  
     
  8.  
     
  9.  
    class TestFrequency(unittest.TestCase):
  10.  
    def setUp(self) -> None:
  11.  
    self.frequency = Frequency()
  12.  
     
  13.  
    def test_init(self) -> None:
  14.  
    self.assertEqual(type(self.frequency.hash_table), LinearProbeHashTable)
  15.  
    self.assertEqual(self.frequency.dictionary.find_word('test'), 1)
  16.  
     
  17.  
    def test_add_file(self) -> None:
  18.  
    # TODO: Add 2 or more unit tests
  19.  
    # raise NotImplementedError
  20.  
    self.frequency.add_file('215-0.txt')
  21.  
    self.assertEqual(self.frequency.hash_table['warm'], 2)
  22.  
    self.frequency.add_file('84-0.txt')
  23.  
    self.assertEqual(self.frequency.hash_table['warm'], 11)
  24.  
     
  25.  
    def test_rarity(self) -> None:
  26.  
    # TODO: Add 2 or more unit tests
  27.  
    # raise NotImplementedError
  28.  
    self.frequency.add_file('215-0.txt')
  29.  
    self.assertEqual(self.frequency.rarity('warm'), Rarity.UNCOMMON)
  30.  
    self.assertEqual(self.frequency.rarity('the'), Rarity.COMMON)
  31.  
     
  32.  
     
  33.  
    if __name__ == '__main__':
  34.  
    unittest.main()
  35.  
     

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgagakf
系列文章
更多 icon
同类精品
更多 icon
继续加载