当前位置: 首页>>代码示例>>Python>>正文


Python Dictionary.verify方法代码示例

本文整理汇总了Python中dictionary.Dictionary.verify方法的典型用法代码示例。如果您正苦于以下问题:Python Dictionary.verify方法的具体用法?Python Dictionary.verify怎么用?Python Dictionary.verify使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dictionary.Dictionary的用法示例。


在下文中一共展示了Dictionary.verify方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: checkFile

# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import verify [as 别名]
def checkFile(file_name, dictionary_file="words.dat"):
    # Set up dictionary based on words.dat
    d = Dictionary(file_name=dictionary_file)

    file_in = open(file_name, 'r')
    file_out = open("{}.out".format(file_name), 'w')

    current_word = ""

    while True:
        # Read one character at a time from the input file
        next_char = file_in.read(1)
        # Exit the loop when there's nothing else to read
        if not next_char:
            break

        if next_char in d.ALLOWED_LETTERS:
            current_word += next_char
        else:
            # Verify the current_word with the dictionary
            resp, current_word = d.verify(current_word)
            if not resp:  # Word was not found in dictionary
                resp, new_word = getUserResponse(current_word)
                d.update(resp, current_word, new_word)
                current_word = new_word
            file_out.write(current_word)
            current_word = ""
            file_out.write(next_char)

    file_in.close()
    file_out.close()
    print("Spellchecked file written to {}.out.".format(file_name))
开发者ID:tomhogans,项目名称:cs113,代码行数:34,代码来源:spellCheck.py

示例2: Dictionary

# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import verify [as 别名]
    return word_list

if __name__ == '__main__':
    this_dict = Dictionary()
    output_string = ''
    try:
        # Prompt user for the name of a document she wants spell-checked.
        file_name = input("Name of the document to be spell-checked: ")
        file = open(file_name)
        # spellCheck should read words from the specified document,
        # one-by-one and test if the words appears in its dictionary.
        for line in file.readlines():
            word_list = parse_line(line)
            index = 0
            for word in word_list:
                if not this_dict.verify(word):
                    def user_prompt(word, index, line, dict_name):
                        print("\n{:^62}".format(word))
                        prompt = "replace(R), replace all(P), ignore(I), ignore all(N), exit(E): "
                        while True:
                            user_cmd = input(prompt)
                            user_cmd = user_cmd.lower()
                            if user_cmd == 'i':
                                return line
                                break
                            elif user_cmd == 'r':
                                replacement_word = input("Replacement word: ")
                                line = re.sub(word, replacement_word, line, flags=re.IGNORECASE)
                                return line
                                break
                            elif user_cmd == 'n':
开发者ID:spacecoffin,项目名称:OOPproj2,代码行数:33,代码来源:spellCheck.py

示例3: spellcheckandoutput

# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import verify [as 别名]
def spellcheckandoutput(text="mytext"):
    """
    reads words from the file to be spell-checked and checks if the word
    is in the dictionary. If not, user is queried for input. The
    output is written to the file.
    """
    adict = Dictionary()
    infile = open(text, 'r')
    outfile = open(text+".out", 'w')

    def processinput(choice, newword, word):
        """
        update the dictionary based on the user input.
        If the user wants to exit, copy the rest of the input file to
        the output
        """
        if choice in ['p','P','n','N']:
            adict.update(choice,newword, word)
        elif choice in ['e','E']:
            for line in infile:
                outfile.write(line)
            
    try:
        wordch = []
        #apostrophe_count = 0
        while True:
            char = infile.read(1)
            if not char:
                if len(wordch) > 0:
                    word = ''.join(wordch)
                    if word[-1] is "'":
                        word = word[:-1] # strip trailing apostrophes
                    newword = adict.verify(word)
                    if newword is None:
                        choice, newword = askuser(word)
                        outfile.write(newword)
                        processinput(choice, newword, word)
                    else:
                        outfile.write(newword)
                break
            elif char.isalpha():
                wordch.append(char)
            elif char is "'":
                if wordch: # don't begin words with apostrophes
                    wordch.append(char)
                    #apostrophe_count += 1
            elif len(wordch) == 0:
                outfile.write(char)
                # not done this
            else:
                word = ''.join(wordch)
                if word[-1] is "'":
                    word = word[:-1] # strip trailing apostrophes
                newword = adict.verify(word)
                if newword is None:
                    choice, newword = askuser(word)
                    outfile.write(newword); outfile.write(char)
                    processinput(choice, newword, word)
                else:
                    outfile.write(newword); outfile.write(char)
                wordch = []
    finally:
        infile.close()
        outfile.close()
开发者ID:spacecoffin,项目名称:OOPproj03,代码行数:66,代码来源:spellCheck.py


注:本文中的dictionary.Dictionary.verify方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。