我有从sqlite3数据库得到的文本。我想先用CountVectorizer
得到一个文本向量来比较文本的相似性。我还有一个字典,在那里存储与messageID
相关的文本(作为字典键)。如何将每个文本向量与其messageID
相关联?e、 用一个像这样的向量数组
[[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)
按照您的示例,您有一个消息ID和句子之间的映射
然后,您需要使用
CountVectorizer
计算文本功能在每个句子中出现的次数。您可以运行与以前相同的分析:在
fit_transform()
documentation中有:这意味着每个向量以相同的顺序对应于输入文本中的一个句子(即
message_map.values()
)。如果要将ID映射到每个向量,只需执行以下操作(注意顺序是保留的):我相信你要问的是,拟合一个语料库,然后把新句子转换成一个特征向量。但是请注意,任何不在原始语料库中的新词都将被忽略,如本例所示:
您将获得:
请注意,现在比以前少了一个特性,因为单词
third
不再是特性的一部分。你知道吗在计算向量之间的相似性时,这可能有点问题,因为您将丢弃有助于区分向量的单词。你知道吗
另一方面,您可以使用英语词典或其子集作为语料库,并将其放入
vectorizer
。然而,得到的向量将变得更加稀疏,并且再次,这可能导致比较向量的问题。但这取决于计算向量之间距离的方法。你知道吗相关问题 更多 >
编程相关推荐