python实现textrank关键词提取

yipeiwu_com6年前Python基础

用python写了一个简单版本的textrank,实现提取关键词的功能。

import numpy as np 
import jieba 
import jieba.posseg as pseg 
 
class TextRank(object): 
   
  def __init__(self, sentence, window, alpha, iternum): 
    self.sentence = sentence 
    self.window = window 
    self.alpha = alpha 
    self.edge_dict = {} #记录节点的边连接字典 
    self.iternum = iternum#迭代次数 
 
  #对句子进行分词 
  def cutSentence(self): 
    jieba.load_userdict('user_dict.txt') 
    tag_filter = ['a','d','n','v'] 
    seg_result = pseg.cut(self.sentence) 
    self.word_list = [s.word for s in seg_result if s.flag in tag_filter] 
    print(self.word_list) 
 
  #根据窗口,构建每个节点的相邻节点,返回边的集合 
  def createNodes(self): 
    tmp_list = [] 
    word_list_len = len(self.word_list) 
    for index, word in enumerate(self.word_list): 
      if word not in self.edge_dict.keys(): 
        tmp_list.append(word) 
        tmp_set = set() 
        left = index - self.window + 1#窗口左边界 
        right = index + self.window#窗口右边界 
        if left < 0: left = 0 
        if right >= word_list_len: right = word_list_len 
        for i in range(left, right): 
          if i == index: 
            continue 
          tmp_set.add(self.word_list[i]) 
        self.edge_dict[word] = tmp_set 
 
  #根据边的相连关系,构建矩阵 
  def createMatrix(self): 
    self.matrix = np.zeros([len(set(self.word_list)), len(set(self.word_list))]) 
    self.word_index = {}#记录词的index 
    self.index_dict = {}#记录节点index对应的词 
 
    for i, v in enumerate(set(self.word_list)): 
      self.word_index[v] = i 
      self.index_dict[i] = v 
    for key in self.edge_dict.keys(): 
      for w in self.edge_dict[key]: 
        self.matrix[self.word_index[key]][self.word_index[w]] = 1 
        self.matrix[self.word_index[w]][self.word_index[key]] = 1 
    #归一化 
    for j in range(self.matrix.shape[1]): 
      sum = 0 
      for i in range(self.matrix.shape[0]): 
        sum += self.matrix[i][j] 
      for i in range(self.matrix.shape[0]): 
        self.matrix[i][j] /= sum 
 
  #根据textrank公式计算权重 
  def calPR(self): 
    self.PR = np.ones([len(set(self.word_list)), 1]) 
    for i in range(self.iternum): 
      self.PR = (1 - self.alpha) + self.alpha * np.dot(self.matrix, self.PR) 
 
  #输出词和相应的权重 
  def printResult(self): 
    word_pr = {} 
    for i in range(len(self.PR)): 
      word_pr[self.index_dict[i]] = self.PR[i][0] 
    res = sorted(word_pr.items(), key = lambda x : x[1], reverse=True) 
    print(res) 
 
if __name__ == '__main__': 
  s = '程序员(英文Programmer)是从事程序开发、维护的专业人员。一般将程序员分为程序设计人员和程序编码人员,但两者的界限并不非常清楚,特别是在中国。软件从业人员分为初级程序员、高级程序员、系统分析员和项目经理四大类。' 
  tr = TextRank(s, 3, 0.85, 700) 
  tr.cutSentence() 
  tr.createNodes() 
  tr.createMatrix() 
  tr.calPR() 
  tr.printResult() 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 初始化一个定长的数组实例

​# 有时候我们提前知道了一个数组的大小,需要给每个元素赋值,此时append好像不管用。我们需要定义一个定# # 长的数组, python中代码如下: b = [0 f...

对Python2与Python3中__bool__方法的差异详解

学习Python面向对象编程的时候,遇到了一个很有意思的小问题。Python的__bool__方法不起作用的问题。 我反复读了我手中的教程,确认了我写的代码应该管用。可是在测试的时候却一...

python中字符串变二维数组的实例讲解

python中字符串变二维数组的实例讲解

有一道算法题题目的意思是在二维数组里找到一个峰值。要求复杂度为n。 解题思路是找田字(四边和中间横竖两行)中最大值,用分治法递归下一个象限的田字。 在用python定义一个二维数组时可以...

Kali Linux安装ipython2 和 ipython3的方法

1、更新包管理 apt-get install update. 2、安装 pip3 :apt-get install python3-pip 3、安装ipython 2 : pip in...

numpy创建单位矩阵和对角矩阵的实例

在学习linear regression时经常处理的数据一般多是矩阵或者n维向量的数据形式,所以必须对矩阵有一定的认识基础。 numpy中创建单位矩阵借助identity()函数。更为准...