?
?
參考: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) #查看分詞結果 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) #查看去停用詞結果 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("文檔數目:") # print (dictionary.num_docs) # # print("所有詞的個數:") # print(dictionary.num_pos ) # # print("單詞在文檔中出現的次數:") # print(dictionary.dfs ) # # print("字典,{單詞id:對應的詞}") # print((dictionary.id2token)) # # print ("字典,{詞:對應的單詞id}") # print((dictionary.token2id)) # print ("每個文件中不重復詞個數的和") # print(dictionary.num_nnz) #每個文件中不重復詞個數的和 # ######################################################################### 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寶利格已經確定將在11月下旬的廣州車展上市正式上市,新車將與B11一樣搭載1.8T汽油機和2.0T柴油機,預計售價10-15萬元之間,最大的亮點就是酷似保時捷卡宴的外觀。;泰寶利格憑借酷似保時捷卡宴的外觀而頗受關注,這款車整體外形設計厚重敦實,有著SUV應有的硬朗和雄渾,其車身采用了大量的鍍鉻裝飾和銀色裝飾件,凸顯年輕和時尚;同時寶利格也繼承了華泰家族式臉譜造型,與華泰B11相似的前臉采用了倒梯形網狀前格柵,新款雙氙氣大燈不僅提供更加理想的照明效果,也將成為寶利格的獨特標識。;泰寶利格2620毫米的軸距帶來的車內空間寬大而理想,其內飾方面也與卡宴經典車型非常接近,內飾配色采用米色和淺棕色的組合,并配備木紋飾板以提升質感,有著典型的歐系風情,帶給消費者極佳的視覺享受。此外,B35還配備了大屏幕的液晶屏和自動空調等多種舒適性配置。T詼力上,華泰寶利格使用的是源于意大利VM技術的清潔柴油發動機,搭載1.8T渦輪增壓發動機,最大功率160馬力,最大扭矩215Nbm,采用全時四輪技術驅動。這一動力配置再次彰顯華泰汽車低碳、環保的產品理念,迎合了高端人士對環保低碳的需求。;泰寶利格采用的是前麥弗遜、后多連桿的前后獨立懸掛設置,車身也是更強調舒適性能的承載式車身,定位上將更加偏向都市SUV。就定位而言,未來寶利格將作為華泰SUV系列中的高端產品,與圣達菲一樣主攻城市SUV領域。...全文 > (來源:網上車市)
汽車 最受歡迎TOP10車型出爐 帝豪EC7脫穎而出 http://auto.data.people.com.cn/news/story_390275.html 愛美之心人皆有之,汽車的外觀炫感度所形成的沖擊力,往往會給第一眼與之接觸的消費者留下揮之不去的深刻印象。當前,隨著中國汽車市場發展的日趨成熟,消費者對汽車外型的要求也變得越來越挑剔和苛刻,經媒體廣泛調查發現,"車型外觀"已經成為購車者的首選考慮因素之一。D敲矗究竟哪些品牌的車型外觀更酷、更贊、更大氣,同時也更加受到大眾 的認同和青睞呢?為了解答這個疑問,汽車點評網聯合《汽車雜志》、《轟》雜志以及互聯網消費者調研中心在近期舉辦了"全球最受歡迎外觀車型大評選"活動。F車也選美,帝豪EC7脫穎而出5酆潰牛茫釩潦友竅黨迪盜校一舉從眾多參選車型中脫穎而出,成為全球十大最受歡迎外觀車型之一,可以說是絲毫不使人感到意外的。我們可以看到,帝豪EC7外觀的優越性、高端性塑造,凸顯出其強勁的全球競爭實力和同歐美系品牌分庭抗衡的信心。M時,帝豪EC7的此番登頂,也意味著國產汽車品牌將迎來一個全新的歷程,并且對國產汽車進入快速崛起模式同樣具有深刻的啟示意義。正如評選參與者所言:"帝豪EC7車型外觀大氣、穩重,是近年來國產車中外觀設計最好的車型之一。找到了價格、安全、空間、動力、材質、做工的平衡點,是一款性價比很高的車型。"9產車發力,帝豪系列眾望所歸O笳髯牛⒑闌、穩健、力量"的帝豪EMGRAND系列可謂國產汽車中的精品,此系列中的各款車型皆具大家風范,圓潤、飽滿的構造,以及簡潔、明快的線條,使整個車型外觀盡顯大氣并活力四射。4擁酆潰牛茫返劍牛茫福詮釋了吉利帝豪 向歐洲標準看齊的步伐正不斷加快,而由國外著名設計公司組成的團隊,也給帝豪的工藝水平提升帶來了質的飛躍。之所以帝豪總能給人一種國際尖端品牌的駕乘感受,與這些因素是分不開的。L氐愕娜諍稀⑿閱艿謀Vぁ⒆鴯蟮奶逖欏⒑俠淼男約郟這些都是帝豪能在此次評選活動中笑傲群雄的基礎,同時也證明了帝豪不斷躋身國際中高端汽車行業的實力,吉利帝豪做為國產車成功的典范,不久的將來,將在世界汽車市場上享有自己的一席之地。#保最受歡迎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: # 如果這個詞出現在停用詞里,過濾掉 if word in stopwords: continue # 存儲過濾后的詞 line_clean.append(word) all_words.append(str(word)) # 把已經清洗的列表存儲起來 contents_clean.append(line_clean) return contents_clean,all_words # contents_clean為清理完的數據,為二維列表 # 讀取語料數據 df_news = pd.read_table( " val.txt " ,names=[ ' category ' , ' theme ' , ' url ' , ' content ' ],encoding= ' utf-8 ' ) df_news = df_news.dropna() # 將數據轉為二維列表:list of list content = df_news.content.values.tolist() # 將每個content列轉為列表,結果為二維列表 # 讀取停用詞表 df_stop= pd.read_csv( " stop_word.txt " ,encoding= " utf-8 " ,sep = " \n " ,names = [ ' stopword ' ]) # 將數據轉為二維列表:list of list stopwords = df_stop.stopword.values.tolist() # 分詞,數據格式: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) # 清停用詞,數據格式:list of list contents_clean, all_words = drop_stopwords(content_res,stopwords) # 查看清洗后的數據 # df_content =pd.DataFrame({'content_res':contents_clean}) # print(df_content.head()) # 詞頻統計 # 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())
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
