def import_howto(args): try: howto_steps, verbose, save = args prac = PRAC() prac.verbose = verbose for howto, steps in list(howto_steps.items()): prac.tell(howto=howto, steps=steps, save=save) except KeyboardInterrupt: return
def extract_actionroles(adt_instance, queryHelper,actioncore): prac = PRAC() #PRAC uses snake case to represent the actionroles #However the episodic memory log file uses camel case actionroles = list(map(snake_case_to_camel_case, prac.actioncores[actioncore].roles)) extraction_result = {} for actionrole in actionroles: actionrole_uri = PRAC_ADT_PROPERTY_URI+actionrole query_result = perform_triplet_query(queryHelper, adt_instance, actionrole_uri) if query_result: #It will be assumed that one role is only represented once in the adt role_owl_individual_uri = query_result[0][2] query_result = perform_triplet_query(queryHelper,role_owl_individual_uri,RDF_TYPE) #At the moment the actionrole is represented with the URI 'http://knowrob.org/kb/actionrole' #TODO Update this procedure if the connection to WordNet is added to the OWL files for e in query_result: temp = e[2].split("/")[-1] if not temp.startswith("owl#"): extraction_result[actionrole] = str(temp) return extraction_result
def main(): headline("Running main...") usage = 'PRAC Object Recognition tool' parser = argparse.ArgumentParser(description=usage) parser.add_argument( "-i", "--interactive", dest="interactive", default=False, action='store_true', help="Starts PRAC object recognition with an interactive " "GUI tool.") parser.add_argument( "-t", "--train", dest="trainMLN", nargs=1, default=None, help= "Train given MLN with inference results from argument. Example: pracobjrec -t orange.n.01 'It is a yellow " "or orange fruit.'") parser.add_argument( "-r", "--regular", dest="regular", default=False, action='store_true', help="Runs regular inference pipeline. Arguments: mlnName") parser.add_argument("-f", "--onthefly", dest="onthefly", default=False, action='store_true', help="Generate MLN on the fly") parser.add_argument( "-m", "--mln", nargs=2, dest='mln', default=None, help="Runs regular inference pipeline. Arguments: mlnName") args = parser.parse_args() opts_ = vars(args) interactive = args.interactive regular = args.regular sentences = args prac = PRAC() prac.wordnet = WordNet(concepts=None) infer = PRACInference(prac, sentences) # in case we have natural-language parameters, parse them if len(infer.instructions) > 0: parser = prac.module('nl_parsing') prac.run(infer, parser) if interactive: # use the GUI logger.info('Entering interactive mode') gui = PRACQueryGUI(infer) gui.open() elif args.trainMLN: # training with property inference output logger.info( 'Training MLN {} with result from property inference'.format( args.trainMLN)) # property inference from parsed input propExtract = prac.module('prop_extraction') prac.run(infer, propExtract) objRecog = prac.module('obj_recognition') praclearn = PRACLearning(prac) praclearn.otherParams['mln'] = args.mln[0] praclearn.otherParams['logic'] = args.mln[1] praclearn.otherParams['concept'] = args.trainMLN praclearn.otherParams['onthefly'] = args.onthefly praclearn.training_dbs = infer.inference_steps[-1].output_dbs objRecog.train(praclearn) sys.exit(0) else: # regular PRAC pipeline logger.info('Entering regular inference pipeline') # property inference from parsed input propExtract = prac.module('prop_extraction') prac.run(infer, propExtract) objRecog = prac.module('obj_recognition') # object inference based on inferred properties prac.run(infer, objRecog) step = infer.inference_steps[-1] print() print(prac_heading('PRAC INFERENCE RESULTS')) print() print('Object description: {}'.format( colorize(''.join(sentences), (None, 'white', True), True))) print() for db in step.output_dbs: print('Inferred properties:') for ek in sorted(db.evidence): e = db.evidence[ek] if e == 1.0 and any( ek.startswith(p) for p in [ 'color', 'size', 'shape', 'hypernym', 'hasa', 'dimension', 'consistency', 'material' ]): print('{}({}, {}'.format( colorize(ek.split('(')[0], (None, 'white', True), True), colorize( ek.split('(')[1].split(',')[0], (None, 'magenta', True), True), colorize( ek.split('(')[1].split(',')[1], (None, 'green', True), True))) for db in step.output_dbs: print() print('Inferred possible concepts:') for ek in sorted(db.evidence, key=db.evidence.get, reverse=True): e = db.evidence[ek] if e > 0.001 and ek.startswith('object'): print('{} {}({}, {})'.format( colorize('{:.4f}'.format(e), (None, 'cyan', True), True), colorize('object', (None, 'white', True), True), colorize( ek.split(',')[0].split('(')[1], (None, 'magenta', True), True), colorize( ek.split(',')[1].split(')')[0], (None, 'yellow', True), True)))
constants.JSON_SENSE_WORD: self.word, constants.JSON_SENSE_LEMMA: self.lemma, constants.JSON_SENSE_POS: self.pos, constants.JSON_SENSE_WORD_IDX: self.widx, constants.JSON_SENSE_SENSE: self.sense, constants.JSON_SENSE_MISC: self.misc }) @staticmethod def fromjson(prac, data): return Word(prac, data.get(constants.JSON_SENSE_WORD), data.get(constants.JSON_SENSE_LEMMA), data.get(constants.JSON_SENSE_POS), data.get(constants.JSON_SENSE_SENSE), data.get(constants.JSON_SENSE_WORD_IDX), data.get(constants.JSON_SENSE_WORD)) if __name__ == '__main__': from prac.core.base import PRAC prac = PRAC() o1 = Object(prac, 'w1', 'cup.n.01', syntax=Word(prac, 'water-1', 'water', 1, 'water.n.06', 'NN', 'water'), props={'color': 'green.n.01'}) print(o1) print(repr(o1)) pprint(o1.tojson())
def main(): logger.level = logs.DEBUG usage = 'PRAC Query Tool' parser = argparse.ArgumentParser(description=usage) parser.add_argument("instruction", help="The instruction.") parser.add_argument( "-i", "--interactive", dest="interactive", default=False, action='store_true', help="Starts PRAC inference with an interactive GUI tool.") parser.add_argument("-v", "--verbose", dest="verbose", default=1, type=int, action="store", help="Set verbosity level {0..3}. Default is 1.") args = parser.parse_args() opts_ = vars(args) sentences = args.instruction prac = PRAC() prac.verbose = args.verbose conf = PRACMLNConfig(DEFAULT_CONFIG) if args.interactive: # use the GUI from tkinter import Tk root = Tk() # in case we have natural-language parameters, parse them infer = PRACInference(prac, sentences) if len(sentences) > 0: # module = prac.module('nl_parsing') # prac.run(infer, module) n = infer.runstep() # print parsing result for odb in n.outdbs: odb.write() # print input sentence print(n.nlinstr()) #Started control structure handling ''' cs_recognition = prac.module('cs_recognition') prac.run(inference, cs_recognition) dbs = inference.inference_steps[-1].output_dbs dbs_ = [] for db in dbs: dbs_.extend(parser.extract_multiple_action_cores(db)) inference.inference_steps[-1].output_dbs = dbs_ ''' app = PRACQueryGUI(root, infer.prac, n, conf, directory=args[0] if args else None) root.mainloop() exit(0) # regular PRAC pipeline infer = PRACInference(prac, sentences) infer.run() print(headline('inference results')) print('instructions:') for i in infer.root: print(i) frames = [] for step in infer.steps(): print(step.frame) print(prac_heading('cram plans', color='blue')) for step in infer.steps(): if hasattr(step, 'plan'): print(step.plan) # infer.write() exit(0)