def _determine_answer(self, description, grammar, target, is_preempt_requested): grammar_parser = CFGParser.fromstring(grammar) grammar_parser.verify(target) self.start_listen_client.wait_for_service() self.start_listen_client.call() timeout = 10 score = 0.0 while score < 0.8: # Now wait for result try: result = rospy.wait_for_message("/hsrb/voice/text", RecognitionResult, timeout) except rospy.ROSException: self.stop_listen_client.call() return None score = result.scores[0] self.stop_listen_client.call() sentence = result.sentences[0] rospy.loginfo("Julius Client received sentence %s", sentence) if result is None: return None semantics = grammar_parser.parse(target, sentence) return HMIResult(sentence, semantics)
def get_dragonfly_grammar(grammar, target, result_queue): global RULES RULES = {} parser = CFGParser.fromstring(grammar) dragonfly_rule_element = _get_dragonfly_rule_element(target, parser) dragonfly_grammar = Grammar("G") with result_queue.mutex: result_queue.queue.clear() class GrammarRule(Rule): def process_recognition(self, node): logger.info('Dragonfly node: %s', str(node)) result = node.value() logger.info('Dragonfly result: %s', str(result)) flattened_string = result if isinstance(result, list): flattened_string = " ".join(flatten(result)) logger.info('Dragonfly flattened result: %s', str(flattened_string)) if not result_queue.empty(): logger.warn('There is already a message in the queue! %s', result_queue) result_queue.put_nowait(flattened_string) logger.debug("Dragonfly thread recognition Q [id=%s, qsize=%d]", id(result_queue), result_queue.qsize()) rule = GrammarRule(element=dragonfly_rule_element, exported=True) dragonfly_grammar.add_rule(rule) return dragonfly_grammar
def _recognition_thread(self): if self._grammar: try: cfg_parser = CFGParser.fromstring(self._grammar) self._sentence = cfg_parser.get_random_sentence(self._target) except Exception as e: print e self._sentence = "" self._grammar = None
def _recognition_thread(self): if self._grammar: try: cfg_parser = CFGParser.fromstring(self._grammar) self._sentence = cfg_parser.get_random_sentence(self._target) except Exception as e: print( f"Could not get random string: {e}\n{traceback.format_exc()}" ) self._sentence = "" self._grammar = None
def _determine_answer(self, description, grammar, target, is_preempt_requested): if is_preempt_requested and self._target: rospy.logwarn("Preempt requested while a query was being answered") self._answer_ready.set() else: rospy.logwarn( "Preempt requested while NO query was being answered") rospy.loginfo('Sending to telegram') self._intermediate_answer = [] self._target = target self._grammar_parser = CFGParser.fromstring(grammar) self._grammar_parser.verify() self._options_pub.publish( Options(question=description, options=sorted( set( self._grammar_parser.next_word( self._target, self._intermediate_answer))))) start = rospy.Time.now() while not rospy.is_shutdown() and \ rospy.Time.now() < start + rospy.Duration(self._timeout) and \ not self._answer_ready.isSet(): self._answer_ready.wait(1) if self._answer_ready.is_set(): rospy.loginfo("Answer is ready") self._answer_ready.clear() try: sentence = self.intermediate_sentence rospy.loginfo("Gathered sentence: %s", sentence) if sentence: semantics = parse_sentence(sentence, grammar, target) rospy.loginfo("Parsed semantics: %s", semantics) self._message_pub.publish( "Completed sentence: '{}'. Thnx!".format(sentence)) self._intermediate_answer = [] self._target = '' return HMIResult(sentence, semantics) except Exception: raise else: self._message_pub.publish("Sorry, that took too long") rospy.loginfo("Telegram took to look to complete the query")
def recognize(self, grammar, target, is_preempt_requested=None): grammar_parser = CFGParser.fromstring(grammar) grammar_parser.verify(target) conn = multiprocessing.connection.Client(self._address) conn.send((grammar, target)) while not conn.poll(.1): if is_preempt_requested and is_preempt_requested(): conn.send(0) # Send a cancel request return None # Now wait for result sentence = conn.recv() logging.info("Dragonfly Client received sentence %s", sentence) return grammar_parser.parse(target, sentence), sentence
def recognize(self, grammar, target, is_preempt_requested=None): grammar_parser = CFGParser.fromstring(grammar) grammar_parser.verify(target) conn = multiprocessing.connection.Client(self._address) conn.send((grammar, target)) while not conn.poll(.1): if is_preempt_requested and is_preempt_requested(): conn.send(0) # Send a cancel request return None # Now wait for result sentence = conn.recv() logging.info("Dragonfly Client received sentence %s", sentence) return grammar_parser.parse(target, sentence), sentence
def get_dragonfly_grammar(engine, grammar, target, result_queue): global RULES RULES = {} parser = CFGParser.fromstring(grammar) dragonfly_rule_element = _get_dragonfly_rule_element(target, parser) dragonfly_grammar = Grammar("G", engine=engine) with result_queue.mutex: result_queue.queue.clear() class GrammarRule(Rule): def process_recognition(self, node): logger.info('Dragonfly node: %s', str(node)) result = node.value() logger.info('Dragonfly result: %s', str(result)) flattened_string = result if isinstance(result, list): flattened_string = " ".join(flatten(result)) logger.info('Dragonfly flattened result: %s', str(flattened_string)) if not result_queue.empty(): logger.warning('There is already a message in the queue! %s', result_queue) result_queue.put_nowait(flattened_string) logger.debug("Dragonfly thread recognition Q [id=%s, qsize=%d]", id(result_queue), result_queue.qsize()) rule = GrammarRule(element=dragonfly_rule_element, exported=True) dragonfly_grammar.add_rule(rule) return dragonfly_grammar
# ANSWER QUESTION # ############################################################################## grammar += """ VP[{"action": "answer-question"}] -> answer a question """ if __name__ == "__main__": print "GPSR Grammar:\n\n{}\n\n".format(grammar) from grammar_parser.cfgparser import CFGParser import sys if sys.argv[1] == "object": grammar_parser = CFGParser.fromstring(obj_grammar) elif sys.argv[1] == "location": grammar_parser = CFGParser.fromstring(loc_grammar) elif sys.argv[1] == "full": grammar_parser = CFGParser.fromstring(grammar) if len(sys.argv) > 2: sentence = " ".join(sys.argv[2:]) else: sentence = grammar_parser.get_random_sentence("T") print "Parsing sentence:\n\n{}\n\n".format(sentence) result = grammar_parser.parse("T", sentence) print "Result:\n\n{}".format(result)
############################################################################## # # FIND OUT AND REPORT # ############################################################################## grammar += """ VP[{"action": "tell-name-of-person", "location": X}] -> tell me the name of the person MEETING_PP the ROOM_OR_LOCATION[X] VP[{"action": "count-and-tell", "object": X, "location": Y}] -> tell me how many NAMED_OBJECT[X] there are on the LOCATION[Y] """ if __name__ == "__main__": print "GPSR Grammar:\n\n{}\n\n".format(grammar) from grammar_parser.cfgparser import CFGParser import sys grammar_parser = CFGParser.fromstring(grammar) if len(sys.argv) > 2: sentence = " ".join(sys.argv[2:]) else: sentence = grammar_parser.get_random_sentence("T") print "Parsing sentence:\n\n{}\n\n".format(sentence) result = grammar_parser.parse("T", sentence) print "Result:\n\n{}".format(result)
############################################################################## # # FIND OUT AND REPORT # ############################################################################## grammar += """ VP[{"action": "tell-name-of-person", "location": X}] -> tell me the name of the person MEETING_PP the ROOM_OR_LOCATION[X] VP[{"action": "count-and-tell", "object": X, "location": Y}] -> tell me how many NAMED_OBJECT[X] there are on the LOCATION[Y] """ if __name__ == "__main__": print "GPSR Grammar:\n\n{}\n\n".format(grammar) from grammar_parser.cfgparser import CFGParser import sys grammar_parser = CFGParser.fromstring(grammar) if len(sys.argv) > 2: sentence = " ".join(sys.argv[2:]) else: sentence = grammar_parser.get_random_sentence("T") print "Parsing sentence:\n\n{}\n\n".format(sentence) result = grammar_parser.parse("T", sentence) print "Result:\n\n{}".format(result)
for room in common.location_rooms: grammar += "\nT[{0}] -> {0}".format(room) category_grammar = """ T[P] -> CATEGORY[P] | it is a CATEGORY[P] | the category is CATEGORY[P] """ for l in common.category_locations: category_grammar += "\nCATEGORY[{}] -> {}".format(l, l.replace('_', ' ')) category_grammar += "\nCATEGORY[{}] -> {}".format("trash", "trash".replace('_', ' ')) if __name__ == "__main__": print("Clean-up Grammar:\n\n{}\n\n".format(category_grammar)) from grammar_parser.cfgparser import CFGParser import sys grammar_parser = CFGParser.fromstring(category_grammar) if len(sys.argv) > 2: sentence = " ".join(sys.argv[2:]) else: sentence = grammar_parser.get_random_sentence("T") print("Parsing sentence:\n\n{}\n\n".format(sentence)) result = grammar_parser.parse("T", sentence) print("Result:\n\n{}".format(result))
VP["object": {"id": X}] -> HE_SHE is in the ROOM_OR_LOCATION[X] | in the ROOM_OR_LOCATION[X] | you could find HE_SHE in the ROOM_OR_LOCATION[X] """ obj_grammar = """ VP["object": {"id": Y}] -> the NAMED_OBJECT[Z] is DET NAMED_OBJECT[Y] | the NAMED_OBJECT[Z] is NAMED_OBJECT[Y] | NAMED_OBJECT[Y] | DET NAMED_OBJECT[Y] """ if __name__ == "__main__": print "GPSR Grammar:\n\n{}\n\n".format(grammar) from grammar_parser.cfgparser import CFGParser import sys if sys.argv[1] == "object": grammar_parser = CFGParser.fromstring(obj_grammar) elif sys.argv[1] == "location": grammar_parser = CFGParser.fromstring(loc_grammar) elif sys.argv[1] == "full": grammar_parser = CFGParser.fromstring(grammar) if len(sys.argv) > 2: sentence = " ".join(sys.argv[2:]) else: sentence = grammar_parser.get_random_sentence("T") print "Parsing sentence:\n\n{}\n\n".format(sentence) result = grammar_parser.parse("T", sentence) print "Result:\n\n{}".format(result)
for loc in common.location_names: location_grammar += '\nLOCATION[{"id": "%s"}] -> %s' % (loc, loc) for loc in common.object_names: category = common.get_object_category(loc) if category not in common.category_locations: continue entity_id = common.get_object_category_location(category)[0] location_grammar += '\nLOCATION[{"id": "%s"}] -> %s' % (entity_id, loc) location_grammar += '\nDET_LOCATION[Y] -> LOCATION[Y] | the LOCATION[Y]' location_grammar += """ V_GUIDE -> guide | bring | lead VP[{"target-location": Y}] -> DET_LOCATION[Y] | V_GUIDE me to DET_LOCATION[Y] | i want to go to DET_LOCATION[Y] | i would like to go to DET_LOCATION[Y] | i like to go to DET_LOCATION[Y] | tell me how to go to DET_LOCATION[Y] | tell me how to reach DET_LOCATION[Y] """ if __name__ == "__main__": print("Where is this Grammar:\n\n{}\n\n".format(location_grammar)) from grammar_parser.cfgparser import CFGParser grammar_parser = CFGParser.fromstring(location_grammar) grammar_parser.verify() grammar_parser.check_rules()
def verify_grammar(grammar, target=None): grammar_parser = CFGParser.fromstring(grammar) grammar_parser.verify(target)
location_grammar += '\nDET_LOCATION[Y] -> LOCATION[Y] | the LOCATION[Y]' location_grammar += """ V_GUIDE -> guide | bring | lead VP[Y] -> DET_LOCATION[Y] | V_GUIDE me to DET_LOCATION[Y] | i want to go to DET_LOCATION[Y] | i would like to go to DET_LOCATION[Y] | i like to go to DET_LOCATION[Y] | tell me how to go to DET_LOCATION[Y] | tell me how to reach DET_LOCATION[Y] """ if __name__ == "__main__": print("Where is this Grammar:\n\n{}\n\n".format(location_grammar)) from grammar_parser.cfgparser import CFGParser print("Location Grammar") grammar_parser = CFGParser.fromstring(location_grammar) grammar_parser.verify() grammar_parser.check_rules() print("Random Sentence:") sentence = grammar_parser.get_random_sentence("T") print(sentence) print("Resulting Semantics:") print(grammar_parser.parse("T", sentence)) print("\n\nStarting Point Grammar") grammar_parser = CFGParser.fromstring(starting_point_grammar) grammar_parser.verify() grammar_parser.check_rules() print("Random Sentence:") sentence = grammar_parser.get_random_sentence("T") print(sentence)
def random_sentence(grammar, target): grammar_parser = CFGParser.fromstring(grammar) grammar_parser.verify() return grammar_parser.get_random_sentence(target)
def parse_sentence(sentence, grammar, target): grammar_parser = CFGParser.fromstring(grammar) return grammar_parser.parse(target, sentence)