Skip to content

Commit

Permalink
modify readme
Browse files Browse the repository at this point in the history
  • Loading branch information
letiantian committed Dec 1, 2014
1 parent 3722b98 commit 0173ef2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 41 deletions.
46 changes: 12 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,13 @@ w1, w2, w3, w4, w5, ..., wn
基于上面构成图,可以计算出每个单词节点的重要性。最重要的若干单词可以作为关键词。


###关键词组提取
###关键短语提取
参照[关键词提取](#关键词提取)提取出若干关键词。若原文本中存在若干个关键词相邻的情况,那么这些关键词可以构成一个关键词组。

例如,在一篇介绍`支持向量机`的文章中,可以找到关键词`支持``向量```,通过关键词组提取,可以得到`支持向量机`

###摘要生成
将每个句子看成图中的一个节点,若两个句子之间有相似性,认为对应的两个节点之间有一个无向有权边,权值是相似性
将每个句子看成图中的一个节点,若两个句子之间有相似性,认为对应的两个节点之间有一个无向有权边,权值是相似度

通过pagerank算法计算得到的重要性最高的若干句子可以当作摘要。

Expand All @@ -105,10 +105,6 @@ w1, w2, w3, w4, w5, ..., wn
`test.py`提供了使用的示例:
```
#-*- encoding:utf-8 -*-
'''
Created on Dec 1, 2014
@author: letian
'''
import codecs
from textrank4zh import TextRank4Keyword, TextRank4Sentence
Expand All @@ -120,7 +116,7 @@ tr4w.train(text=text, speech_tag_filter=True, lower=True, window=2)
print '关键词:'
print '/'.join(tr4w.get_keywords(10, word_min_len=2))
print '关键词组:'
print '关键短语:'
print '/'.join(tr4w.get_keyphrases(keywords_num=20, min_occur_num= 2))
tr4s = TextRank4Sentence(stop_words_file='./stopword.data')
Expand All @@ -134,7 +130,7 @@ print '\n'.join(tr4s.get_key_sentences(num=3))
```
媒体/高圆圆/宾客/新人/记者/北京/赵又廷/谢娜/现身/答谢
```
没有关键词组
没有关键短语

得到的摘要:
```
Expand All @@ -147,10 +143,13 @@ print '\n'.join(tr4s.get_key_sentences(num=3))

类TextRank4Keyword、TextRank4Sentence在处理一段文本时会将文本拆分成4种格式:

sentences:由句子组成的列表。
words_no_filter:对sentences中每个句子分词而得到的两级列表。
words_no_stop_words:去掉words_no_filter中的停止词而得到的两级列表。
words_all_filters:保留words_no_stop_words中指定词性的单词而得到的两级列表。
**sentences:**由句子组成的列表。

**words_no_filter:**对sentences中每个句子分词而得到的两级列表。

**words_no_stop_words:**去掉words_no_filter中的停止词而得到的两级列表。

**words_all_filters:**保留words_no_stop_words中指定词性的单词而得到的两级列表。

例如,对于:
```
Expand Down Expand Up @@ -184,28 +183,7 @@ words_all_filters:
]
```

###class TextRank4Keyword
位于`textrank4zh/TextRank4Keyword.py`中,

**构造函数:**

`stop_words_file`:默认值为None,此时内部停止词表为空;可以设置为文件路径(字符串),将从停止词文件中提取停止词。

`delimiters`:默认值是`'?!;?!。;…\n'`,用来将文本拆分为句子。

**函数train(...):**

`text`:文本内容,字符串。

`window`:窗口大小,int,用来构造单词之间的边。默认值为2。

`lower`:是否将文本转换为小写。默认为False。

`speech_tag_filter`:若值为True,将调用内部的词性列表来过滤生成words_all_filters。若值为False,words_all_filters与words_no_stop_words相同。

`vertex_source`:选择使用words_no_filter, words_no_stop_words, words_all_filters中的哪一个来构造pagerank对应的图中的节点。默认值为`'all_filters'`,可选值为`'no_filter', 'no_stop_words', 'all_filters'`。关键词也来自`vertex_source`

`edge_source`:选择使用words_no_filter, words_no_stop_words, words_all_filters中的哪一个来构造pagerank对应的图中的节点之间的边。默认值为`'no_stop_words'`,可选值为`'no_filter', 'no_stop_words', 'all_filters'`。边的构造要结合`window`参数。
类TextRank4Keyword位于`textrank4zh/TextRank4Keyword.py`中,类TextRank4Sentence位于`textrank4zh/TextRank4Sentence.py`中,详情请参考源码注释。



Expand Down
2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
print '关键词:'
print '/'.join(tr4w.get_keywords(10, word_min_len=2))

print '关键词组:'
print '关键短语:'
print '/'.join(tr4w.get_keyphrases(keywords_num=20, min_occur_num= 2))

tr4s = TextRank4Sentence(stop_words_file='./stopword.data')
Expand Down
33 changes: 29 additions & 4 deletions textrank4zh/TextRank4Keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
class TextRank4Keyword(object):

def __init__(self, stop_words_file = None, delimiters = '?!;?!。;…\n'):
''' '''
'''
`stop_words_file`:默认值为None,此时内部停止词表为空;可以设置为文件路径(字符串),将从停止词文件中提取停止词。
`delimiters`:默认值是`'?!;?!。;…\n'`,用来将文本拆分为句子。
self.words_no_filter:对sentences中每个句子分词而得到的两级列表。
self.words_no_stop_words:去掉words_no_filter中的停止词而得到的两级列表。
self.words_all_filters:保留words_no_stop_words中指定词性的单词而得到的两级列表。
'''
self.text = ''
self.keywords = []

Expand All @@ -29,7 +36,15 @@ def train(self, text, window = 2, lower = False, speech_tag_filter=True,
vertex_source = 'all_filters',
edge_source = 'no_stop_words'):
'''
vertex_source, edge_source: no_filter, no_stop_words, all_filters这三个值
`text`:文本内容,字符串。
`window`:窗口大小,int,用来构造单词之间的边。默认值为2。
`lower`:是否将文本转换为小写。默认为False。
`speech_tag_filter`:若值为True,将调用内部的词性列表来过滤生成words_all_filters。
若值为False,words_all_filters与words_no_stop_words相同。
`vertex_source`:选择使用words_no_filter, words_no_stop_words, words_all_filters中的哪一个来构造pagerank对应的图中的节点。
默认值为`'all_filters'`,可选值为`'no_filter', 'no_stop_words', 'all_filters'`。关键词也来自`vertex_source`。
`edge_source`:选择使用words_no_filter, words_no_stop_words, words_all_filters中的哪一个来构造pagerank对应的图中的节点之间的边。
默认值为`'no_stop_words'`,可选值为`'no_filter', 'no_stop_words', 'all_filters'`。边的构造要结合`window`参数。
'''

self.text = text
Expand Down Expand Up @@ -95,7 +110,11 @@ def train(self, text, window = 2, lower = False, speech_tag_filter=True,


def combine(self, word_list, window = 2):
''' '''
'''
构造在window下的单词组合,用来构造单词之间的边。使用了生成器。
word_list: 由单词组成的列表。
windows:窗口大小。
'''
window = int(window)
if window < 2: window = 2
for x in xrange(1, window):
Expand All @@ -108,6 +127,8 @@ def combine(self, word_list, window = 2):

def get_keywords(self, num = 6, word_min_len = 1):
'''
获取最重要的num个长度大于等于word_min_len的关键词。
返回关键词列表。
'''
result = []
count = 0
Expand All @@ -120,7 +141,11 @@ def get_keywords(self, num = 6, word_min_len = 1):
return result

def get_keyphrases(self, keywords_num = 12, min_occur_num = 2):
''' 获取关键词组 '''
'''
获取关键短语。
获取 keywords_num 个关键词构造在可能出现的短语,要求这个短语在原文本中至少出现的次数为min_occur_num。
返回关键短语的列表。
'''
keywords_set = set(self.get_keywords(num=keywords_num, word_min_len = 1))

keyphrases = set()
Expand Down
24 changes: 22 additions & 2 deletions textrank4zh/TextRank4Sentence.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@
class TextRank4Sentence(object):

def __init__(self, stop_words_file = None, delimiters='?!;?!。;…\n'):
'''
`stop_words_file`:默认值为None,此时内部停止词表为空;可以设置为文件路径(字符串),将从停止词文件中提取停止词。
`delimiters`:默认值是`'?!;?!。;…\n'`,用来将文本拆分为句子。
self.sentences:由句子组成的列表。
self.words_no_filter:对sentences中每个句子分词而得到的两级列表。
self.words_no_stop_words:去掉words_no_filter中的停止词而得到的两级列表。
self.words_all_filters:保留words_no_stop_words中指定词性的单词而得到的两级列表。
'''
self.seg = Segmentation(stop_words_file=stop_words_file, delimiters=delimiters)

self.sentences = None
Expand All @@ -25,7 +33,13 @@ def __init__(self, stop_words_file = None, delimiters='?!;?!。;…\n'):
def train(self, text, lower = False, speech_tag_filter=True,
source = 'no_stop_words', sim_func = 'standard'):
'''
source: no_filter, no_stop_words, all_filters这三个值
`text`:文本内容,字符串。
`lower`:是否将文本转换为小写。默认为False。
`speech_tag_filter`:若值为True,将调用内部的词性列表来过滤生成words_all_filters。
若值为False,words_all_filters与words_no_stop_words相同。
`source`:选择使用words_no_filter, words_no_stop_words, words_all_filters中的哪一个来生成句子之间的相似度。
默认值为`'all_filters'`,可选值为`'no_filter', 'no_stop_words', 'all_filters'`。
`sim_func`: 指定计算句子相似度的函数。当前只有一个函数,对应默认值`standard`。
'''

self.key_sentences = []
Expand Down Expand Up @@ -77,6 +91,7 @@ def train(self, text, lower = False, speech_tag_filter=True,

def _get_similarity_standard(self, word_list1, word_list2):
'''
默认的用于计算两个句子相似度的函数。
word_list1, word_list2: 分别代表两个句子,都是由单词组成的列表
'''
vector1, vector2 =self._gen_vectors(word_list1, word_list2)
Expand All @@ -101,14 +116,19 @@ def _get_similarity_standard(self, word_list1, word_list2):


def _gen_vectors(self, word_list1, word_list2):
''' '''
'''
两个句子转换成两个同样大小向量。可以通过这两个向量来计算两个句子的相似度。
word_list1, word_list2: 分别代表两个句子,都是由单词组成的列表
'''
words = list(set(word_list1 + word_list2))
vector1 = [float(word_list1.count(word)) for word in words]
vector2 = [float(word_list2.count(word)) for word in words]
return vector1, vector2

def get_key_sentences(self, num = 6, sentence_min_len = 6):
'''
获取最重要的num个长度大于等于sentence_min_len的句子用来生成摘要。
返回列表。
'''
result = []
count = 0
Expand Down

0 comments on commit 0173ef2

Please sign in to comment.