0

我有一个段落列表,我想检查这些单词是否是有效的英文单词。有时,由于一些外部问题,我可能无法在这些段落中找到有效的英文单词。我知道像 pyenchant 和 nltk 这样的库,它们有一组字典并提供一定程度的准确性,但这两者都没有什么缺点。我想知道是否存在另一个库或程序可以尽可能准确地为我提供我正在寻找的东西。

4

2 回答 2

1

这在很大程度上取决于您所说的有效英语单词是什么意思。ECG、Thor 或 Loki 是有效的英文单词吗?如果您对有效词的定义不同,您可能需要创建自己的语言模型。无论如何,除了明显使用 pyEnchant 或 nltk 之外,我建议使用 fasttext 库。它有多个预先构建的词向量模型,您可以检查您的段落中是否有稀有或词汇表外的词。您本质上要检查的是,此特定“无效”单词的单词嵌入表示是否对应于少量(或零)其他单词。您可以直接从 python 使用 fasttext

pip install fasstext

或者您可以使用 gensim 库(它也会为您提供一些额外的算法,例如 Word2Vec,这对您的情况也很有用)

pip install --upgrade gensim

或者对于康达

conda install -c conda-forge gensim

假设您使用 gensim 并且使用预训练的 fasttext 模型:

from gensim.models import FastText
from gensim.test.utils import datapath

cap_path = datapath("fasttext-model.bin")
fb_model = load_facebook_model(cap_path)

现在您可以执行多项任务来实现您的目标: 1. 检查词汇表外

'mybizarreword' in fb_model.wv.vocab
  1. 检查相似性
fb_model.wv.most_similar("man")

对于稀有词,您将获得低分,通过设置阈值,您将决定哪个词不是“有效”

于 2019-10-16T09:09:18.047 回答
-1

Linux 和 Mac OS X 有一个单词列表,你可以直接使用,否则你可以下载一个英文单词列表。您可以按如下方式使用它:

d = {}
fname = "/usr/share/dict/words"
with open(fname) as f:
    content = f.readlines()

for w in content:
    d[w.strip()] = True

p ="""I have a list of paragraphs, I would like to check if these words are valid English words or not. Sometimes, due to some external issues, i might not get valid English words in these paragraphs. I am aware of libraries like pyenchant and nltk which have a set of dictionaries and provide accuracy of some level but both of these have few drawbacks. I wonder if there exists another library or procedure that can provide me with what I am looking for with at-most accuracy possible."""

lw = []
for w in p.split():
    if len(w) < 4:
        continue
    if d.get(w, False):
        lw.append(w)

print(len(lw))
print(lw)

#43
#['have', 'list', 'would', 'like', 'check', 'these', 'words', 'valid', 'English', 'words', 'some', 'external', 'might', 'valid', 'English', 'words', 'these', 'aware', 'libraries', 'like', 'which', 'have', 'dictionaries', 'provide', 'accuracy', 'some', 'level', 'both', 'these', 'have', 'wonder', 'there', 'exists', 'another', 'library', 'procedure', 'that', 'provide', 'with', 'what', 'looking', 'with', 'accuracy']


于 2019-10-17T13:02:26.627 回答