-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextrank_graph_model.py
68 lines (44 loc) · 1.19 KB
/
textrank_graph_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# coding: utf-8
# In[ ]:
from gensim.summarization.summarizer import summarize
#import pytextrank
#import spacy
# In[ ]:
def gensim_summarize(text, ratio = 0.2,word_count=None):
"""
Input: a paragraph as text string, proportion of sentences as summary, or limit words as summary
Output a summary as text string
"""
return summarize(text, ratio, word_count)
# In[ ]:
def pytextrank_rank(text):
"""
Input: a paragraph as text string
Process: spacy pipeline including pytextrank
Output: a spacy nlp doc
"""
tr = pytextrank.TextRank()
nlp = spacy.load("en")
nlp.add_pipe(tr.PipelineComponent, name="textrank", last=True)
doc = nlp(text)
return doc
# In[ ]:
def pytextrank_get_rank(doc):
"""
return the rank table of processed text
"""
rank = {}
for p in doc._.phrases:
rank[p] = [p.rank,p.count]
return rank
# In[ ]:
def pytextrank_get_summary(doc, n=2):
"""
return the full sentence of the rank n key words
"""
summary = ""
for p in doc._.phrases[0:2]:
for s in doc.sents:
if p.text in s.text:
summary += ''.join(s.text)
return summary