【发布时间】:2020-03-15 17:42:39
【问题描述】:
我创建了一个小型测试语料库:
words = ["he she why fun", "you are why it", "believe it or stop", 'hello goodbye it', 'i goodbye']
print(len(words))
我正在尝试创建一个字典,其中键是唯一的单词,值是它们来自的文档。所以我创建了这个例程:
count = 0
while count < len(words):
for word in words[count].split():
p = " ".join(words[0:count]) + " " + " ".join(words[count+1:len(words)])
if word not in p.split():
dc[word] = count
count += 1
print(dc)
{'he': 0, 'she': 0, 'fun': 0, 'you': 1, 'are': 1, 'believe': 2, 'or': 2, 'stop': 2, 'hello': 3, 'i': 4}
这行得通,但它很笨重。有没有办法使用计数矢量化器、TF-IDF 或一些 Spacy 函数,也许可以做到这一点?我还担心可读性,即字典格式看起来不太好。
【问题讨论】:
标签: python-3.x nlp countvectorizer tfidfvectorizer