簡體   English   中英

如何使用 gensim.similarities.Similarity 查找兩個句子之間的相似性

[英]How to use gensim.similarities.Similarity to find similarity between two sentences

我想編寫代碼來找到兩個句子之間的相似性,然后我最終使用 nltk 和 gensim 編寫了這段代碼。 我使用標記化和 gensim.similarities.Similarity 來完成這項工作。 但這不符合我的目的。 在我介紹最后一行代碼之前,它工作正常。

import gensim
import nltk

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."]
from nltk.tokenize import word_tokenize
gen_docs = [[w.lower() for w in word_tokenize(text)]
        for text in raw_documents]



dictionary = gensim.corpora.Dictionary(gen_docs)
print(dictionary[5])
print(dictionary.token2id['socks'])
print("Number of words in dictionary:",len(dictionary))
for i in range(len(dictionary)):
    print(i, dictionary[i])

corpus = [dictionary.doc2bow(gen_doc) for gen_doc in gen_docs]
print(corpus)

tf_idf = gensim.models.TfidfModel(corpus)
print(tf_idf)
    s = 0
for i in corpus:
s += len(i)
print(s)

sims = gensim.similarities.Similarity('/usr/workdir/',tf_idf[corpus],
                                  num_features=len(dictionary))
print(sims)
print(type(sims))


query_doc = [w.lower() for w in word_tokenize("Socks are a force for good.")]
print(query_doc)
query_doc_bow = dictionary.doc2bow(query_doc)
print(query_doc_bow)
query_doc_tf_idf = tf_idf[query_doc_bow]
print(query_doc_tf_idf)

sims[query_doc_tf_idf]

它拋出這個錯誤。 我在互聯網上的任何地方都找不到答案。

Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\gensim\utils.py", line 679, in save
_pickle.dump(self, fname_or_handle, protocol=pickle_protocol)
TypeError: file must have a 'write' attribute

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "semantic.py", line 45, in <module>
    sims[query_doc_tf_idf]
  File "C:\Python36\lib\site-packages\gensim\similarities\docsim.py", line 
503, in __getitem__
    self.close_shard()  # no-op if no documents added to index since last 
query
 File "C:\Python36\lib\site-packages\gensim\similarities\docsim.py", line 
427, in close_shard
    shard = Shard(self.shardid2filename(shardid), index)
 File "C:\Python36\lib\site-packages\gensim\similarities\docsim.py", line 
110, in __init__
    index.save(self.fullname())
  File "C:\Python36\lib\site-packages\gensim\utils.py", line 682, in save
    self._smart_save(fname_or_handle, separately, sep_limit, ignore, 
pickle_protocol=pickle_protocol)
  File "C:\Python36\lib\site-packages\gensim\utils.py", line 538, in 
_smart_save
    pickle(self, fname, protocol=pickle_protocol)
  File "C:\Python36\lib\site-packages\gensim\utils.py", line 1337, in pickle
    with smart_open(fname, 'wb') as fout:  # 'b' for binary, needed on 
Windows
  File "C:\Python36\lib\site-packages\smart_open\smart_open_lib.py", line 
181, in smart_open
fobj = _shortcut_open(uri, mode, **kw)
  File "C:\Python36\lib\site-packages\smart_open\smart_open_lib.py", line 
287, in _shortcut_open
return io.open(parsed_uri.uri_path, mode, **open_kwargs)

請幫助找出問題所在。

如果在實例化相似性時指定有效路徑,則查詢應該有效。 對於下面的示例,我在 C 驅動器上創建了一個目錄 Similarity,並在函數調用中為文件指定了目錄路徑和名稱。

sims = gensim.similarities.Similarity('C:/Similarity/sims',tf_idf[corpus],
                                  num_features=len(dictionary))
print(sims)
print(type(sims))

query_doc = [w.lower() for w in word_tokenize("Socks are a force for good.")]
print(query_doc)
query_doc_bow = dictionary.doc2bow(query_doc)
print(query_doc_bow)
query_doc_tf_idf = tf_idf[query_doc_bow]
print(query_doc_tf_idf)

print('Query result:', sims[query_doc_tf_idf])

Query result: [0.       0.84565616   0.      0.06124881   0.        ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM