LDA算法進行相似性分析

import gensim
from gensim import corpora
from gensim.models import LdaModel
from gensim.matutils import cossim
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import string# 如果您尚未下載nltk的停用詞列表,請取消下面的注釋并運行一次
# nltk.download('punkt')
# nltk.download('stopwords')# 數據預處理函數
def preprocess(text):stop_words = set(stopwords.words('english'))tokens = word_tokenize(text.lower())tokens = [word for word in tokens if word.isalpha()]  # 僅保留字母tokens = [word for word in tokens if word not in stop_words]  # 去除停用詞return tokens# 示例文檔
documents = ["Text processing using LDA is interesting.","Another document example for LDA.","Text mining and natural language processing.","LDA helps in topic modeling and finding patterns.","This document is for testing LDA similarity."
]# 數據預處理
texts = [preprocess(doc) for doc in documents]# 創建詞典
dictionary = corpora.Dictionary(texts)# 轉換為詞袋模型
corpus = [dictionary.doc2bow(text) for text in texts]# 訓練LDA模型
num_topics = 2
lda_model = LdaModel(corpus, num_topics=num_topics, id2word=dictionary, passes=15)# 對新文檔進行主題分布提取
new_doc = "New text for testing similarity with LDA."
new_doc_preprocessed = preprocess(new_doc)
new_doc_bow = dictionary.doc2bow(new_doc_preprocessed)
new_doc_topics = lda_model.get_document_topics(new_doc_bow)# 獲取原始文檔的主題分布
doc_topics = [lda_model.get_document_topics(doc_bow) for doc_bow in corpus]# 計算新文檔與每個原始文檔的相似性
similarities = []
for i, doc_topic in enumerate(doc_topics):similarity = cossim(new_doc_topics, doc_topic)similarities.append((i, similarity))# 輸出相似性結果
print("Similarity between new document and each original document:")
for i, similarity in similarities:print(f"Document {i}: Similarity = {similarity}")

import gensim
from gensim import corpora
from gensim.models import LdaModel
from gensim.matutils import cossim
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import string

# 如果您尚未下載nltk的停用詞列表,請取消下面的注釋并運行一次
# nltk.download('punkt')
# nltk.download('stopwords')

# 數據預處理函數
def preprocess(text):
? ? stop_words = set(stopwords.words('english'))
? ? tokens = word_tokenize(text.lower())
? ? tokens = [word for word in tokens if word.isalpha()] ?# 僅保留字母
? ? tokens = [word for word in tokens if word not in stop_words] ?# 去除停用詞
? ? return tokens

# 示例文檔
documents = [
? ? "Text processing using LDA is interesting.",
? ? "Another document example for LDA.",
? ? "Text mining and natural language processing.",
? ? "LDA helps in topic modeling and finding patterns.",
? ? "This document is for testing LDA similarity."
]

# 數據預處理
texts = [preprocess(doc) for doc in documents]

# 創建詞典
dictionary = corpora.Dictionary(texts)

# 轉換為詞袋模型
corpus = [dictionary.doc2bow(text) for text in texts]

# 訓練LDA模型
num_topics = 2
lda_model = LdaModel(corpus, num_topics=num_topics, id2word=dictionary, passes=15)

# 對新文檔進行主題分布提取
new_doc = "New text for testing similarity with LDA."
new_doc_preprocessed = preprocess(new_doc)
new_doc_bow = dictionary.doc2bow(new_doc_preprocessed)
new_doc_topics = lda_model.get_document_topics(new_doc_bow)

# 獲取原始文檔的主題分布
doc_topics = [lda_model.get_document_topics(doc_bow) for doc_bow in corpus]

# 計算新文檔與每個原始文檔的相似性
similarities = []
for i, doc_topic in enumerate(doc_topics):
? ? similarity = cossim(new_doc_topics, doc_topic)
? ? similarities.append((i, similarity))

# 輸出相似性結果
print("Similarity between new document and each original document:")
for i, similarity in similarities:
? ? print(f"Document {i}: Similarity = {similarity}")
?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/42445.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/42445.shtml
英文地址,請注明出處:http://en.pswp.cn/web/42445.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

使用sklearn的基本流程

scikit-learn,通常簡稱為 sklearn,是一個開源的Python庫,是基于 Python 編程語言的一個非常流行的機器學習庫。它建立在 NumPy 和 SciPy 這兩個科學計算庫之上,并與 Matplotlib 配合使用,為數據預處理、模型訓練、評估…

Atom編輯器插件Minimap使用樣例

Atom編輯器插件Minimap是一個強大的工具,它為開發者提供了快速、直觀的代碼預覽功能。以下是Minimap的使用樣例和一些關鍵特性的介紹: 1. 安裝Minimap 首先,你需要在Atom編輯器中安裝Minimap插件。這通常可以通過Atom的插件市場&#xff08…

vue緩存頁面,當tab切換時保留原有的查詢條件

需求: 切換tab時,查詢條件不變 路由頁面: 單個頁面上加這句話:

bert-base-chinese模型離線使用案例

import torch import torch.nn as nn from transformers import BertModel, BertTokenizer# 通過torch.hub(pytorch中專注于遷移學的工具)獲得已經訓練好的bert-base-chinese模型 # model torch.hub.load(huggingface/pytorch-transformers, model, bert-base-chinese) model…

超過35歲的網工,你該何去何從?

在網絡工程師這個職業中,35歲往往被視為一個重要的門檻,這個年齡段的工程師,既擁有豐富的經驗和技能,也面臨著職業發展的諸多挑戰。 隨著技術的飛速發展和年輕一代的不斷涌入,不少35歲以上的網工都在迷茫的路口&#x…

