?
?
參考:python文本相似度計算
原始語料格式:一個文件,一篇文章。
#
!/usr/bin/env python
#
-*- coding: UTF-8 -*-
import
jieba
from
gensim
import
corpora,models,similarities
import
codecs
def
cut_words(file):
with open(file,
'
r
'
,encoding=
"
utf-8
"
) as f:
text
=
f.read()
words
=
jieba.lcut(text)
#
print(len(words),words) #查看分詞結(jié)果
return
words
def
drop_Disable_Words(cut_res,stopwords):
res
=
[]
for
word
in
cut_res:
if
word
in
stopwords
or
word ==
"
\n
"
or
word ==
"
\u3000
"
:
continue
res.append(word)
#
print(len(res),res) #查看去停用詞結(jié)果
return
res
def
read_stop_word(file_path):
file
=
file_path
stopwords
= codecs.open(file,
'
r
'
,encoding=
'
utf8
'
).readlines()
stopwords
= [ w.strip()
for
w
in
stopwords ]
return
stopwords
#
讀取原始語料、停用詞表
files = [
'
file1.txt
'
,
'
file2.txt
'
,
'
file3.txt
'
]
stopwords
= read_stop_word(
"
stop_word.txt
"
)
#
分詞、去停用詞
corpus =
[]
for
file
in
files:
#
分詞
cut_res =
cut_words(file)
#
去停用詞
res =
drop_Disable_Words(cut_res,stopwords)
corpus.append(res)
#
print(len(corpus))
#
建立詞袋模型
dictionary =
corpora.Dictionary(corpus)
doc_vectors
= [dictionary.doc2bow(text)
for
text
in
corpus]
#
print(len(doc_vectors),doc_vectors)
#
####################################################################
#
print("文檔數(shù)目:")
#
print (dictionary.num_docs)
#
#
print("所有詞的個數(shù):")
#
print(dictionary.num_pos )
#
#
print("單詞在文檔中出現(xiàn)的次數(shù):")
#
print(dictionary.dfs )
#
#
print("字典,{單詞id:對應(yīng)的詞}")
#
print((dictionary.id2token))
#
#
print ("字典,{詞:對應(yīng)的單詞id}")
#
print((dictionary.token2id))
#
print ("每個文件中不重復(fù)詞個數(shù)的和")
#
print(dictionary.num_nnz) #每個文件中不重復(fù)詞個數(shù)的和
#
#########################################################################
tfidf
=
models.TfidfModel(doc_vectors)
tfidf_vectors
=
tfidf[doc_vectors]
print
(len(tfidf_vectors))
print
(len(tfidf_vectors[0]))
print
(tfidf_vectors[0])
#
建立TF-IDF模型
def
TF_IDF(tfidf_vectors,doc_vectors):
index
=
similarities.MatrixSimilarity(tfidf_vectors)
sims
=
index[doc_vectors[0]]
print
(list(enumerate(sims)))
#
建立LSI模型
def
LSI(tfidf_vectors,dictionary,doc_vectors,theme_num):
lsi
= models.LsiModel(tfidf_vectors, id2word=dictionary, num_topics=
theme_num)
lsi_vector
=
lsi[tfidf_vectors]
query_lsi
=
lsi[doc_vectors[0]]
index
=
similarities.MatrixSimilarity(lsi_vector)
sims
=
index[query_lsi]
print
(list(enumerate(sims)))
#
使用LSI模型計算相似度
LSI(tfidf_vectors,dictionary,doc_vectors,2
)
#
使用TF-IDF模型計算相似度
TF_IDF(tfidf_vectors,doc_vectors)
?
?
原始語料格式如下示例:
汽車 酷似卡宴 華泰新SUV寶利格廣州車展上市 http://auto.data.people.com.cn/news/story_428419.html 華泰在推出自主轎車B11后,又一款自主SUV寶利格已經(jīng)確定將在11月下旬的廣州車展上市正式上市,新車將與B11一樣搭載1.8T汽油機(jī)和2.0T柴油機(jī),預(yù)計售價10-15萬元之間,最大的亮點就是酷似保時捷卡宴的外觀。;泰寶利格憑借酷似保時捷卡宴的外觀而頗受關(guān)注,這款車整體外形設(shè)計厚重敦實,有著SUV應(yīng)有的硬朗和雄渾,其車身采用了大量的鍍鉻裝飾和銀色裝飾件,凸顯年輕和時尚;同時寶利格也繼承了華泰家族式臉譜造型,與華泰B11相似的前臉采用了倒梯形網(wǎng)狀前格柵,新款雙氙氣大燈不僅提供更加理想的照明效果,也將成為寶利格的獨特標(biāo)識。;泰寶利格2620毫米的軸距帶來的車內(nèi)空間寬大而理想,其內(nèi)飾方面也與卡宴經(jīng)典車型非常接近,內(nèi)飾配色采用米色和淺棕色的組合,并配備木紋飾板以提升質(zhì)感,有著典型的歐系風(fēng)情,帶給消費(fèi)者極佳的視覺享受。此外,B35還配備了大屏幕的液晶屏和自動空調(diào)等多種舒適性配置。T詼力上,華泰寶利格使用的是源于意大利VM技術(shù)的清潔柴油發(fā)動機(jī),搭載1.8T渦輪增壓發(fā)動機(jī),最大功率160馬力,最大扭矩215Nbm,采用全時四輪技術(shù)驅(qū)動。這一動力配置再次彰顯華泰汽車低碳、環(huán)保的產(chǎn)品理念,迎合了高端人士對環(huán)保低碳的需求。;泰寶利格采用的是前麥弗遜、后多連桿的前后獨立懸掛設(shè)置,車身也是更強(qiáng)調(diào)舒適性能的承載式車身,定位上將更加偏向都市SUV。就定位而言,未來寶利格將作為華泰SUV系列中的高端產(chǎn)品,與圣達(dá)菲一樣主攻城市SUV領(lǐng)域。...全文 > (來源:網(wǎng)上車市)
汽車 最受歡迎TOP10車型出爐 帝豪EC7脫穎而出 http://auto.data.people.com.cn/news/story_390275.html 愛美之心人皆有之,汽車的外觀炫感度所形成的沖擊力,往往會給第一眼與之接觸的消費(fèi)者留下?lián)]之不去的深刻印象。當(dāng)前,隨著中國汽車市場發(fā)展的日趨成熟,消費(fèi)者對汽車外型的要求也變得越來越挑剔和苛刻,經(jīng)媒體廣泛調(diào)查發(fā)現(xiàn),"車型外觀"已經(jīng)成為購車者的首選考慮因素之一。D敲矗究竟哪些品牌的車型外觀更酷、更贊、更大氣,同時也更加受到大眾 的認(rèn)同和青睞呢?為了解答這個疑問,汽車點評網(wǎng)聯(lián)合《汽車雜志》、《轟》雜志以及互聯(lián)網(wǎng)消費(fèi)者調(diào)研中心在近期舉辦了"全球最受歡迎外觀車型大評選"活動。F車也選美,帝豪EC7脫穎而出5酆潰牛茫釩潦友竅黨迪盜校一舉從眾多參選車型中脫穎而出,成為全球十大最受歡迎外觀車型之一,可以說是絲毫不使人感到意外的。我們可以看到,帝豪EC7外觀的優(yōu)越性、高端性塑造,凸顯出其強(qiáng)勁的全球競爭實力和同歐美系品牌分庭抗衡的信心。M時,帝豪EC7的此番登頂,也意味著國產(chǎn)汽車品牌將迎來一個全新的歷程,并且對國產(chǎn)汽車進(jìn)入快速崛起模式同樣具有深刻的啟示意義。正如評選參與者所言:"帝豪EC7車型外觀大氣、穩(wěn)重,是近年來國產(chǎn)車中外觀設(shè)計最好的車型之一。找到了價格、安全、空間、動力、材質(zhì)、做工的平衡點,是一款性價比很高的車型。"9產(chǎn)車發(fā)力,帝豪系列眾望所歸O笳髯牛⒑闌、穩(wěn)健、力量"的帝豪EMGRAND系列可謂國產(chǎn)汽車中的精品,此系列中的各款車型皆具大家風(fēng)范,圓潤、飽滿的構(gòu)造,以及簡潔、明快的線條,使整個車型外觀盡顯大氣并活力四射。4擁酆潰牛茫返劍牛茫福詮釋了吉利帝豪 向歐洲標(biāo)準(zhǔn)看齊的步伐正不斷加快,而由國外著名設(shè)計公司組成的團(tuán)隊,也給帝豪的工藝水平提升帶來了質(zhì)的飛躍。之所以帝豪總能給人一種國際尖端品牌的駕乘感受,與這些因素是分不開的。L氐愕娜諍稀⑿閱艿謀Vぁ⒆鴯蟮奶逖欏⒑俠淼男約郟這些都是帝豪能在此次評選活動中笑傲群雄的基礎(chǔ),同時也證明了帝豪不斷躋身國際中高端汽車行業(yè)的實力,吉利帝豪做為國產(chǎn)車成功的典范,不久的將來,將在世界汽車市場上享有自己的一席之地。#保最受歡迎TOP10車型出爐 帝豪EC7脫穎而出
#
!/usr/bin/env python
#
-*- coding: UTF-8 -*-
import
pandas as pd
import
jieba
def
drop_stopwords(content_res,stopwords):
contents_clean
=
[]
all_words
=
[]
for
line
in
content_res:
#
用于存儲清洗后的詞
line_clean =
[]
for
word
in
line:
#
如果這個詞出現(xiàn)在停用詞里,過濾掉
if
word
in
stopwords:
continue
#
存儲過濾后的詞
line_clean.append(word)
all_words.append(str(word))
#
把已經(jīng)清洗的列表存儲起來
contents_clean.append(line_clean)
return
contents_clean,all_words
#
contents_clean為清理完的數(shù)據(jù),為二維列表
#
讀取語料數(shù)據(jù)
df_news = pd.read_table(
"
val.txt
"
,names=[
'
category
'
,
'
theme
'
,
'
url
'
,
'
content
'
],encoding=
'
utf-8
'
)
df_news
=
df_news.dropna()
#
將數(shù)據(jù)轉(zhuǎn)為二維列表:list of list
content = df_news.content.values.tolist()
#
將每個content列轉(zhuǎn)為列表,結(jié)果為二維列表
#
讀取停用詞表
df_stop= pd.read_csv(
"
stop_word.txt
"
,encoding=
"
utf-8
"
,sep =
"
\n
"
,names = [
'
stopword
'
])
#
將數(shù)據(jù)轉(zhuǎn)為二維列表:list of list
stopwords =
df_stop.stopword.values.tolist()
#
分詞,數(shù)據(jù)格式:list of list
content_res =
[]
for
line
in
content:
current_segment
=
jieba.lcut(line)
if
len(current_segment) > 1
and
current_segment !=
'
\r\n
'
:
content_res.append(current_segment)
#
清停用詞,數(shù)據(jù)格式:list of list
contents_clean, all_words =
drop_stopwords(content_res,stopwords)
#
查看清洗后的數(shù)據(jù)
#
df_content =pd.DataFrame({'content_res':contents_clean})
#
print(df_content.head())
#
詞頻統(tǒng)計
#
df_all_words = pd.DataFrame({'all_words':all_words})
#
words_count = df_all_words.groupby(by=['all_words'])['all_words'].agg({"count":np.size})
#
words_count =words_count.reset_index().sort_values(by=['count'],ascending=False)
#
print(words_count.head())
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

