【发布时间】:2020-01-22 23:01:00
【问题描述】:
我有从 sqlite3 数据库中获取的文本。我想通过首先获取带有CountVectorizer 的文本向量来比较文本的相似性。我还有一本字典,用于存储与messageID 相关的文本(作为字典键)。如何将每个文本向量与其messageID 关联起来?例如用一个看起来像这样的向量数组
[[1 1 0 1 1 0 1]
[0 1 1 1 1 0 1]
[0 1 0 1 1 1 1]]
我想知道messageID = 0 有向量[1 1 0 1 1 0 1]。向量大小和数组的大小随着每条新消息而增长。
我尝试将字典放入CountVectorizer 并尝试仅评估一条消息,但均未成功。
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics.pairwise import cosine_similarity as cosineSimilarity
def getVectorsAndFeatures(strs):
text = [t for t in strs]
vectorizer = CountVectorizer(text)
vectorizer.fit(text)
vectors = vectorizer.transform(text).toarray()
features = vectorizer.get_feature_names()
return vectors, features
text = ['This is the first sentence', 'This is the second sentence',
'This is the third sentence']
messageDict = {0: 'This is the first sentence', 1: 'This is the second sentence', 2: 'This is the third sentence'}
vectors, features = getVectorsAndFeatures(text)
【问题讨论】:
-
列表项用逗号分隔,在你的文本中你没有在不同的句子之间提供逗号..
标签: python scikit-learn countvectorizer