2021SC@SDUSC
关键词提取器

class KeywordExtractor(object):STOP_WORDS = set(("the", "of", "is", "and", "to", "in", "that", "we", "for", "an", "are","by", "be", "as", "on", "with", "can", "if", "from", "which", "you", "it","this", "then", "at", "have", "all", "not", "one", "has", "or", "that"))def set_stop_words(self, stop_words_path):abs_path = _get_abs_path(stop_words_path)if not os.path.isfile(abs_path):raise Exception("jieba: file does not exist: " + abs_path)content = open(abs_path, 'rb').read().decode('utf-8')for line in content.splitlines():self.stop_words.add(line)def extract_tags(self, *args, **kwargs):raise NotImplementedError

set_stop_words方法中将"the", “of”, “is”, “and”, “to”, “in”, “that”, “we”, “for”, “an”, “are”,
“by”, “be”, “as”, “on”, “with”, “can”, “if”, “from”, “which”, “you”, “it”,
“this”, “then”, “at”, “have”, “all”, “not”, “one”, “has”, “or”, “that”
这些无意义的副词提取,因为这样的token,我们对今后的token分析没有贡献。读入stopwords.txt以删除这些token。也就是说分词的过程不变,打印时做个集合差运算,另外一个方法是使用extract_tags函数,这个函数会根据TF-IDF算法将特征词提取出来,在提取之前会去掉停用词,可以人工指定停用词字典.

class IDFLoader(object):

def __init__(self, idf_path=None):self.path = ""self.idf_freq = {}self.median_idf = 0.0if idf_path:self.set_new_path(idf_path)def set_new_path(self, new_idf_path):if self.path != new_idf_path:self.path = new_idf_pathcontent = open(new_idf_path, 'rb').read().decode('utf-8')self.idf_freq = {}for line in content.splitlines():word, freq = line.strip().split(' ')self.idf_freq[word] = float(freq)self.median_idf = sorted(self.idf_freq.values())[len(self.idf_freq) // 2]def get_idf(self):return self.idf_freq, self.median_idf

词频 (term frequency, TF) 指的是某一个给定的词语在该文件中出现的次数。这个数字通常会被归一化(一般是词频除以文章总词数), 以防止它偏向长的文件。IDF的主要思想是:如果包含词条t的文档越少, IDF越大,则说明词条具有很好的类别区分能力。某一特定词语的IDF,可以由总文件数目除以包含该词语之文件的数目,再将得到的商取对数得到。
解析 idf.txt,拿到词与idf的对应值,建立一个字典key = word,value = idf
extract_tags:
使用TF-IDF算法从句子中提取关键词。返回多少个关键字。None表示所有可能的单词。数量:如果为真,返回(单词、数量)列表;
如果为False,则返回一个单词列表。-允许的POS列表。如“ns”、“n”、“vn”、“v”、“nr”]。


class TFIDF(KeywordExtractor):def __init__(self, idf_path=None):self.tokenizer = jieba.dtself.postokenizer = jieba.posseg.dtself.stop_words = self.STOP_WORDS.copy()self.idf_loader = IDFLoader(idf_path or DEFAULT_IDF)self.idf_freq, self.median_idf = self.idf_loader.get_idf()def set_idf_path(self, idf_path):new_abs_path = _get_abs_path(idf_path)if not os.path.isfile(new_abs_path):raise Exception("jieba: file does not exist: " + new_abs_path)self.idf_loader.set_new_path(new_abs_path)self.idf_freq, self.median_idf = self.idf_loader.get_idf()def extract_tags(self, sentence, topK=20, withWeight=False, allowPOS=(), withFlag=False):"""Extract keywords from sentence using TF-IDF algorithm.Parameter:- topK: return how many top keywords. `None` for all possible words.- withWeight: if True, return a list of (word, weight);if False, return a list of words.- allowPOS: the allowed POS list eg. ['ns', 'n', 'vn', 'v','nr'].if the POS of w is not in this list,it will be filtered.- withFlag: only work with allowPOS is not empty.if True, return a list of pair(word, weight) like posseg.cutif False, return a list of words"""if allowPOS:allowPOS = frozenset(allowPOS)words = self.postokenizer.cut(sentence)else:words = self.tokenizer.cut(sentence)freq = {}for w in words:if allowPOS:if w.flag not in allowPOS:continueelif not withFlag:w = w.wordwc = w.word if allowPOS and withFlag else wif len(wc.strip()) < 2 or wc.lower() in self.stop_words:continuefreq[w] = freq.get(w, 0.0) + 1.0total = sum(freq.values())for k in freq:kw = k.word if allowPOS and withFlag else kfreq[k] *= self.idf_freq.get(kw, self.median_idf) / totalif withWeight:tags = sorted(freq.items(), key=itemgetter(1), reverse=True)else:tags = sorted(freq, key=freq.__getitem__, reverse=True)if topK:return tags[:topK]else:return tags

