Skip to content

Instantly share code, notes, and snippets.

@alaakh42
Last active October 3, 2018 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alaakh42/4dc4fbeb2af4adecc7de1221d4d1b7f0 to your computer and use it in GitHub Desktop.
Save alaakh42/4dc4fbeb2af4adecc7de1221d4d1b7f0 to your computer and use it in GitHub Desktop.
import spacy
import random
urw_replies = [u'Happy to help you :)', u'You are more than Welcome! :)', u'No problem, Anytime! :)']
nonsense_replies = [u"Sorry, I don't understand what you are saying", u"Sorry, I cannot help you on that!",
u"That's not my area of expertise"]
break_the_ice_replies = [u"Hello, Anything I can do for you?", u"Hi there! How can I help you today?"]
friendly_replies = [u"Sure! ", u"Sure! Let's see what we have here "]
nlp = spacy.load('xx_ent_wiki_sm')
doc = nlp(message)
entities_txts = []
entities_labels = []
for ent in doc.ents:
entities_txts.append(ent.text)
entities_labels.append(ent.label_)
if intent_confidence >= 0.5:
if intent_type == "laliga_questions":
history = ""
for txt, label in zip(entities_txts, entities_labels):
if label in ['LOC', 'ORG', 'PER']:
# retrieve the history of the mentioned location/ organization in the user's message
print(txt, label)
history = df[(df['Team'] == txt) | (df['Team_alt'] == txt.encode("utf-8"))]['History'] #df.loc[df['Team'] == txt]['History']
if len(history) < 1:
continue
if len(history) >= 1:
return random.choice(friendly_replies).encode("utf-8") + history.to_string(header=False, index=False,dtype=str, max_rows=None) #' '.join([s for s in history.values.tolist()])
return random.choice(nonsense_replies).encode("utf-8")
if intent_type == "city_questions":
summary = ""
for txt, label in zip(entities_txts, entities_labels):
if label in ['LOC', 'ORG', 'PER']:
# retrieve an intro of the mentioned location/ organization in the user's message
print(txt, label)
summary = df[(df['Team'] == txt) | (df['Team_alt'] == txt.encode("utf-8"))]['Summary'] #df.loc[df['Team'] == txt]['Summary']
if len(summary) < 1:
continue
if len(summary) >= 1:
return random.choice(friendly_replies).encode("utf-8") + summary.to_string(header=False, index=False,dtype=str, max_rows=None) #' '.join([s for s in summary.values.tolist()])
return random.choice(nonsense_replies).encode("utf-8")
elif intent_type == "thankyou":
return random.choice(urw_replies).encode("utf-8")
elif intent_type == "great":
return random.choice(break_the_ice_replies).encode("utf-8")
return random.choice(nonsense_replies).encode("utf-8")
else:
return random.choice(nonsense_replies).encode("utf-8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment