You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.3 KiB

6 years ago
6 years ago
  1. import json
  2. import requests
  3. from stanford_parser.parser import Parser
  4. import nltk
  5. import nltk.corpus as corpus
  6. def getData( searched, data, data_final ):
  7. if type( searched ) is dict:
  8. for key in searched:
  9. if len( searched[ key ] ) == 0:
  10. data_final[ key ] = data[ key ]
  11. else:
  12. getData( searched[ key ], data[ key ], data_final )
  13. elif type( searched ) is list:
  14. for key in range( len( searched ) ):
  15. if len( searched[ key ] ) == 0:
  16. data_final[ key ] = data[ key ]
  17. else:
  18. getData( searched[ key ], data[ key ], data_final )
  19. parser = Parser()
  20. while True:
  21. sentence = raw_input("You:")
  22. #Parse the input
  23. tokenized = nltk.word_tokenize( sentence )
  24. stop_words = set( corpus.stopwords.words( 'english' ) )
  25. cleaned = [ w for w in tokenized if w not in stop_words ]
  26. tagged = nltk.pos_tag( cleaned )
  27. named = nltk.ne_chunk( tagged )
  28. cleaned_sentence = " ".join( cleaned )
  29. dependencies = parser.parseToStanfordDependencies( cleaned_sentence )
  30. tupleResult = [ (rel, gov.text, dep.text) for rel, gov, dep in dependencies.dependencies ]
  31. #Read the files
  32. files = [open("user_details.json","r"),open('keywords.json', 'r'),open('predefined.json', 'r')]
  33. defaults = json.loads(files[0].read())
  34. keywords = json.loads( files[1 ].read() )
  35. predefined = json.loads(files[2].read())
  36. print("[TAGGED]: " + str( tagged ) + "\n")
  37. print("[SPECIAL]: " + str( named ) + "\n")
  38. print("[CLEAN]: " + str( cleaned_sentence ) + "\n")
  39. print("[DEPENDENCIES]: " + str( tupleResult ) + "\n")
  40. wp_word = ""
  41. for i in range( len( tagged ) ):
  42. if tagged[ i ][ 1 ] == "WP" or tagged[ i ][ 1 ] == "WRB" or tagged[ i ][ 1 ] == "WP$":
  43. wp_word = tagged[ i ][ 0 ]
  44. if sentence in predefined:
  45. print(predefined[sentence])
  46. else:
  47. if not wp_word == "":
  48. key = ""
  49. understood = False
  50. key_connect = ""
  51. for dep in tupleResult:
  52. if dep[2] == wp_word:
  53. key = dep[1]
  54. for dep in tupleResult:
  55. if dep[1]==key: key_connect=dep[2]
  56. for keyword_id in keywords[0 ]:
  57. if key in keywords[0 ][keyword_id ]:
  58. key = keyword_id
  59. understood = True
  60. if(understood):
  61. api_details = keywords[ 1 ][ key ]
  62. params = api_details[ "parameters" ]
  63. for i in named:
  64. for j in params:
  65. try:
  66. i.label()
  67. if i.label() == params[ j ]:
  68. params[j] = i[0]
  69. else:
  70. params[j] = defaults[api_details["defaults"][j]]
  71. except Exception:
  72. if i[0]==key_connect:
  73. params[j] = defaults[api_details["defaults"][j]]
  74. params[ api_details[ 'apikey' ].keys()[ 0 ] ] = api_details[ 'apikey' ][ api_details[ 'apikey' ].keys()[ 0 ] ]
  75. r = requests.get( url=api_details[ "url" ], params=params )
  76. data = r.json()
  77. data_final = {}
  78. getData(api_details[ "data_to_receive" ], data, data_final)
  79. out_format = api_details[ "output_format" ]
  80. out_final = ""
  81. isvar = False
  82. for let in out_format:
  83. if (let == "{"):
  84. var = ""
  85. isvar = True
  86. continue
  87. if not isvar:
  88. out_final += let
  89. else:
  90. if let == "}":
  91. out_final += str( data_final[ var ] )
  92. isvar = False
  93. else:
  94. var += let
  95. print ("[KEYWORD]: " + key + "\n")
  96. print ("[KEYWORD_ID]: " + key + "\n")
  97. print (out_final)
  98. else:
  99. print("Sorry, I didn't quite get that")
  100. else:
  101. print("Sorry, I didn't quite get that")