如何在python中使用RAKE提取关键字

时间:2017-10-30 19:24:54

标签: python rake

我已经实现了这段代码,但它在屏幕上没有打印任何内容。

from rake_nltk import Rake

r = Rake() # Uses stopwords for english from NLTK, and all puntuation characters.

#r = Rake(english) # To use it in a specific language supported by nltk.

# If you want to provide your own set of stop words and punctuations to
# r = Rake(<list of stopwords>, <string of puntuations to ignore>)

print(r.extract_keywords_from_text('hello world'))

r.get_ranked_phrases() # To get keyword phrases ranked highest to lowest.

1 个答案:

答案 0 :(得分:1)

声明:print(r.extract_keywords_from_text(&#39; hello world&#39;))将打印None,因为它只提取关键字。您需要打印第二个代码行,如下所示: print(r.get_ranked_phrases())。以下内容可能有所帮助:

from rake_nltk import Rake
from nltk.corpus import stopwords 
r = Rake() # Uses stopwords for english from NLTK, and all puntuation characters.Please note that "hello" is not included in the list of stopwords.

text='Hello World'
a=r.extract_keywords_from_text(text)
b=r.get_ranked_phrases()
c=r.get_ranked_phrases_with_scores()
print(b)
print(c)

output:['hello world']
[(4.0, 'hello world')]

我建议您尝试使用包含多个字符串的文本。请参阅https://pypi.python.org/pypi/rake-nltk以获取更多详细信息。