-
Notifications
You must be signed in to change notification settings - Fork 2
/
rake_demo.py
55 lines (37 loc) · 1.9 KB
/
rake_demo.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
import tweepy
import re
import nltk
def start(r , auth ,keyword,max_items):
api = tweepy.API(auth)
para = ""
for tweet in tweepy.Cursor(api.search, q=keyword, count=100, lang='en', include_entities=False,
tweet_mode='extended').items(max_items):
temp = tweet._json.get('full_text')
if temp.startswith("RT"):
try:
temp = tweet._json.get('retweeted_status').get('full_text')
except:
temp = tweet._json.get('full_text')
else:
temp = tweet._json.get('full_text')
temp = temp.replace("RT ", "").replace("!", "").replace("..", "").replace("$", "").replace("%", "").replace("&",
"").replace(
"~", "").replace("-", "").replace("+", "").replace("#", "").replace("\\n", "").replace("\\", "").replace("|",
"")
temp = " ".join(filter(lambda x: x[0] != '@', temp.split()))
temp = re.sub(r'https\S+', "", temp)
temp = temp.strip()
para = para + temp
#print(para)
r.extract_keywords_from_text(para)
# r.get_ranked_phrases_with_scores()
ranked_phrases = r.get_ranked_phrases()
for i in range(0, len(ranked_phrases)):
ranked_phrases[i] = ranked_phrases[i].replace(",", "").replace("'", "").replace("(", "").replace(')', "").replace('.', "").replace('`', "").replace('!', "")
ranked_phrases[i] = re.sub(' +', ' ', ranked_phrases[i]).strip()
temp = ranked_phrases[:]
for i in range(0, len(ranked_phrases)):
t1 = ranked_phrases[i].split()
if len(t1) > 3:
temp.remove(ranked_phrases[i])
return temp[:10]