雅思詞匯7~9

生活類詞匯 英文中文fridge冰箱washing machine洗衣機dishwasher洗碗機water heater熱水器microwave oven微波驢stereo system音箱radiator電暖爐toaster烤面包機dryer烘干機air conditioner空調accmodation住宿,膳宿tenant租客landlord房東couple夫婦veterinarian…

【資源下載】《數據倉庫工具箱》

歡迎來到我的博客,很高興能夠在這里和您見面!歡迎訂閱相關專欄: 歡迎關注微信公眾號:野老雜談 ?? 全網最全IT互聯網公司面試寶典:收集整理全網各大IT互聯網公司技術、項目、HR面試真題. ?? AIGC時代的創新與未來&a…

SpringBoot報錯:The field file exceeds its maximum permitted size of 1048576 bytes

報錯信息 The field file exceeds its maximum permitted size of 1048576 bytes原因是 SpringBoot內嵌的 tomcat 默認的所有上傳的文件大小為 1MB 解決辦法 修改配置 spring:servlet:multipart:max-file-size: 50MBmax-request-size: 50MB或者 spring.servlet.multipart.…

拖地機檢測液位的原理-管道液位傳感器

在現代洗地機中,確保水箱液位充足是保證清潔效率和質量的關鍵之一。為了實現這一功能,洗地機通常配備了管道光電液位傳感器,這種傳感器利用先進的光學感應原理來準確檢測水箱中的液位情況。 管道光電液位傳感器的工作原理基于光學傳感技術&a…

VOS歷史話單的非法呼叫話單解決方案,IPSS模塊安裝到VOS服務器,可大幅度提高安全性!

由于VOS的普及性,不得不承認VOS確實是非常優秀的軟交換,但是很多客戶在使用過程中都會遇到各種安全問題,比如話費被盜用了,歷史話單一堆的非法呼叫話單,嚴重的影響到了話務安全,并不是那點話費的事了&#…

Flutter【組件】標簽

簡介 flutter 標簽組件。標簽組件是一種常見的 UI 元素,用于顯示和管理多個標簽(或標簽集合)。 github地址: https://github.com/ThinkerJack/jac_uikit pub地址:https://pub.dev/packages/jac_uikit 使用方式&…

中科馭數HADOS 3.0:以四大架構革新,全面擁抱敏捷開發理念,引領DPU應用生態

一家成功的大算力芯片公司,其核心壁壘必須是軟硬兼備的,既要有芯片架構的強大技術壁壘,更要有持久投入的、生態兼容完備的軟件護城河。HADOS,就是中科馭數的軟件護城河。 ——中科馭數高級副總裁 張宇 近日,在DPU基礎…

設計模式使用場景實現示例及優缺點(創建型模式——工廠方法模式、抽象工廠模式)

創建型模式 工廠方法模式 (Factory Method Pattern) 工廠方法模式(Factory Method Pattern)是一種創建型設計模式,提供了一個創建對象的接口,但由子類決定要實例化的類是哪一個。工廠方法模式讓類的實例化推遲到子類。這樣&…

關于MySQL mvcc

innodb mvcc mvcc 多版本并發控制 在RR isolution 情況下 trx在啟動的時候就拍了個快照。這個快照是基于整個數據庫的。 其實這個快照并不是說拷貝整個數據庫。并不是說要拷貝出這100個G的數據。 innodb里面每個trx有一個唯一的trxID 叫做trx id .在trx 開始的時候向innodb系…

嵌入式音頻處理技術的現在發展及未來的方向

嵌入式音頻處理技術:從音頻流媒體到聲音識別 嵌入式音頻處理技術的迅猛發展正在改變我們的生活方式,從音頻流媒體到聲音識別,這個領域為人們的生活和工作帶來了巨大的影響。本文將探討嵌入式音頻處理技術的最新趨勢和應用,以及提…

邏輯回歸模型(非回歸問題,而是解決二分類問題)

目錄: 一、Sigmoid激活函數:二、邏輯回歸介紹:三、決策邊界四、邏輯回歸模型訓練過程:1.訓練目標:2.梯度下降調整參數: 一、Sigmoid激活函數: Sigmoid函數是構建邏輯回歸模型的重要激活函數&am…

openlayers更改點坐標

我現在的需求是無人機點位根據ws傳輸的經緯度改變位置,在網上查了很多資料,終于是做出來了,如果有問題請指出。 效果圖,無人機可以來回移動 這里是核心代碼 // 添加飛機點位圖層let vectorLayerpointfunction DronepointLayer()…

Mercer 條件的基本概念及證明

Mercer 條件 是核函數理論中的一個重要概念,它確保了一個給定的對稱函數可以表示為某個高維特征空間中的內積。這個條件在支持向量機(SVM)和其他基于核方法的機器學習算法中非常重要。 文章目錄 基本介紹Mercer 條件的定義Mercer 定理實際應用…

Kafka(三)Producer第二篇

一,生產者架構 生產者客戶端由兩個線程協調運行,分別為主線程和Sender線程(發送線程)。 主線程:KafkaProducer創建消息,通過攔截器、序列化器和分區器之后緩存到消息收集器RecordAccumulator中;…

Redis 中的跳躍表(Skiplist)基本介紹

Redis 中的跳躍表(Skiplist)是一種用于有序元素集合的快速查找數據結構。它通過一個多級索引來提高搜索效率,能夠在對數時間復雜度內完成查找、插入和刪除操作。跳躍表特別適用于實現有序集合(sorted set)的功能&#…