当前位置:   article > 正文

textrank算法提取文本摘要_textrank 摘要

textrank 摘要

TextRank算法的基本思想是将文档看作一个词的网络(一种基于图的排序方法,用于提取相关句子或查找关键字),该网络中的链接表示词与词之间的语义关系。

TextRank算法主要包括:关键词抽取、关键短语抽取、关键句抽取。

(1)关键词抽取(keyword extraction)

关键词抽取是指从文本中确定一些能够描述文档含义的术语的过程。对关键词抽取而言,用于构建顶点集的文本单元可以是句子中的一个或多个字;根据这些字之间的关系(比如:在一个框中同时出现)构建边。根据任务的需要,可以使用语法过滤器(syntactic filters)对顶点集进行优化。语法过滤器的主要作用是将某一类或者某几类词性的字过滤出来作为顶点集。

(2)关键短语抽取(keyphrase extration)

关键词抽取结束后,我们可以得到的N个关键词,在原始文本中相邻的关键词构成关键短语。因此,从get_keyphrases函数的源码中我们可以看到,它先调用get_keywords抽取关键词,然后分析关键词是否存在相邻的情况,最后确定哪些是关键短语。

(3)关键句抽取(sentence extraction)

句子抽取任务主要针对的是自动摘要这个场景,将每一个sentence作为一个顶点,根据两个句子之间的内容重复程度来计算他们之间的“相似度”,以这个相似度作为联系,由于不同句子之间相似度大小不一致,在这个场景下构建的是以相似度大小作为edge权重的有权图。

这是利用TextRank提取的一些摘要

 效果很差:

  1. from snownlp import SnowNLP
  2. import re
  3. import jieba.analyse
  4. import jieba.posseg
  5. import os
  6. import json
  7. import re
  8. import jieba.analyse
  9. import jieba.posseg
  10. class TextSummary:
  11. def __init__(self, text):
  12. self.text = text
  13. def splitSentence(self):
  14. sectionNum = 0
  15. self.sentences = []
  16. for eveSection in self.text.split("\n"):
  17. if eveSection:
  18. sentenceNum = 0
  19. for eveSentence in re.split("!|。|?", eveSection):
  20. if eveSentence:
  21. mark = []
  22. if sectionNum == 0:
  23. mark.append("FIRSTSECTION")
  24. if sentenceNum == 0:
  25. mark.append("FIRSTSENTENCE")
  26. self.sentences.append({
  27. "text": eveSentence,
  28. "pos": {
  29. "x": sectionNum,
  30. "y": sentenceNum,
  31. "mark": mark
  32. }
  33. })
  34. sentenceNum = sentenceNum + 1
  35. sectionNum = sectionNum + 1
  36. self.sentences[-1]["pos"]["mark"].append("LASTSENTENCE")
  37. for i in range(0, len(self.sentences)):
  38. if self.sentences[i]["pos"]["x"] == self.sentences[-1]["pos"]["x"]:
  39. self.sentences[i]["pos"]["mark"].append("LASTSECTION")
  40. def getKeywords(self):
  41. self.keywords = jieba.analyse.extract_tags(self.text, topK=100, withWeight=False, allowPOS=('n', 'vn', 'v'))
  42. def sentenceWeight(self):
  43. # 计算句子的位置权重
  44. for sentence in self.sentences:
  45. mark = sentence["pos"]["mark"]
  46. weightPos = 0
  47. if "FIRSTSECTION" in mark:
  48. weightPos = weightPos + 2
  49. if "FIRSTSENTENCE" in mark:
  50. weightPos = weightPos + 2
  51. if "LASTSENTENCE" in mark:
  52. weightPos = weightPos + 1
  53. if "LASTSECTION" in mark:
  54. weightPos = weightPos + 1
  55. sentence["weightPos"] = weightPos
  56. # 计算句子的线索词权重
  57. index = [" 总之 ", " 总而言之 "]
  58. for sentence in self.sentences:
  59. sentence["weightCueWords"] = 0
  60. sentence["weightKeywords"] = 0
  61. for i in index:
  62. for sentence in self.sentences:
  63. if sentence["text"].find(i) >= 0:
  64. sentence["weightCueWords"] = 1
  65. for keyword in self.keywords:
  66. for sentence in self.sentences:
  67. if sentence["text"].find(keyword) >= 0:
  68. sentence["weightKeywords"] = sentence["weightKeywords"] + 1
  69. for sentence in self.sentences:
  70. sentence["weight"] = sentence["weightPos"] + 2 * sentence["weightCueWords"] + sentence["weightKeywords"]
  71. def getSummary(self, ratio=0.1):
  72. self.keywords = list()
  73. self.sentences = list()
  74. self.summary = list()
  75. # 调用方法,分别计算关键词、分句,计算权重
  76. self.getKeywords()
  77. self.splitSentence()
  78. self.sentenceWeight()
  79. # 对句子的权重值进行排序
  80. self.sentences = sorted(self.sentences, key=lambda k: k['weight'], reverse=True)
  81. length = 0
  82. # 根据排序结果,取排名占前 ratio% 的句子作为摘要
  83. for i in range(len(self.sentences)):
  84. if length<3:
  85. if i < ratio * len(self.sentences):
  86. sentence = self.sentences[i]
  87. self.summary.append(sentence["text"])
  88. length+=1
  89. return self.summary
  90. def get_abstract(text):
  91. testSummary = TextSummary(text)
  92. abstract_content = ".".join(testSummary.getSummary())
  93. return abstract_content
  94. if __name__ == '__main__':
  95. sample = ''
  96. abstract_content = get_abstract(sample)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/281686
推荐阅读
相关标签
  

闽ICP备14008679号