Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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 使用Enchant检查拼写时忽略某些单词_Python_Enchant - Fatal编程技术网

Python 使用Enchant检查拼写时忽略某些单词

Python 使用Enchant检查拼写时忽略某些单词,python,enchant,Python,Enchant,我正在用Python Enchant检查一些文件的拼写,希望它忽略专有名词。它纠正拼写错误的专有名词和错误地“纠正”它不知道的名词之间的权衡似乎太大了(尽管也有关于这方面的建议!) 这是我的代码,但目前它仍在更正NNP列表中的单词 chkr = SpellChecker("en_GB") f = open('test_file.txt', 'r', encoding = 'utf-8') text = f.read() tagged = pos_tag(word_token

我正在用Python Enchant检查一些文件的拼写,希望它忽略专有名词。它纠正拼写错误的专有名词和错误地“纠正”它不知道的名词之间的权衡似乎太大了(尽管也有关于这方面的建议!)

这是我的代码,但目前它仍在更正NNP列表中的单词

chkr = SpellChecker("en_GB")

f = open('test_file.txt', 'r', encoding = 'utf-8')
text = f.read()
tagged = pos_tag(word_tokenize(text))
NNP = [(word) for word, tag in tagged if tag == 'NNP']
chkr.set_text(text)
for err in chkr:
    if err is word in NNP:
        err.ignore_always()
else:
    sug = err.suggest()[0]
    err.replace(sug)

corrected = chkr.get_text()
print (NNP)
print (corrected) 
例如,在输出中,“Boojum”更改为Boomer,即使它在NNP列表中

chkr = SpellChecker("en_GB")

f = open('test_file.txt', 'r', encoding = 'utf-8')
text = f.read()
tagged = pos_tag(word_tokenize(text))
NNP = [(word) for word, tag in tagged if tag == 'NNP']
chkr.set_text(text)
for err in chkr:
    if err is word in NNP:
        err.ignore_always()
else:
    sug = err.suggest()[0]
    err.replace(sug)

corrected = chkr.get_text()
print (NNP)
print (corrected) 

有人能给我指出正确的方向吗?我对Python相当陌生。提前谢谢。

我想好了。必须告诉它错误的单词是stings,以便它可以将它们与NNP列表中的单词进行比较。新代码:

chkr = SpellChecker("en_GB")

for file in os.listdir(path):       
        f = open(file, 'r', encoding = 'utf-8')
        text = f.read()
        tagged = pos_tag(word_tokenize(text))
        NNP = [word for word, tag in tagged if tag == 'NNP']
        chkr.set_text(text)
        for err in chkr:
            if str(err.word) in NNP:
                err.ignore_always()
            else:
                sug = chkr.suggest()
                if len(sug) is not 0:
                    err.replace(sug[0])

        corrected = chkr.get_text()

还进行了更正,以便如果Enchant没有任何建议,它将保留错误。

我认为这将用于案例或错误中?这里也合适吗?你可以写一些IO plz吗?我编辑过:)