您可以通过查看spaCy .vocab 属性/对象并将其与GloVe文件中的单词进行比较来自行检查spaCy和GloVe模型的词汇表 . 首先将数据加载到两个列表中:
import spacy
nlp = spacy.load('en_vectors_web_lg')
spacy_words = [word for word in nlp.vocab.strings]
glove_filename = 'glove.840B.300d.txt'
glove_words = [line.split()[0].decode('utf-8') for line in open(glove_filename)]
然后检查设定差异以获得“缺失”字样:
>>> list(set(glove_words) - set(spacy_words))[:10]
[u'Inculcation', u'Dholes', u'6-night', u'AscensionMidkemia',
u'.90.99', u'USAMol', u'USAMon', u'Connerty', u'RealLife',
u'NaughtyAllie']
>>> list(set(spacy_words) - set(glove_words))[:10]
[u'ftdna', u'verplank', u'NICARIO', u'Plastic-Treated', u'ZAI-TECH',
u'Lower-Sulfur', u'desmonds', u'KUDNER', u'berlinghoff', u'50-ACRE']
有超过2.2 mio - 1.1 mio~1.1 mio字缺失:
>>> len(set(glove_words) - set(spacy_words))
1528158
请注意 nlp.vocab.strings 和 nlp.vocab.vectors 之间存在差异 . 您可以使用向量对象加载单词
vector_words = []
for key, vector in nlp.vocab.vectors.items():
try:
vector_words.append(nlp.vocab.strings[key])
except KeyError:
pass
(关于try / except:我不清楚为什么 vocab.strings 中缺少某些键)
通过此列表,您将获得:
>>> list(set(glove_words) - set(vector_words))[:10]
[u'Inculcation', u'Dholes', u'6-night', u'AscensionMidkemia', u'.90.99',
u'USAMol', u'USAMon', u'Connerty', u'RealLife', u'NaughtyAllie']