def human2machine(msg): if not isinstance(msg, unicode): msg = msg.decode('utf-8') #: process with some hard coded translations first for k, v in h_d.items(): if k in msg.split('@3bugs')[-1]: return v[0] action = None action_type = None obj = None repeated_duration = 0 import jieba.posseg as pseg seg = classify(pseg.cut(msg), l_d.keys()) for v in seg['verb']: action = ch_d.get(v, None)[0] or action action_type = action_type_d.get(action, None)[0] for n in seg['noun']: obj = ch_d.get(n, None)[0] or obj repeated_duration = find_repeated(seg) or 0 if action and (action_type is not None) and \ (action == 'capture' or obj): return action, action_type, obj, repeated_duration else: logger.info('Found unknown command %s' % msg) logger.debug('%s %s %s %s' % (str(action), str(action_type), str(obj), str(repeated_duration))) logger.debug(seg) return None
def find_repeated(d): '''Find repeated duration''' if not d.get('num', None) or \ not (d.get('measure', None) or d.get('noun', None)): return None basic, unit = None, None for n in d['num']: try: basic = int(n) except: pass for m in d.get('measure', []) + d.get('noun', []): unit = t_d.get(ch_d.get(m, None)[0], None) or unit if basic and unit: return unit(basic) return None