前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布

Gensim

作者头像
裴来凡
发布2022-05-29 10:39:30
2890
发布2022-05-29 10:39:30
举报
代码语言:javascript
复制
import logging
import jieba
from gensim import corpora, models, similarities
logging.basicConfig(level=logging.DEBUG)
jieba.setLogLevel(logging.INFO)
class DocumentSimilar(object):
    def __init__(self, documents):
        self.documents=documents
        self.dictionary=None
        self.tfidf=None
        self.similar_matrix=None
        self.calculate_similar_matrix()
    @staticmethod
    def split_word(document):
        """
        分词,去除停用词
        """
        stop_words={":", "的", ",", "”"}
        text=[]
        for word in jieba.cut(document):
            if word not in stop_words:
                text.append(word)
        logging.debug(text)
        return text
    def calculate_similar_matrix(self):
        """
        计算相似度矩阵及相关必要数据
        """
        words=[self.split_word(document) for document in self.documents]

        self.dictionary=corpora.Dictionary(words)
        corpus=[self.dictionary.doc2bow(word) for word in words]
        self.tfidf=models.TfidfModel(corpus)
        corpus_tfidf=self.tfidf[corpus]
        self.similar_matrix=similarities.MatrixSimilarity(corpus_tfidf)

    def get_similar(self, document):
        """
        计算比较的文档与语料库中每篇文档的相似度
        """
        words=self.split_word(document)
        corpus=self.dictionary.doc2bow(words)
        corpus_tfidf=self.tfidf[corpus]
        return self.similar_matrix[corpus_tfidf]


if __name__=='__main__':

    documents=[
        "本公众号主要关注图像处理与模式识别的前沿进展",
        "经典书籍与最新文献研究成果,同时也包含计算机相关实用操作技能",
    ]
    doc_similar=DocumentSimilar(documents)
    #比较的文档
    new_doc="图像处理与模式识别研究所"

    for value, document in zip(doc_similar.get_similar(new_doc),documents):
        print("{:.2f}".format(value),document)

0.47 本公众号主要关注图像处理与模式识别的前沿进展 0.00 经典书籍与最新文献研究成果,同时也包含计算机相关实用操作技能

算法:Gensim是处理非结构化的数值化文本(纯文本)。

链接:https://github.com/RaRe-Technologies/gensim

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-05-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 图像处理与模式识别研究所 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
图像处理
图像处理基于腾讯云深度学习等人工智能技术,提供综合性的图像优化处理服务,包括图像质量评估、图像清晰度增强、图像智能裁剪等。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档