【问题标题】:Ignore certain words when spell checking with Enchant使用 Enchant 进行拼写检查时忽略某些单词
【发布时间】:2017-11-10 18:34:38
【问题描述】:

我正在使用 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 列表中。

有人能指出我正确的方向吗?我对 Python 还很陌生。提前致谢。

【问题讨论】:

  • 我以为是用的case还是出错了?这里也合适吗?
  • 请你写一些IO好吗?
  • 我已经编辑了:)

标签: python enchant


【解决方案1】:

我想通了。必须告诉它错误词是刺,以便它可以将它们与 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 没有任何建议,它将保留错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 2020-01-21
    相关资源
    最近更新 更多