Guest User

Untitled

a guest
Feb 28th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. # -*- encoding: utf-8 -*-
  2. __author__ = 'malevolent'
  3. """
  4. A sample program that uses multiple intents and disambiguates by
  5. intent confidence
  6.  
  7. try with the following:
  8. PYTHONPATH=. python examples/multi_intent_parser_spanish.py "qué tiempo hace en tokyo"
  9. PYTHONPATH=. python examples/multi_intent_parser_spanish.py "pon algo de música de los clash"
  10. """
  11.  
  12. import json
  13. import sys
  14. from adapt.entity_tagger import EntityTagger
  15. from adapt.tools.text.tokenizer import EnglishTokenizer
  16. from adapt.tools.text.trie import Trie
  17. from adapt.intent import IntentBuilder
  18. from adapt.parser import Parser
  19. from adapt.engine import IntentDeterminationEngine
  20.  
  21. tokenizer = EnglishTokenizer()
  22. trie = Trie()
  23. tagger = EntityTagger(trie, tokenizer)
  24. parser = Parser(tokenizer, tagger)
  25.  
  26. engine = IntentDeterminationEngine()
  27.  
  28. # define vocabulary
  29. weather_keyword = [
  30.     "tiempo"
  31. ]
  32.  
  33. for wk in weather_keyword:
  34.     engine.register_entity(wk, "WeatherKeyword")
  35.  
  36. weather_types = [
  37.     "nieve",
  38.     "lluvia",
  39.     "viento",
  40.     "aguanieve",
  41.     "sol"
  42. ]
  43.  
  44. for wt in weather_types:
  45.     engine.register_entity(wt, "WeatherType")
  46.  
  47. locations = [
  48.     "Seattle",
  49.     "San Francisco",
  50.     "Tokyo"
  51. ]
  52.  
  53. for l in locations:
  54.     engine.register_entity(l, "Location")
  55.  
  56. # structure intent
  57. weather_intent = IntentBuilder("WeatherIntent")\
  58.     .require("WeatherKeyword")\
  59.     .optionally("WeatherType")\
  60.     .require("Location")\
  61.     .build()
  62.  
  63.  
  64. # define music vocabulary
  65. artists = [
  66.     "third eye blind",
  67.     "the who",
  68.     "los clash",
  69.     "john mayer",
  70.     "kings of leon",
  71.     "adelle"
  72. ]
  73.  
  74. for a in artists:
  75.     engine.register_entity(a, "Artist")
  76.  
  77. music_verbs = [
  78.     "escuchar",
  79.     "oir",
  80.     "tocar",
  81.     "pon"
  82. ]
  83.  
  84. for mv in music_verbs:
  85.     engine.register_entity(mv, "MusicVerb")
  86.  
  87. music_keywords = [
  88.     "canciones",
  89.     "música"
  90. ]
  91.  
  92. for mk in music_keywords:
  93.     engine.register_entity(mk, "MusicKeyword")
  94.  
  95. music_intent = IntentBuilder("MusicIntent")\
  96.     .require("MusicVerb")\
  97.     .optionally("MusicKeyword")\
  98.     .optionally("Artist")\
  99.     .build()
  100.  
  101. engine.register_intent_parser(weather_intent)
  102. engine.register_intent_parser(music_intent)
  103.  
  104.  
  105. if __name__ == "__main__":
  106.     for intent in engine.determine_intent(' '.join(sys.argv[1:])):
  107.         if intent and intent.get('confidence') > 0:
  108.             print(json.dumps(intent, indent=4))
Advertisement
Add Comment
Please, Sign In to add comment