2323from nluas .core_agent import *
2424from nluas .language .analyzer_proxy import *
2525from nluas .ntuple_decoder import NtupleDecoder
26- #from nluas.language.spell_checker import *
2726import sys , traceback , time
2827import json
2928import time
@@ -40,24 +39,20 @@ class UserAgent(CoreAgent):
4039 def __init__ (self , args ):
4140 CoreAgent .__init__ (self , args )
4241 self .initialize_UI ()
43- #self.ui_parser = self.setup_ui_parser()
4442 self .solve_destination = "{}_{}" .format (self .federation , "ProblemSolver" )
4543 self .speech_address = "{}_{}" .format (self .federation , "SpeechAgent" )
4644 self .text_address = "{}_{}" .format (self .federation , "TextAgent" )
4745 self .transport .subscribe (self .solve_destination , self .callback )
4846 self .transport .subscribe (self .speech_address , self .speech_callback )
4947 self .transport .subscribe (self .text_address , self .text_callback )
5048
51-
5249 def setup_ui_parser (self ):
5350 parser = argparse .ArgumentParser ()
5451 parser .add_argument ("-port" , type = str , help = "indicate host to connect to" ,
5552 default = "http://localhost:8090" )
5653 return parser
5754
5855 def initialize_UI (self ):
59- #args = self.ui_parser.parse_known_args(self.unknown)
60- #self.analyzer_port = args[0].port
6156 self .clarification = False
6257 self .analyzer_port = "http://localhost:8090"
6358 connected , printed = False , False
@@ -74,7 +69,6 @@ def initialize_UI(self):
7469 time .sleep (1 )
7570
7671 self .decoder = NtupleDecoder ()
77- #self.spell_checker = SpellChecker(self.analyzer.get_lexicon())
7872
7973 def initialize_analyzer (self ):
8074 self .analyzer = Analyzer (self .analyzer_port )
@@ -89,15 +83,12 @@ def initialize_specializer(self):
8983
9084 def match_spans (self , spans , sentence ):
9185 sentence = sentence .replace ("." , " . " ).replace ("," , " , " ).replace ("?" , " ? " ).replace ("!" , " ! " ).split ()
92- #final = OrderedDict()
93- #word_spans = OrderedDict()
9486 final = []
9587 for span in spans :
9688 lr = span ['span' ]
9789 final .append ([span ['type' ], sentence [lr [0 ]:lr [1 ]], lr , span ['id' ]])
9890 return final
9991
100-
10192 def process_input (self , msg ):
10293 try :
10394 full_parse = self .analyzer .full_parse (msg )
@@ -110,12 +101,11 @@ def process_input(self, msg):
110101 matched = self .match_spans (span , msg )
111102 self .specializer .set_spans (matched )
112103 ntuple = self .specializer .specialize (fs )
113-
114- #json_ntuple = self.decoder.convert_to_JSON(ntuple)
115104 return ntuple
116105 except Exception as e :
117- #self.output_stream(self.name, e)
118- #traceback.print_exc()
106+ self .verbose :
107+ traceback .print_exc ()
108+ self .output_stream (self .name , e )
119109 index += 1
120110 except Exception as e :
121111 print (e )
@@ -126,20 +116,17 @@ def output_stream(self, tag, message):
126116
127117 def speech_callback (self , ntuple ):
128118 """ Processes text from a SpeechAgent. """
129- #print(ntuple)
130- #ntuple = json.loads(ntuple)
131119 text = ntuple ['text' ].lower ()
132- print ("Got {}" .format (text ))
120+ if self .verbose :
121+ print ("Got {}" .format (text ))
133122 new_ntuple = self .process_input (text )
134123 if new_ntuple and new_ntuple != "null" and "predicate_type" in new_ntuple :
135124 self .transport .send (self .solve_destination , new_ntuple )
136125
137126
138127 def text_callback (self , ntuple ):
139128 """ Processes text from a SpeechAgent. """
140- #print(ntuple)
141129 specialize = True
142- #ntuple = json.loads(ntuple)
143130 msg = ntuple ['text' ]
144131 if self .is_quit (ntuple ):
145132 self .close ()
@@ -162,8 +149,6 @@ def text_callback(self, ntuple):
162149
163150
164151 def callback (self , ntuple ):
165- print (ntuple )
166- #ntuple = self.decoder.convert_JSON_to_ntuple(ntuple)
167152 call_type = ntuple ['type' ]
168153 if call_type == "id_failure" :
169154 self .output_stream (ntuple ['tag' ], ntuple ['message' ])
@@ -181,15 +166,13 @@ def write_file(self, json_ntuple, msg):
181166 f = open (generated , "w" )
182167 f .write (json_ntuple )
183168
184-
185-
186169 def process_clarification (self , tag , msg , ntuple ):
187170 self .clarification = True
188- #self.output_stream(tag, msg)
171+ if self .verbose :
172+ self .output_stream (tag , msg )
189173 new_ntuple = {'tag' : tag , 'message' : msg , 'type' : "clarification" , 'original' : ntuple }
190174 self .transport .send (self .text_address , new_ntuple )
191175
192-
193176 def clarify_ntuple (self , ntuple , descriptor ):
194177 """ Clarifies a tagged ntuple with new descriptor. """
195178 new = dict ()
@@ -203,20 +186,5 @@ def clarify_ntuple(self, ntuple, descriptor):
203186 new [key ] = value
204187 return new
205188
206- def check_spelling (self , msg ):
207- table = self .spell_checker .spell_check (msg )
208- if table :
209- checked = self .spell_checker .join_checked (table ['checked' ])
210- if checked != msg :
211- print (self .spell_checker .print_modified (table ['checked' ], table ['modified' ]))
212- affirm = input ("Is this what you meant? (y/n) > " )
213- if affirm and affirm [0 ].lower () == "y" :
214- self .process_input (checked )
215- else :
216- return
217- else :
218- self .process_input (msg )
219-
220-
221189if __name__ == "__main__" :
222190 ui = UserAgent (sys .argv [1 :])
0 commit comments