简体   繁体   中英

how to extract keywords using RAKE in python

I have implemented this code but it prints nothing on screen.

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.

The statement: print(r.extract_keywords_from_text('hello world')) will print None as it only extracts the keywords.You need to print the 2nd code line as below: print(r.get_ranked_phrases()).The following may help:

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')]

I would suggest to also try this with a text that contains more than one string.Refer https://pypi.python.org/pypi/rake-nltk for more detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM