python - 如何从文档(数据集)中查找和打印不匹配/不相似的单词?
问题描述
我正在尝试重写基本上采用输入文本文件并与不同文档进行比较并得出相似性的算法。
现在我想打印不匹配单词的输出并输出一个带有不匹配单词的新纺织品。
从此代码中,“hello force”是输入,并根据 raw_documents 检查并打印出 0-1 之间匹配文档的排名(单词“force”与第二个文档匹配,输出为第二个文档提供更高的排名,但“hello”不在任何 raw_document 我想打印不匹配的单词“hello”作为不匹配),但我想要打印与任何 raw_document 都不匹配的不匹配的输入单词
import gensim
import nltk
from nltk.tokenize import word_tokenize
raw_documents = ["I'm taking the show on the road",
"My socks are a force multiplier.",
"I am the barber who cuts everyone's hair who doesn't cut their own.",
"Legend has it that the mind is a mad monkey.",
"I make my own fun."]
gen_docs = [[w.lower() for w in word_tokenize(text)]
for text in raw_documents]
dictionary = gensim.corpora.Dictionary(gen_docs)
corpus = [dictionary.doc2bow(gen_doc) for gen_doc in gen_docs]
tf_idf = gensim.models.TfidfModel(corpus)
s = 0
for i in corpus:
s += len(i)
sims = gensim.similarities.Similarity('/usr/workdir/',tf_idf[corpus],
num_features=len(dictionary))
query_doc = [w.lower() for w in word_tokenize("hello force")]
query_doc_bow = dictionary.doc2bow(query_doc)
query_doc_tf_idf = tf_idf[query_doc_bow]
result = sims[query_doc_tf_idf]
print result
解决方案
推荐阅读
- python-3.x - 为 Python3 设置 SublimeText……这个命令有什么作用?
- typescript - 带有通用键和常规键的打字稿对象?
- postgresql - 特定元素的 JSONB 数组更新
- python - 将函数应用于 DataFrame 列的字典理解
- excel - excel vba userform设置数量值不能小于0
- javascript - Testcafe Selector 没有在 React Modal 中获取元素
- c# - 如何计算所有元素都是字符串的矩阵的行列式?
- api - 认证微服务解耦
- tfs - 是否可以为本地 TFS 2018 设置代理发布代理?
- reactjs - 静态服务的 create-react-app 是否可以使用 react-router browserHistory *并且*完全保持客户端(在路由上没有网络获取,没有 SSR)