项目作者: SilvioMessi

项目描述 :
Python library for Natural Language Understanding
高级语言: Python
项目地址: git://github.com/SilvioMessi/nlu.git
创建时间: 2017-06-22T09:20:08Z
项目社区:https://github.com/SilvioMessi/nlu

开源协议:

下载


NLU

Python library for NLU (Natural Language Understanding).
This python library allows to identify intents and entities present in a sentence, starting from a train set of examples.

Installation

  1. pip install git+https://github.com/SilvioMessi/nlu.git

Dependencies

  • Stanford CoreNLP
    At the moment all the NLP (Natural Language Processing) task are made by Stanford CoreNLP, used like external server.
    Before use NLU library make sure that an instance of CoreNLP Server is correctly running on port 9000.
  • Wordnet
    The NLU library use Wordnet.
    Before use NLU library make sure that Wordnet corpora is correctly “intstalled” by NTLK.

Basic Usage

  1. from nlu.entity_recognizer import EntityRecognizer
  2. from nlu.intent_recognizer import IntentRecognizer
  3. entity_recongizer = EntityRecognizer()
  4. intent_recognizer = IntentRecognizer()
  5. # load entities definitions from datastore and put them in data structure
  6. entities = {
  7. 'pizza_type': {
  8. 'Margherita' : ['Margherita', 'margherita'],
  9. 'Neapolitan' : ['Neapolitan', 'neapolitan'],
  10. 'Sicilian' : ['Sicilian', 'sicilian']
  11. },
  12. 'drink_type' : {
  13. 'Coca-Cola' : ['Coca-Cola', 'coca cola', 'coke'],
  14. 'Beer' : ['Beer', 'beer'],
  15. 'Water' : ['Water', 'water'],
  16. 'Wine' : ['Wine', 'wine']
  17. }
  18. }
  19. # load intents definitions from datastore and put them in data structure
  20. intents = {
  21. 'order_a_pizza': ['Can i have a pizza margherita?', 'A pizza margherita, please !', 'Two margherita please!'],
  22. 'order_a_drink': ['Can i have a can of coke?', 'I\'ll have a glass of wine!']
  23. }
  24. # intents definitions can be pre tagged with entities
  25. intents_pre_tagged = {
  26. 'order_a_pizza': ['Can i have a pizza pizza_type?', 'A pizza pizza_type, please !', 'Two pizza_type please!'],
  27. 'order_a_drink': ['Can i have a can of drink_type?', 'I\'ll have a glass of drink_type!']
  28. }
  29. new_sentence = 'I want order a pizza margherita and a can of coca cola!'
  30. # find entities in new sentence
  31. sentence_tokens, final_entities_positions, tagged_sentence = entity_recongizer.get_entities(entities, new_sentence, tag_sentence=True)
  32. print(sentence_tokens)
  33. ['I', 'want', 'order', 'a', 'pizza', 'margherita', 'and', 'a', 'can', 'of', 'coca', 'cola', '!']
  34. print(final_entities_positions)
  35. [{'entity_id': 'pizza_type', 'value_id': 'Margherita', 'start': 21, 'end': 31}, {'entity_id': 'drink_type', 'value_id': 'Coca-Cola', 'start': 45, 'end': 54}]
  36. # if parameter tag_sentence=True
  37. print(tagged_sentence)
  38. I want order a pizza pizza_type and a can of drink_type !
  39. # find intents probabilities
  40. intents_probabilities = intent_recognizer.get_intents_probabilities(intents, new_sentence)
  41. print(intents_probabilities)
  42. {'order_a_pizza': 0.51000000000000001, 'order_a_drink': 0.161}
  43. # better performance can be achieved used tagged sentences
  44. intents_probabilities = intent_recognizer.get_intents_probabilities(intents_pre_tagged, tagged_sentence)
  45. print(intents_probabilities)
  46. {'order_a_pizza': 0.53653846153846152, 'order_a_drink': 0.23928846153846156}