1、gensim使用流程
2、代碼實現
from gensim import corpora, models, similarities
import jieba
# 文本集和搜索詞
texts = ['吃雞這裏所謂的吃雞並不是真的吃雞,也不是我們常用的諧音詞刺激的意思',
'而是出自策略射擊遊戲《絕地求生:大逃殺》裏的臺詞',
'我吃雞翅,你吃雞腿']
keyword = '玩過吃雞?今晚一起吃雞'
# 1、將【文本集】生成【分詞列表】
texts = [jieba.lcut(text) for text in texts]
# 2、基於文本集建立【詞典】,並提取詞典特徵數
dictionary = corpora.Dictionary(texts)
feature_cnt = len(dictionary.token2id)
# 3、基於詞典,將【分詞列表集】轉換成【稀疏向量集】,稱作【語料庫】
corpus = [dictionary.doc2bow(text) for text in texts]
# 4、使用【TF-IDF模型】處理語料庫
tfidf = models.TfidfModel(corpus)
# 5、同理,用【詞典】把【搜索詞】也轉換爲【稀疏向量】
kw_vector = dictionary.doc2bow(jieba.lcut(keyword))
# 6、對【稀疏向量集】建立【索引】
index = similarities.SparseMatrixSimilarity(tfidf[corpus], num_features=feature_cnt)
# 7、相似度計算
sim = index[tfidf[kw_vector]]
for i in range(len(sim)):
print('keyword 與 text%d 相似度爲:%.2f' % (i + 1, sim[i]))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 打印結果
- keyword 與 text1 相似度爲:0.62
keyword 與 text2 相似度爲:0.00
keyword 與 text3 相似度爲:0.12
3、過程拆解
3.1、生成分詞列表
- 對文本集中的文本進行中文分詞,返回
分詞列表
,格式如下: - [‘word1’, ‘word2’, ‘word3’, …]
import jieba
text = '七月七日長生殿,夜半無人私語時。'
words = jieba.lcut(text)
- 1
- 2
- 3
- print(words)
- [‘七月’, ‘七日’, ‘長生殿’, ‘,’, ‘夜半’, ‘無人’, ‘私語’, ‘時’, ‘。’]
3.2、基於文本集建立詞典
,獲取特徵數
- corpora.Dictionary:建立詞典
- len(dictionary.token2id):詞典中詞的個數
from gensim import corpora
import jieba
# 文本集
text1 = '堅果果實'
text2 = '堅果實在好吃'
texts = [text1, text2]
# 將文本集生成分詞列表
texts = [jieba.lcut(text) for text in texts]
print('文本集:', texts)
# 基於文本集建立詞典
dictionary = corpora.Dictionary(texts)
print('詞典:', dictionary)
# 提取詞典特徵數
feature_cnt = len(dictionary.token2id)
print('詞典特徵數:%d' % feature_cnt)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 打印結果
- 文本集: [[‘堅果’, ‘果實’], [‘堅果’, ‘實在’, ‘好吃’]]
詞典: Dictionary(4 unique tokens: [‘堅果’, ‘果實’, ‘好吃’, ‘實在’])
詞典特徵數:4
3.3、基於詞典建立語料庫
語料庫即存放稀疏向量的列表
from gensim import corpora
import jieba
text1 = '來東京吃東京菜'
text2 = '東京啊東京啊東京'
texts = [text1, text2]
texts = [jieba.lcut(text) for text in texts]
dictionary = corpora.Dictionary(texts)
print('詞典(字典):', dictionary.token2id)
# 基於詞典建立新的【語料庫】
corpus = [dictionary.doc2bow(text) for text in texts]
print('語料庫:', corpus)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 打印結果
- 詞典(字典): {‘東京’: 0, ‘吃’: 1, ‘來’: 2, ‘菜’: 3, ‘啊’: 4}
語料庫: [[(0, 2), (1, 1), (2, 1), (3, 1)], [(0, 3), (4, 2)]]
- doc2bow函數生成稀疏向量
- 1、將所有單詞取【集合】,並對每個單詞分配一個ID號
- 以
['東京', '啊', '東京', '啊', '東京']
爲例
對單詞分配ID:東京
→0
;啊
→4
變成:[0, 4, 0, 4, 0]
- 2、轉換成稀疏向量
- 0有
3
個,即表示爲(0,3
)
4有2
個,即表示爲(4,2
)
最終結果:[(0,3
), (4,2
)]
3.4、使用TF-IDF
模型處理語料庫,並建立索引
- TF-IDF是一種統計方法,用以評估一字詞對於一個文件集或一個語料庫中的其中一份文件的重要程度
from gensim import corpora, models, similarities
import jieba
text1 = '南方醫院無痛人流'
text2 = '北方人流浪到南方'
texts = [text1, text2]
texts = [jieba.lcut(text) for text in texts]
dictionary = corpora.Dictionary(texts)
feature_cnt = len(dictionary.token2id.keys())
corpus = [dictionary.doc2bow(text) for text in texts]
# 用TF-IDF處理語料庫
tfidf = models.TfidfModel(corpus)
# 對語料庫建立【索引】
index = similarities.SparseMatrixSimilarity(tfidf[corpus], num_features=feature_cnt)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- print(tfidf)
- TfidfModel(num_docs=2, num_nnz=9)
3.5、用詞典把搜索詞轉成稀疏向量
from gensim import corpora
import jieba
text1 = '南方醫院無痛人流'
text2 = '北方人流落南方'
texts = [text1, text2]
texts = [jieba.lcut(text) for text in texts]
dictionary = corpora.Dictionary(texts)
# 用【詞典】把【搜索詞】也轉換爲【稀疏向量】
keyword = '無痛人流'
kw_vector = dictionary.doc2bow(jieba.lcut(keyword))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- print(kw_vector)
- [(0, 1), (3, 1)]
3.6、相似度計算
from gensim import corpora, models, similarities
import jieba
text1 = '無痛人流並非無痛'
text2 = '北方人流浪到南方'
texts = [text1, text2]
keyword = '無痛人流'
texts = [jieba.lcut(text) for text in texts]
dictionary = corpora.Dictionary(texts)
feature_cnt = len(dictionary.token2id)
corpus = [dictionary.doc2bow(text) for text in texts]
tfidf = models.TfidfModel(corpus)
new_vec = dictionary.doc2bow(jieba.lcut(keyword))
# 相似度計算
index = similarities.SparseMatrixSimilarity(tfidf[corpus], num_features=feature_cnt)
print('\nTF-IDF模型的稀疏向量集:')
for i in tfidf[corpus]:
print(i)
print('\nTF-IDF模型的keyword稀疏向量:')
print(tfidf[new_vec])
print('\n相似度計算:')
sim = index[tfidf[new_vec]]
for i in range(len(sim)):
print('第', i+1, '句話的相似度爲:', sim[i])
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
4、附錄
-
閱讀擴展
jieba中文分詞
中文LDA模型
文本相似度分析【矩陣版】 -
註釋
En | Cn |
---|---|
corpus | n. 文集;[計]語料庫(複數:corpora ) |
sparse | adj. 稀疏的 |
vector | n. 矢量 |
Sparse Matrix Similarity | 稀疏矩陣相似性 |
word2vec | word to vector |
doc2bow | document to bag of words (詞袋) |