jieba分词TFIDF算法2相关推荐

  1. jieba分词textrank算法

    2021SC@SDUSC TextRank是一种用以关键词提取的算法,因为是基于PageRank的,所以先介绍PageRank. PageRank通过互联网中的超链接关系确定一个网页的排名,其公式是通 ...

  2. 自然语言处理之jieba分词

    在处理英文文本时,由于英文文本天生自带分词效果,可以直接通过词之间的空格来分词(但是有些人名.地名等需要考虑作为一个整体,比如New York).而对于中文还有其他类似形式的语言,我们需要根据来特殊处 ...

  3. 3.TF-IDF算法介绍、应用、NLTK实现TF-IDF算法、Sklearn实现TF-IDF算法、算法的不足、算法改进

    3.TF-IDF 3.1.TF-IDF算法介绍 3.2.TF-IDF应用 3.3.NLTK实现TF-IDF算法 3.4.Sklearn实现TF-IDF算法 3.5.Jieba实现TF-IDF算法 3. ...

  4. 用通俗易懂的方式讲解:TF-IDF算法介绍及实现

    文章目录 1.TF-IDF算法介绍 (1)TF是词频(Term Frequency) (2) IDF是逆向文件频率(Inverse Document Frequency) (3)TF-IDF实际上是: ...

  5. TF-IDF算法实现

    Python实现TF-IDF算法 # -*- coding: utf-8 -*- from collections import defaultdict import math import oper ...

  6. 自然语言处理之中文文本分析(jieba分词、词袋doc2bow、TFIDF文本挖掘)

    中文分词常用的分词工具有jieba等,本文以jieba分词为例,讲解中文文本分析. 一.jieba分词 来源github:https://github.com/fxsjy/jieba 1.主要模式 支 ...

  7. jieba分词算法总结

    jieba分词算法总结 特点: 支持三种分词模式 –精确模式,试图将句子最精确地切开,适合文本分析; –全模式,把句子中所有的可以成词的词语都扫描出来,速度非常快,但不能解决歧义; –搜索引擎模式,在 ...

  8. jieba分词流程及算法学习

    目录 jieba 特点 算法 jieba分词流程图 Trie 树 建立 DAG 词图 分词 DAG 代码实现 计算全局概率Route ,基于词频最大切分组合 隐马尔可夫HMM 算法 引用 jieba ...

  9. Hanlp分词实例:Java实现TFIDF算法

    2019独角兽企业重金招聘Python工程师标准>>> 算法介绍 最近要做领域概念的提取,TFIDF作为一个很经典的算法可以作为其中的一步处理. 关于TFIDF算法的介绍可以参考这篇 ...

最新文章

  1. 河北省电子工程高级职称公示_2019年河北省电子工程职称评审,中级职称已经出结果了!...
  2. python mapreduce函数_Map-reduce在Python高阶函数中的应用,python,用法,之,mapreduce
  3. 如何循序渐进有效学习 JavaScript?
  4. 企业实战案例01_Jenkins_连接远程执行shell脚本
  5. C++之指针探究(六):二级指针和指针数组
  6. 教你成为全栈工程师(Full Stack Developer) 一-各显神通总结八大类编程语言的区别...
  7. 003自动装配歧义性解决
  8. [IOS Tableview] cell自定义view显示错误问题
  9. 07网络发展趋势:风险和机遇并存
  10. ASP.NET 页面传值方法的一些事情儿。
  11. [HihoCoder1369]网络流一·Ford-Fulkerson算法
  12. JAVA 设计模式 装饰者模式
  13. C# .NET 如何修改代码字体
  14. [C语言]切比雪夫多项式,并写入到文件中
  15. 理解OSEK NM原理,看完这个就够了
  16. Coded UI- Run Coded UI in WinForm
  17. 使用 JavaScript 将 JSON 数据动态转换为 HTML 表
  18. Camera ITS当中的test_lens_shading_and_color_uniformity测试
  19. vela和鸿蒙,小米Vela系统发布,将对标华为鸿蒙OS
  20. 基于C#通过PLCSIM ADV仿真软件实现与西门子1500PLC的S7通信方法演示

热门文章

  1. 如何提高自己的知识水平?
  2. Web3域名,热潮还是泡沫?
  3. 新视野大学英语听说教程4(第二版)答案
  4. KVM镜像管理利器-guestfish使用详解
  5. SD 协议与协议栈源码分析(SD 内存卡)
  6. leaflet 实现左卷帘效果 (代码示例045)
  7. Mysql使用函数json_extract处理Json类型数据
  8. 更改ip地址的软件多少钱一个月_武汉社保代缴多少钱一个月?武汉社保一个月交多少钱?...
  9. 安徽科技学院 信网学院网络文化节 曹健
  10. 用java语言编写程序计算九宫图