Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix alllowPos logic in Textrank to be consistent with TF-IDF #494

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions jieba/analyse/textrank.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ def __init__(self):
self.pos_filt = frozenset(('ns', 'n', 'vn', 'v'))
self.span = 5

def pairfilter(self, wp):
return (wp.flag in self.pos_filt and len(wp.word.strip()) >= 2
and wp.word.lower() not in self.stop_words)
def pairfilter(self, wp, filt_pos=True):
if filt_pos == True:
return (wp.flag in self.pos_filt and len(wp.word.strip()) >= 2
and wp.word.lower() not in self.stop_words)
else:
return (len(wp.word.strip()) >= 2
and wp.word.lower() not in self.stop_words)

def textrank(self, sentence, topK=20, withWeight=False, allowPOS=('ns', 'n', 'vn', 'v'), withFlag=False):
"""
Expand All @@ -83,11 +87,12 @@ def textrank(self, sentence, topK=20, withWeight=False, allowPOS=('ns', 'n', 'vn
cm = defaultdict(int)
words = tuple(self.tokenizer.cut(sentence))
for i, wp in enumerate(words):
if self.pairfilter(wp):
filt_pos = True if self.pos_filt else False
if self.pairfilter(wp, filt_pos):
for j in xrange(i + 1, i + self.span):
if j >= len(words):
break
if not self.pairfilter(words[j]):
if not self.pairfilter(words[j], filt_pos):
continue
if allowPOS and withFlag:
cm[(wp, words[j])] += 1
Expand Down