-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankText.py
45 lines (37 loc) · 1.12 KB
/
rankText.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
# -*- coding: utf-8 -*-
#
# TELEGRAM BOT
#
# Name: Nova
# Username: Nova_X1_Bot
#
# - Command: /rankText
# - Description:
# Extract a relevancy-ranked list of topic keywords from within a text.
#
# By Jose Acevedo
# Copyright 2016.
import json
import unirest
# Extract keywords from the given message
def handleRankText(msg, chatID, bot):
if "reply_to_message" in msg:
text = msg["reply_to_message"]["text"]
else:
try:
text = msg["text"].split(' ',1)[1]
except:
text = ""
formattedText = ""
for i in range(len(text)):
if text[i] == ' ':
formattedText += '+'
else:
formattedText += text[i]
response = unirest.get("https://alchemy.p.mashape.com/text/TextGetRankedKeywords?outputMode=json&text="+formattedText,headers={"X-Mashape-Key":"DuskHHYl5DmshhsjvJ4LaVXZinfpp1KnC92jsndIqrz6pC0CDa","Accept": "application/json"})
response = response.body
message = text + "\n\n" + "------Ranked Keywords------\n"
for i in range(len(response["keywords"])):
message += "- " + response["keywords"][i]["text"] + " = " + \
response["keywords"][i]["relevance"] + ".\n"
bot.sendMessage(chatID, message)