Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyEnchant';纠正';字典里的词和字典里没有的词_Python_Dictionary_Spelling_Pyenchant_Enchant - Fatal编程技术网

Python PyEnchant';纠正';字典里的词和字典里没有的词

Python PyEnchant';纠正';字典里的词和字典里没有的词,python,dictionary,spelling,pyenchant,enchant,Python,Dictionary,Spelling,Pyenchant,Enchant,我正试图从一个网络论坛中获取大量的自然语言,并用PyEnchant纠正拼写错误。文本通常是非正式的,涉及医疗问题,因此我创建了一个文本文件“test.pwl”,其中包含相关的医疗词汇、聊天缩写等。在某些情况下,一些html、URL等确实不幸地保留在其中 我的脚本设计为使用en_US字典和PWL查找所有拼写错误的单词,并将它们完全自动更正为d.suggest的第一个建议。它打印拼写错误的单词列表,然后打印没有建议的单词列表,并将更正后的文本写入“spellfixed.txt”: import en

我正试图从一个网络论坛中获取大量的自然语言,并用PyEnchant纠正拼写错误。文本通常是非正式的,涉及医疗问题,因此我创建了一个文本文件“test.pwl”,其中包含相关的医疗词汇、聊天缩写等。在某些情况下,一些html、URL等确实不幸地保留在其中

我的脚本设计为使用en_US字典和PWL查找所有拼写错误的单词,并将它们完全自动更正为d.suggest的第一个建议。它打印拼写错误的单词列表,然后打印没有建议的单词列表,并将更正后的文本写入“spellfixed.txt”:

import enchant
import codecs

def spellcheckfile(filepath):
    d = enchant.DictWithPWL("en_US","test.pwl")
    try:
        f = codecs.open(filepath, "r", "utf-8")
    except IOError:
        print "Error reading the file, right filepath?"
        return
    textdata = f.read()
    mispelled = []
    words = textdata.split()
    for word in words:
        # if spell check failed and the word is also not in
        # mis-spelled list already, then add the word
        if d.check(word) == False and word not in mispelled:
            mispelled.append(word)
    print mispelled
    for mspellword in mispelled:
        #get suggestions
        suggestions=d.suggest(mspellword)
        #make sure we actually got some
        if len(suggestions) > 0:
            # pick the first one
            picksuggestion=suggestions[0]
        else: print mspellword
        #replace every occurence of the bad word with the suggestion
        #this is almost certainly a bad idea :)
        textdata = textdata.replace(mspellword,picksuggestion)
    try:
        fo=open("spellfixed.txt","w")
    except IOError:
        print "Error writing spellfixed.txt to current directory. Who knows why."
        return 
    fo.write(textdata.encode("UTF-8"))
    fo.close()
    return
问题在于,输出通常包含字典或pwl中单词的“更正”。例如,当输入的第一部分是:

我的新医生觉得我现在是双极的。在被其他人认为是极度沮丧的9年之后

我明白了:

我的新宠觉得我现在有躁郁症。在被其他人认为是极度沮丧的9年之后

我可以处理病例的变化,但dotor医生一点也不在行。当输入较短时(例如,上述引用是整个输入),结果是理想的:

我的新医生觉得我现在有躁郁症。在被其他人认为是极度沮丧的9年之后


谁能给我解释一下原因吗?请简单地说,因为我对编程非常陌生,对Python也比较新。一个循序渐进的解决方案将不胜感激。

我认为您的问题是您正在替换单词中的字母序列。“ER”可能是对“ER”的有效拼写更正,但这并不意味着您应该将“考虑”改为“考虑”

可以使用正则表达式而不是简单的文本替换,以确保只替换全字。正则表达式中的“\b”表示“单词边界”:

你是对的,这是个坏主意。这就是导致“已考虑”被“已考虑”取代的原因。而且,即使你找不到建议,你也在做替换。将替换项移动到
if len(suggestions)>0

至于替换单词的每个实例,您要做的是将拼写错误的单词的位置与拼写错误的单词的文本一起保存(或者可能只是位置,您可以在以后查找建议时在文本中查找单词),允许重复拼写错误的单词,并仅用其建议替换单个单词


不过,我将把实现细节和优化留给您。一个循序渐进的解决方案不会帮你学到那么多。

谢谢你,伙计。我确实知道正则表达式,但由于对编程和Python如此陌生,我不知道如何在代码中实现单词边界分隔符。。。线索?我是通过这样做来实现的:textdata=textdata.replace(“\\b”+mspellword+“\\b”、“\\b”+picksuggestion+“\\b”)@user2437842不完全正确,您需要使用正则表达式函数,如
re.sub
而不是字符串
replace
。看到我的答案中剪下的代码以及。您可以将正则表达式构造为
“\\b”+re.escape(mspellword)+“\\b”
。您要插入作为替换的文本(
picksuggestion
)不应转换为正则表达式。我感谢您的智慧和精神。谢谢我希望我爸爸有时能像你一样。也就是说,恐怕你所描述的完全超出了我的能力范围。保存单词位置和允许重复是我做梦都没想到的事情。我很高兴“为它工作”,但我想我需要一个推动开始。是朋友吗?@user2437842嘿,很公平。查看元组。
(单词、位置)
的元组应该可以工作。
>>> "considered at the er".replace( "er", "ER" )
'considERed at the ER'
>>> import re
>>> re.sub( "\\b" + "er" + "\\b", "ER", "considered at the er" )
'considered at the ER'
    #replace every occurence of the bad word with the suggestion
    #this is almost certainly a bad idea :)