def test_nltk_cfg_qtype(self): print("test_nltk_cfg_qtype") gfile = os.path.join(curdir, os.path.pardir, "config", "grammar.question-type.cfg") question_grammar = nltk.data.load('file:%s' % gfile) def get_missing_words(grammar, tokens): """ Find list of missing tokens not covered by grammar """ missing = [ tok for tok in tokens if not grammar._lexical_index.get(tok) ] return missing sentence = "what is your name" sent = sentence.split() missing = get_missing_words(question_grammar, sent) target = [] for x in sent: if x in missing: continue target.append(x) rd_parser = RecursiveDescentParser(question_grammar) result = [] print("target: ", target) for tree in rd_parser.parse(target): result.append(x) print("Question Type\n", tree) if len(result) == 0: print("Not Question Type")
def cfg_zh(self): grammar = nltk.CFG.fromstring(""" S -> N VP VP -> V NP | V NP | V N V -> "尊敬" N -> "我们" | "老师" """) sent = "我们 尊敬 老师".split() rd_parser = RecursiveDescentParser(grammar) result = [] for i, tree in enumerate(rd_parser.parse(sent)): result.append(tree) print("Tree [%s]: %s" % (i + 1, tree)) assert len(result) > 0, "Can not recognize CFG tree." if len(result) == 1: print("Draw tree with Display ...") result[0].draw() else: print("WARN: Get more then one trees.") print(result)
def cfg_en(self): print("test_nltk_cfg_en") # 定义英文语法规则 grammar = nltk.CFG.fromstring(""" S -> NP VP VP -> V NP | V NP PP V -> "saw" | "ate" NP -> "John" | "Mary" | "Bob" | Det N | Det N PP Det -> "a" | "an" | "the" | "my" N -> "dog" | "cat" | "cookie" | "park" PP -> P NP P -> "in" | "on" | "by" | "with" """) sent = "Mary saw Bob".split() rd_parser = RecursiveDescentParser(grammar) result = [] for i, tree in enumerate(rd_parser.parse(sent)): result.append(tree) assert len(result) > 0, " CFG tree parse fail." print(result)
def Process(str, file): sr = RecursiveDescentParser(pgrammar) r = list(sr.parse(str.split())) if len(r) > 0: cadResult = GenerateCadFile(ParentedTree.convert(r[0])) cadResult.write(file) else: print("************* " + str)
def test2(): from nltk.parse import RecursiveDescentParser rd = RecursiveDescentParser(grammar) sentence1 = 'the cat chased the dog'.split() sentence2 = 'the cat chased the dog on the rug'.split() print rd.parse(sentence2)
def check_syntax(text): # here we list all possible languages that can be used in the grammar lang_pos = {} for l in pycountry.languages: p = pycountry.languages.get(name=l.name) try: alpha_kind = p.alpha_2 lang_pos[p.name]=alpha_kind except: pass lang_command = '''LANG ->''' for lg in lang_pos: if list(lang_pos.keys()).index(lg) == 0: lang_command+=' '+str(lang_pos[lg])+' ' else: lang_command+='''| '{x}' '''.format(x=lang_pos[lg]) # here we list all possible functions , given that the system was already re-written to accomodate # the changes func_command = '''FUNC ->''' for attr in dir(node_func): if dir(node_func).index(attr) == 0: func_command+=' '+str(attr)+' ' else: func_command+='''| '{x}' '''.format(x=attr) # here we substitute the pre made rules in the grammar itself grammar = CFG.fromstring((''' S -> 'plug' '<' FUNC '>' 'as' LANG | 'unplug' '<' LANG '>' command1 command2 '''.replace('command1',lang_command)).replace('command2',func_command)) grammar_rd = RecursiveDescentParser(grammar) # here we check the syntax and the lexical using the already described cfg for t in text.split('\n'): parsed = [] try: for tree in grammar_rd.parse(t.split()): parsed.append(tree) if len(parsed) != 0: print(parsed) pass else: return 'syntax error' return 'parsed' except: return 'syntax/lexical error'
def sensibility_test(transcribeText, backdoor): if backdoor: print('Sentence is sensible') return 1 else: grammar = nltk.data.load('grammars/book_grammars/drt.cfg') # sr = ShiftReduceParser(grammar=grammar) rd = RecursiveDescentParser() try: for t in rd.parse(transcribeText): print(t) print('Sentence is sensible') except: print('Sentence is not sensible')
class GridGen(): def __init__(self): PrWd_rules = [] PrWd_rules_old = [''] for i in range(7): # set maximum level-1 length PrWd_rules_new = [x +' '+ y for x in PrWd_rules_old \ for y in ['x', 'o']] # level-1 grid PrWd_rules += PrWd_rules_new PrWd_rules_old = PrWd_rules_new[:] PrWd_rules = ['PrWd -> ' + x for x in PrWd_rules] # Culminativity (at least one level-1 grid mark) PrWd_rules = [x for x in PrWd_rules if re.search('x', x)] # Expansions of syllable preterminals Term_rules = ['x -> "σ"', 'o -> "σ"'] grammar_rules = PrWd_rules + Term_rules grammar_rulestr = '\n'.join(grammar_rules) grammar = CFG.fromstring(grammar_rulestr) print(f'# of productions in grammar: {len(grammar.productions())}') self.grammar = grammar self.parser = RecursiveDescentParser(grammar) def parses(self, inpt): T = [t for t in self.parser.parse(inpt.split())] T = [ParentedTree.convert(t) for t in T] return T
def test_sample(self): print("test_sample") # This is a CFG grammar, where: # Start Symbol : S # Nonterminal : NP,VP,DT,NN,VB # Terminal : "I", "a" ,"saw" ,"dog" grammar = nltk.grammar.CFG.fromstring(""" S -> NP VP NP -> DT NN | NN VP -> VB NP DT -> "a" NN -> "I" | "dog" VB -> "saw" """) sentence = "I saw a dog".split() parser = RecursiveDescentParser(grammar) final_tree = parser.parse(sentence) for i in final_tree: print(i)
def __init__(self): # Expansions of PrWd PrWd_rules = [] PrWd_rules_old = [''] for i in range(5): PrWd_rules_new = [x+' '+y for x in PrWd_rules_old \ for y in ['MainFt', 'Ft', 'Syll']] PrWd_rules += PrWd_rules_new PrWd_rules_old = PrWd_rules_new[:] PrWd_rules = ['PrWd -> ' + x for x in PrWd_rules] # Culminativity (exactly one main-stress foot) PrWd_rules = [x for x in PrWd_rules if re.search('Main', x) \ and not re.search('Main.*Main', x)] #print(len(PrWd_rules)) # Expansions of (Main)Ft MainFt_rules = ['MainFt -> '+y for y in \ ['MainStressSyll', 'MainStressSyll Syll', 'Syll MainStressSyll']] Ft_rules = ['Ft -> '+y for y in \ ['StressSyll', 'StressSyll Syll', 'Syll StressSyll']] # Expansions of (Main)(Stress)Syll Syll_rules = ['MainStressSyll -> s1', 'StressSyll -> s2', 'Syll -> s0'] # Expansions of syllable preterminals Term_rules = ['s1 -> "σ"', 's2 -> "σ"', 's0 -> "σ"'] grammar_rules = PrWd_rules + MainFt_rules \ + Ft_rules + Syll_rules + Term_rules grammar_rulestr = '\n'.join(grammar_rules) grammar = CFG.fromstring(grammar_rulestr) print(f'# of productions in grammar: {len(grammar.productions())}') self.grammar = grammar self.parser = RecursiveDescentParser(grammar)
def callback(self, text): """this function is to process the recognized text and send the processed information to Arduino which controls some devices""" global text_last global xm_speech_req global xm_speech_res global sentences #if len(text) != 0 : # if text[28] == '=': # text = text[29:] # elif text[29] == '=': # text = text[30:] if text != "you are right" and text != "no" and len(text) != 0: text_last = text if type(text) == str and len(text) != 0: print("recognized : " + text) if xm_speech_req.command == 1: #Answer a question if text == "you are right": print('question: ' + text_last) file_handle_c = open( '/home/domistic/catkin_ws/src/xm_speech_for_linux/xm_speech/msc/bnf/c.txt', mode='rb') file_handle_d = open( '/home/domistic/catkin_ws/src/xm_speech_for_linux/xm_speech/msc/bnf/d.txt', mode='rb') c_contents = file_handle_c.readlines() d_contents = file_handle_d.readlines() t = '\"' + text_last + '\"' + '\n' if t in c_contents: c_index = c_contents.index(t) d = d_contents[c_index] print('answer: ' + d) xm_speech_res.num = 1 self.tts(d) elif xm_speech_req.command == 2: #GPSR if text != "you are right" and text != "no" and text != "you can stop here": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: self.tts("OK") sen = text_last if "tell the day of the month" in sen: sentences = "tell the day of the month" sen = sen.replace("tell the day of the month", "sentence") if "tell the day of the week" in sen: sentences = "tell the day of the week" sen = sen.replace("tell the day of the week", "sentence") if "tell the date" in sen: sentences = "tell the date" sen = sen.replace("tell the date", "sentence") for i in double_words: sen = sen.replace(i.replace("_", " "), i) #print(sen) rd = RecursiveDescentParser(grammar) t = rd.parse_one(sen.split()) #print(t) search(t) text_last = '' elif xm_speech_req.command == 3: #WhoIsWho if text != "you are right" and text != "no": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: print("k") self.tts("OK") print("text_last:" + text_last) #name for t in people: if t in text_last: xm_speech_res.name.append(t) xm_speech_res.num += 1 for i in objectno: if i in text_last: xm_speech_res.object.append(i) xm_speech_res.num += 1 text_last = '' elif xm_speech_req.command == 4: #SPR #print('question: ' + text) file_handle_a = open( '/home/domistic/catkin_ws/src/xm_speech_for_linux/xm_speech/msc/bnf/a.txt', mode='rb') file_handle_b = open( '/home/domistic/catkin_ws/src/xm_speech_for_linux/xm_speech/msc/bnf/b.txt', mode='rb') a_contents = file_handle_a.readlines() b_contents = file_handle_b.readlines() t = text + '?\n' if t == a_contents[0]: print('finally') if t in a_contents: a_index = a_contents.index(t) b = b_contents[a_index] xm_speech_res.answer = b print('answer: ' + b) xm_speech_res.num = 1 self.tts(b) elif text == "number of people standing": xm_speech_res.num = 2 time.sleep(1) elif text == "number of people sitting": xm_speech_res.num = 3 time.sleep(1) elif text == "what is the number of the people who waving arms": xm_speech_res.num = 4 time.sleep(1) elif (xm_speech_req.command == 5): #help me carry if text != "you are right" and text != "no": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: if text_last.find("stop") >= 0: xm_speech_res.action.append('stop') elif text_last.find("follow") >= 0: xm_speech_res.action.append('follow') elif text_last.find("take") >= 0: xm_speech_res.action.append('take') if text_last.find("kitchen") >= 0: xm_speech_res.target.append('kitchen') elif text_last.find("livingroom") >= 0: xm_speech_res.target.append('livingroom') elif text_last.find("bedroom") >= 0: xm_speech_res.target.append('bedroom') elif text_last.find("diningroom") >= 0: xm_speech_res.target.append('diningroom') xm_speech_res.num = 1 print("text_last:" + text_last) text_last = '' if text == "you can stop here": xm_speech_res.action.append('stop') elif (xm_speech_req.command == 6): #shopping if text != "you are right" and text != "no": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: self.tts("OK") print("text_last:" + text_last) if text_last == "you can stop here": xm_speech_res.action.append('stop') xm_speech_res.num += 1 #follow elif text_last.find('follow') >= 0: xm_speech_res.action.append('follow') xm_speech_res.num += 1 else: for i in objectno: if i in text_last: xm_speech_res.object.append(i) xm_speech_res.num += 1 #方向 if text_last.find('right') >= 0: xm_speech_res.target.append('right') xm_speech_res.num += 1 if text_last.find('left') >= 0: xm_speech_res.target.append('left') xm_speech_res.num += 1 if text_last.find('front') >= 0: xm_speech_res.target.append('front') xm_speech_res.num += 1 text_last = ''
def Theona(): intro1, intro2, intro3 = sentence_generation('open') audio_play('boost.wav') os.system(intro1) train_sents = conll2000.chunked_sents('train.txt', chunk_types=['NP']) print('Training data... It will take 2-4 minutes.') chunker = ConsecutiveNPChunker(train_sents) os.system(intro2) # Theona Introduction audio_play('start_up.wav') os.system(intro3) # Step1. ASR # Use recognizer to record the speech. recorder = sr.Recognizer() starting = sentence_generation('hello') with sr.Microphone() as mike: print('Hello. Please speaking.') audio_play('pong.wav') os.system(starting) my_sound = recorder.listen(mike) print('Processing...') # Speech signal to text. Supported by google Speech api: Internet needs to be connected. tmp_words = recorder.recognize_google(my_sound) words = str(tmp_words) # test printing... print(words) # Step2. SLU # 1. find the specific places to users. #words = 'show me starbucks' # Tokenize the sentence. tokenized = word_tokenize(words) # Parsing the sentence to find out goal and entity clearly. pos_tagged = nltk.pos_tag(tokenized) chunk_words = chunker.parse(pos_tagged) reorder_words = tree_reconstruct(chunk_words) # Build the grammar for parsing. GOAL_FIND,ENTITY_PLACE = nonterminals('GOAL_FIND,ENTITY_PLACE') usr_goal = ENTITY_PLACE usr_find = GOAL_FIND VP,NP,O = nonterminals('VP,NP,O') grammar = CFG_grammar() rd_parser = RecursiveDescentParser(grammar) # Parsing the sentence. parsed_words = [] for parsing in rd_parser.parse(reorder_words): print(parsing) # Find GOAL and ENTITY for detect in parsing: if detect.label() == 'GOAL_FIND': usr_goal = detect.leaves()[0] if detect.label() == 'ENTITY_PLACE': usr_place = detect.leaves()[0] finding = sentence_generation('finding') finding = re.sub('<place>',usr_place,finding) audio_play('tone.wav') os.system(finding) # 2. Provide weather information to users. # Step3. DM # Collect information from the internet. # Location google_url = "https://www.google.co.kr/?gfe_rd=cr&ei=8YoTV-OdF8WL8AWGp5DgDg&gws_rd=ssl#newwindow=1&q=" daum_url = 'http://search.daum.net/search?w=tot&DA=YZR&t__nil_searchbox=btn&sug=&sugo=&sq=&o=&q=' # Connect to the internet to proceed the users' request: goal and entity. if usr_goal == 'find': # Searching in Daum. usr_request_url = daum_url + usr_place + '&tltm=1' request = requests.get(usr_request_url) soup = BeautifulSoup(request.content,'html.parser') # Searching in Google. #usr_request_url = google_url + usr_place #request = requests.get(usr_request_url) #soup = BeautifulSoup(request) # Collect information. # Find the closest 5 places around the location in which you start to request. all_data = soup.find_all('div',{'class','cont_place'}) first_data = all_data[0] # Address address_info = all_data[0].find_all('a',{'class','more_address'})[0].text # Phone Number phone_info = all_data[0].find_all('span',{'class','f_url'})[0].text # Location (map) map_info = all_data[0].find('a').get('href') # Weather # Step4. NLG # Generate an appropriate sentence. answer_text = NLG_transoformation('find') # Adjust the words if it is Korean. address_info = lang_adjust(address_info) # Substitude the markers to proper words answer_text = re.sub('<place>',usr_place,answer_text) answer_text = re.sub('<address>',address_info,answer_text) answer_text = re.sub('<phone>',phone_info,answer_text) # Step5. TTS audio_play('tone.wav') os.system('say ' + answer_text)
grammar_reduced = CFG.fromstring(""" S -> NS|VS NS -> N N VS -> V0 N | V1 N N | V2 N N N PP -> P N N -> 'الماء'|'الكريم'|'الكثير'|'الشجاع'|'الرسول'|'محمد'|'الطفل'|'العدو'|'الصاحب'|'الطالب'|'الدرس'|'الصلاة'|'الحق'|'المسافر'|'الطريق'|'الرجل'|'الخطأ'|'الأرض'|'كل'|'ثوب'|'جديد'|'درس'|'سهل'|'ماء'|'كريم'|'كثير'|'شجاع'|'رسول'|'طفل'|'عدو'|'صاحب'|'طالب'|'صلاة'|'حق'|'مسافر'|'طريق'|'رجل'|'خطأ'|'أرض'|'صاحبه'|'حقه'|'الامتحان' P -> 'في'|'الى'|'من'|'عن'|'على' V0 -> 'تفتح'|'فاض'|'ثار'|'هبت'|'جلس'|'ضاع'|'خرج'|'نام'|'وقعد'|'سافر'|'صدق' V1 -> 'طوى'|'أكل'|'بلل'|'زرع'|'أطفأ'|'يركب'|'يستجيب'|'حفظ'|'كتب'|'شاهد'|'قال' V2 -> 'يسقي'|'كسا'|'أعطى'|'ظن'|'حسب'|'جعل'|'خال'|'منح'|'منع'|'ألبس' """) #####RecursiveDescentParser###### tdParser = RecursiveDescentParser(grammar) def rdp(s): for w in tdParser.parse(s.split()): print (w) #####ShiftReduceParser##### srPraser = ShiftReduceParser(grammar_reduced,2) def srp(s): for w in srPraser.parse(s.split()): print (w) #####LeftCornerParser##### lcPraser = LeftCornerChartParser(grammar) def lcp(s):
def createTree2(self): grammar1 = self.makeGrammar() rd = RecursiveDescentParser(grammar1) for tree in rd.parse(self.myWords): print(tree)
def callback(self, text): """this function is to process the recognized text and send the processed information to Arduino which controls some devices""" global text_last global xm_speech_req global xm_speech_res flag = 0 if len(text) != 0 : if text[28] == '=': text = text[29:] elif text[29] == '=': text = text[30:] if text != "you are right" and text != "no" and len(text) != 0: text_last = text if type(text) == str and len(text) != 0: print("recognized : " + text) if xm_speech_req.command == 1:#Answer a question if text == "what is the capital of china": tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('Beijing') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","bei jing"]) xm_speech_res.num = 1 elif text == "how many hours in a day": tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('twenty four') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","24"]) xm_speech_res.num = 1 elif text == "how many season are there in one year": tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('four') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","4"]) xm_speech_res.num = 1 elif text == "how many seconds in one minute": tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('sixty') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","60"]) xm_speech_res.num = 1 elif text == "what is the world biggest island": tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('green land') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","green land"]) xm_speech_res.num = 1 elif text == "what is the biggest province of china": tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('xin jiang') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","xin jiang"]) xm_speech_res.num = 1 elif text == "how large is the area of china": tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('nine million and six hundred thousand saquare kilometers') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","nine million and six hundred thousand saquare kilometers"]) xm_speech_res.num = 1 elif text == "who was the first president of the usa": tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('george washington') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","george washington"]) xm_speech_res.num = 1 elif text == "what is china's national animal": tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('panda') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","panda"]) xm_speech_res.num = 1 elif text == "how many children did queen victoria have": tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('nine children') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","nine children"]) xm_speech_res.num = 1 elif text == "what was the former name of new york": tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('new amsterdam') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","new amsterdam"]) xm_speech_res.num = 1 elif xm_speech_req.command == 2:#GPSR if text != "you are right" and text != "no": if len(text_last) != 0: tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call(text_last) if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130",text_last]) tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('am i right?') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","am i right?"]) if text == "you are right": if len(text_last) != 0: tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('OK') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","OK"]) sen = text_last rd = RecursiveDescentParser(grammar) t = rd.parse_one(sen.split()) search(t) elif xm_speech_req.command == 3:#WhoIsWho if text != "you are right" and text != "no": if len(text_last) != 0: tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call(text_last) if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130",text_last]) tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('am i right?') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","am i right?"]) if text == "you are right": if len(text_last) != 0: tts_test = rospy.ServiceProxy('tts',xm_Speech_tts) tts_res = tts_test.call('OK') if tts_res.flag == 1: subprocess.call(["play","tts_sample.wav"]) else: subprocess.call(["espeak","-v","f3+en_us","-s","130","OK"]) print("text_last:"+text_last) #name if text_last.find('Tom') >= 0: xm_speech_res.name.append('Tom') xm_speech_res.num += 1 elif text_last.find('Paul') >= 0: xm_speech_res.name.append('Paul') xm_speech_res.num += 1 elif text_last.find('Green') >= 0: xm_speech_res.name.append('Green') xm_speech_res.num += 1 elif text_last.find('Kevin') >= 0: xm_speech_res.name.append('Kevin') xm_speech_res.num += 1 elif text_last.find('Tracy') >= 0: xm_speech_res.name.append('Tracy') xm_speech_res.num += 1 elif text_last.find('John') >= 0: xm_speech_res.name.append('John') xm_speech_res.num += 1 elif text_last.find('Angel') >= 0: xm_speech_res.name.append('Angel') xm_speech_res.num += 1 elif text_last.find('Jamie') >= 0: xm_speech_res.name.append('Jamie') xm_speech_res.num += 1 elif text_last.find('Fisher') >= 0: xm_speech_res.name.append('Fisher') xm_speech_res.num += 1 elif text_last.find('Shirley') >= 0: xm_speech_res.name.append('Shirley') xm_speech_res.num += 1 elif text_last.find('Robin') >= 0: xm_speech_res.name.append('Robin') xm_speech_res.num += 1 elif text_last.find('Daniel') >= 0: xm_speech_res.name.append('Daniel') xm_speech_res.num += 1 #object if text_last.find('paper') >= 0: xm_speech_res.object.append('paper') xm_speech_res.num += 1 elif text_last.find('noodles') >= 0: xm_speech_res.object.append('noodles') xm_speech_res.num += 1 elif text_last.find('pie') >= 0: xm_speech_res.object.append('pie') xm_speech_res.num += 1 elif text_last.find('tea') >= 0: xm_speech_res.object.append('tea') xm_speech_res.num += 1 elif text_last.find('biscuit') >= 0: xm_speech_res.object.append('biscuit') xm_speech_res.num += 1 elif text_last.find('chip') >= 0: xm_speech_res.object.append('chip') xm_speech_res.num += 1 elif text_last.find('soap') >= 0: xm_speech_res.object.append('soap') xm_speech_res.num += 1 elif text_last.find('shampoo') >= 0: xm_speech_res.object.append('shampoo') xm_speech_res.num += 1 elif text_last.find('milk tea') >= 0: xm_speech_res.object.append('milk tea') xm_speech_res.num += 1 elif text_last.find('water') >= 0: xm_speech_res.object.append('water') xm_speech_res.num += 1 elif text_last.find('toothpaste') >= 0: xm_speech_res.object.append('toothpaste') xm_speech_res.num += 1 elif text_last.find('coffee') >= 0: xm_speech_res.object.append('coffee') xm_speech_res.num += 1 elif text_last.find('cola') >= 0: xm_speech_res.object.append('cola') xm_speech_res.num += 1 elif text_last.find('red bull') >= 0: xm_speech_res.object.append('red bull') xm_speech_res.num += 1
def parser(plain_text, set_name={ 'name': ['x', 'y', 'z'], 'iten': ['a', 'b', 'c'], 'def': ['m', 'n', 'o'] }): #first we define the cfg that will generate all possible sentences for the language # based on the names of set's and ite's name the user have choosen # formatting the functions names line_grammar = "MEM_FUNC -> " counter = 0 while counter < len(set_name['def']): if counter == 0: formated_newstring = " '{a}' ".format( a=set_name['def'][counter]) else: formated_newstring = " | '{a}' ".format( a=set_name['def'][counter]) line_grammar += formated_newstring counter += 1 # formatting for set names line_grammar_0 = "NAME -> " counter = 0 while counter < len(set_name['name']): if counter == 0: formated_newstring = " '{a}' ".format( a=set_name['name'][counter]) else: formated_newstring = " | '{a}' ".format( a=set_name['name'][counter]) line_grammar_0 += formated_newstring counter += 1 line_grammar_1 = "NAME_I -> " counter = 0 while counter < len(set_name['iten']): if counter == 0: formated_newstring = " '{a}' ".format( a=set_name['iten'][counter]) else: formated_newstring = " | '{a}' ".format( a=set_name['iten'][counter]) line_grammar_1 += formated_newstring counter += 1 prime_cloudy_grammar = (((""" T -> COM_D END | INIT_A COM_A END | 'start_cloud{' | '}end_cloud' COM_D -> 'name::=' NAME '{' ITEN ';' MEM ';' lacune_1 lacune_2 ATTR -> ITEN ';' MEM ';' ITEN -> 'iten::=' NAME_I MEM -> 'membership::=' '(' MEM_FUNC ')' lacune_3 INIT_A -> 'active=>' '{' COM_A -> NAME Q NAME | NAME_I O NAME |'plot=>' CONJ 'using:' PLOT_S PLOT_S -> 'line' | 'venn' CONJ -> NAME Q NAME | NAME Q -> '-u' | '-i' | '-c' O -> 'in' | 'out' | '<m>' END -> '}end' """.replace('lacune_1', line_grammar_0)).replace( 'lacune_2', line_grammar_1)).replace('lacune_3', line_grammar)) # using the nltk's tool to generate , we create the formal cfg _cloudy_grammar = CFG.fromstring(prime_cloudy_grammar) #for sentences_test in generate(_cloudy_grammar,n=200): #print(' '.join(sentences_test)) # then we create the parser for this grammar cloudy_rd = RecursiveDescentParser(_cloudy_grammar) # split the input text into lines code_total = plain_text.split('\n') counter = 0 while counter < len(code_total): test = code_total[counter].split() # all code must start and end with specific sample of code as follows if counter == 0 and 'start_cloud{' in test: print("starting parsing") pass elif counter != 0: pass else: return 'start_cloud statment not found' # the end cloud statment determines where the parser will stop parsing the code if "}end_cloud" in test: print('end of parsing') return 'end of parsing' else: pass try: parsed_check = [] for parsed in cloudy_rd.parse(test): parsed_check.append(parsed) # if the length of the list which contain the parsed sentences is equal to 0 then # it means that the sentence wasnt well, so there's a some syntax error if len(parsed_check) != 0: pass else: return 'Syntax error on: (' + str( code_total[counter]) + ' ) at line : ' + str(counter) except: # if some lexical component not allowed is used then the system can recognize it faster return 'Lexical error on : (' + str( code_total[counter]) + ') at line : ' + str(counter) counter += 1
def callback(recognizer, audio): # received audio data, now we'll recognize it using xunfei Speech Recognition global xm_speech_req global xm_speech_res global text_last audio_wav_data = audio.get_wav_data() with open("microphone-results.wav", "wb") as f: f.write(audio.get_wav_data()) # print("audio_len : " + str(len(audio_wav_data))) # audio data length # print("Got it! Now to recognize it...") print("detecting...") if len(audio_wav_data) > 50000 and len(audio_wav_data) < 300000: print("pcm_len : " + str(len(audio_wav_data))) # audio data length rospy.wait_for_service('xm_speech') try: xm_speech = rospy.ServiceProxy('xm_speech', xm_Speech) resp1 = xm_speech(audio_wav_data) text = resp1.sr_text if text != "yes" and text != "no" and len(text) != 0: text_last = text if type(text) == str and len(text) != 0: print("recognized : " + text) if xm_speech_req.command == 0: if text == "what is the captital of china": subprocess.call([ "espeak", "-v", "f3+en_us", "-s", "130", "bei jing" ]) xm_speech_res.num = 1 elif text == "how many hours in a day": subprocess.call( ["espeak", "-v", "f3+en_us", "-s", "130", "24"]) xm_speech_res.num = 1 elif text == "how many season are there in one year": subprocess.call( ["espeak", "-v", "f3+en_us", "-s", "130", "4"]) xm_speech_res.num = 1 elif text == "how many seconds in one minute": subprocess.call( ["espeak", "-v", "f3+en_us", "-s", "130", "60"]) xm_speech_res.num = 1 elif text == "what is the world biggest island": subprocess.call([ "espeak", "-v", "f3+en_us", "-s", "130", "green land" ]) xm_speech_res.num = 1 elif text == "what is the biggest province of china": subprocess.call([ "espeak", "-v", "f3+en_us", "-s", "130", "xin jiang" ]) xm_speech_res.num = 1 elif text == "how large is the area of china": subprocess.call([ "espeak", "-v", "f3+en_us", "-s", "130", "nine million and six hundred thousand saquare kilometers" ]) xm_speech_res.num = 1 elif text == "who was the first president of the usa": subprocess.call([ "espeak", "-v", "f3+en_us", "-s", "130", "george washington" ]) xm_speech_res.num = 1 elif text == "what is china's national animal": subprocess.call( ["espeak", "-v", "f3+en_us", "-s", "130", "panda"]) xm_speech_res.num = 1 elif xm_speech_req.command == 1: #GPSR if text != "yes" and text != "no": if len(text_last) != 0: subprocess.call([ "espeak", "-v", "f3+en_us", "-s", "130", text_last ]) subprocess.call([ "espeak", "-v", "f3+en_us", "-s", "130", "yes or no" ]) if text == "yes": if len(text_last) != 0: sen = text_last rd = RecursiveDescentParser(grammar) t = rd.parse_one(sen.split()) search(t) elif xm_speech_req.command == 2: #HelpMeCarry if text != "yes" and text != "no": if len(text_last) != 0: subprocess.call([ "espeak", "-v", "f3+en_us", "-s", "130", text_last ]) subprocess.call([ "espeak", "-v", "f3+en_us", "-s", "130", "yes or no" ]) if text == "yes": if len(text_last) != 0: subprocess.call([ "espeak", "-v", "f3+en_us", "-s", "130", "OK" ]) print("text_last:" + text_last) if text_last == "follow me": xm_speech_res.action.append('follow') xm_speech_res.num += 1 elif text_last == "remember location": xm_speech_res.action.append('remember') xm_speech_res.num += 1 elif text_last == "take it": xm_speech_res.action.append('grasp') xm_speech_res.num += 1 elif text_last == "carry to the kitchen": xm_speech_res.target.append('kitchen') xm_speech_res.num += 1 elif text_last == "carry to the bedroom": xm_speech_res.target.append('bedroom') xm_speech_res.num += 1 elif text_last == "take to the dining room": xm_speech_res.target.append('diningroom') xm_speech_res.num += 1 elif text_last == "carry to the living room": xm_speech_res.target.append('livingroom') xm_speech_res.num += 1 else: pass except rospy.ServiceException, e: print "Service call failed: %s" % e
N -> 'tree' N -> 'fish' Adj -> 'angry' Adj -> 'frightened' Adj -> 'little' Adj -> 'tall' V -> 'chased' V -> 'said' V -> 'thought' V -> 'was' V -> 'put' P -> 'on' """) # In[4]: rd = RecursiveDescentParser(grammar1) sentence1 = 'mary saw a telescope in the park'.split() for t in rd.parse(sentence1): print(t) t.draw() # In[ ]: #before executing this line restart the kernel and clear all outputs rd = RecursiveDescentParser(grammar2) sentence2 = 'the bear chased the frightened squirrel'.split() for s in rd.parse(sentence2): print(s) s.draw()
def parse_word_tag(word, tag, sentence): rule_perphrase_c = """S -> DP | PP | AP | VP | CP | ADVP DP -> Dprime | Dprime QP | Dprime AP | Dprime CP Dprime -> D | NP | D NP | D CP NP -> Nprime | Nprime DP | Nprime PP | Nprime AP | Nprime VP | Nprime CP | Nprime ADVP Nprime -> N | N PP | PP N | N QP PP -> Pprime | Pprime ADVP | Pprime VP Pprime -> P | P DP AP -> Aprime | Aprime ADVP | Aprime AP | Aprime CP Aprime -> A | A DP VP -> Vprime | Vprime ADVP | Vprime DP | Vprime CP Vprime -> V | V DP | V PRN CP -> Cprime | Cprime VP | Cprime DP | Cprime NP | Cprime AP | Cprime QP | Cprime ADVP Cprime -> C | C Cprime QP -> Qprime | Qprime CP Qprime -> Q | Q NP ADVP -> ADVprime | ADVprime QP | ADVprime DP | ADVprime AP | ADVprime CP | ADVprime VP ADVprime -> ADV | ADV ADVP""" + '\n' rule_perphrase_b = """S -> DP | PP | AP | VP | CP | ADV DP -> Dprime | Dprime QP | Dprime AP | Dprime CP Dprime -> D | D NP | NP | D CP NP -> Nprime | Nprime DP | Nprime PP | Nprime AP | Nprime VP | Nprime CP Nprime -> N | N PP | PP N PP -> Pprime | Pprime ADV | Pprime VP Pprime -> P | P DP AP -> Aprime | Aprime ADV | Aprime AP | Aprime CP Aprime -> A | A DP VP -> Vprime | Vprime ADV| Vprime DP | Vprime CP Vprime -> V | V DP | V PRN CP -> Cprime | Cprime VP | Cprime DP | Cprime NP | Cprime QP | Cprime ADV Cprime -> C QP -> Qprime | Qprime CP Qprime -> Q""" + '\n' rule_perphrase_a = """S -> DP | PP | AP | VP | CP | ADV DP -> Dprime | Dprime QP | Dprime AP | Dprime CP Dprime -> D NP | NP | D CP NP -> Nprime | Nprime DP | Nprime PP | Nprime AP | Nprime VP | Nprime CP Nprime -> N | N PP | PP N PP -> Pprime | Pprime ADV | Pprime VP Pprime -> P | P DP AP -> Aprime | Aprime ADV Aprime -> A | A DP VP -> Vprime | Vprime ADV | Vprime DP Vprime -> V | V DP | V PRN | Vprime CP CP -> Cprime | Cprime VP | Cprime DP | Cprime NP | Cprime QP Cprime -> C """ + '\n' rule_test_c = """S -> DP Period | VP Period DP -> Dprime | Dprime QP | Dprime AP | Dprime CP Dprime -> D NP | NP | D CP NP -> Nprime | Nprime DP | Nprime PP | Nprime AP | Nprime VP | Nprime CP Nprime -> N | N PP | PP N PP -> Pprime | Pprime ADV | Pprime VP Pprime -> P | P DP AP -> Aprime | Aprime ADV Aprime -> A | A DP VP -> Vprime | Vprime ADV | Vprime DP Vprime -> V | V DP | V PRN | Vprime CP CP -> Cprime | Cprime VP | Cprime DP | Cprime NP Cprime -> C """ + '\n' rule_test = """S -> DP Period | VP Period DP -> Dprime | Dprime QP | Dprime AP Dprime -> D NP | NP NP -> Nprime | Nprime DP | Nprime PP | Nprime AP | Nprime VP Nprime -> N | N PP | PP N | N CP PP | PP CP N PP -> Pprime | Pprime ADV | Pprime VP Pprime -> P | P DP AP -> Aprime | Aprime ADV Aprime -> A | A DP VP -> Vprime | Vprime ADV | Vprime DP Vprime -> V | V DP | V PRN | Vprime CP CP -> Cprime | Cprime VP Cprime -> C | C VP | C NP """ + '\n' rule_test_b = """S -> DP VP DP -> Dprime QP | Dprime AP Dprime -> D NP PP -> Pprime ADV | Pprime VP Pprime -> P DP AP -> Aprime ADV Aprime -> A DP VP -> Vprime ADV | Vprime DP Vprime -> V DP | V PRN | V CP NP -> Nprime DP | Nprime PP | Nprime AP | Nprime VP Nprime -> N PP | PP N CP -> Cprime VP Cprime -> C VP | C NP """ + '\n' rule_abc = """S -> DP Period DP -> Dprime QP | Dprime AP Dprime -> D NP NP -> Nprime DP | Nprime PP | Nprime AP | Nprime VP Nprime -> N PP | PP N | N CP PP | PP CP N PP -> Pprime ADV | Pprime VP Pprime -> P DP AP -> Aprime ADV Aprime -> A DP VP -> Vprime ADV | Vprime DP Vprime -> V DP | V PRN | Vprime CP CP -> Cprime VP Cprime -> C VP | C NP """ + '\n' rule_test_b = """S -> DP VP DP -> Dprime QP | Dprime AP Dprime -> D NP PP -> Pprime ADV | Pprime VP Pprime -> P DP AP -> Aprime ADV Aprime -> A DP VP -> Vprime ADV | Vprime DP Vprime -> V DP | V PRN | V CP NP -> Nprime DP | Nprime PP | Nprime AP | Nprime VP Nprime -> N PP | PP N CP -> Cprime VP Cprime -> C VP | C NP """ + '\n' rule = """S -> NP VP Sym | VP NP Sym | VP Comma NP | NP Comma VP DP -> Dprime QP | Dprime AP Dprime -> D NP PP -> Pprime ADV | Pprime TP Pprime -> P DP AP -> Aprime ADV Aprime -> A DP VP -> Vprime ADV | Vprime DP Vprime -> V DP | V PRN | Vprime CP | V comma DP | V comma PRN | comma Vprime CP NP -> Nprime DP | Nprime PP | Nprime AP | Nprime VP Nprime -> N PP | PP N | N Comma PP | PP Comma N | N CP PP | PP CP N TP -> Tprime DP | Tprime Q Tprime -> Tum VP | Tin VP Tprime -> Tma AP Tprime -> Tna- PP Tprime -> Tmay VP Tprime -> Ttaga VP CP -> Cprime TP Cprime -> C TP | C NP | comma C TP | comma C NP""" + '\n' rule_backup = """S -> NP VP | VP NP DP -> Dprime QP | Dprime AP Dprime -> D NP PP -> Pprime ADV | Pprime TP Pprime -> P DP AP -> Aprime ADV Aprime -> A DP VP -> Vprime ADV | Vprime DP Vprime -> V DP | V PRN | Vprime CP NP -> Nprime DP | Nprime PP | Nprime AP | Nprime VP Nprime -> N PP | PP N | N CP PP | PP CP N TP -> Tprime DP | Tprime Q Tprime -> Tum VP | Tin VP Tprime -> Tma AP Tprime -> Tna- PP Tprime -> Tmay VP Tprime -> Ttaga VP CP -> Cprime TP Cprime -> C TP | C NP """ + '\n' i_tag = 0 tag_rule = [] sentence_word_tag = '' #print('tag length: ', len(tag)) while i_tag < len(tag): if "NN" in tag[i_tag]: tag_rule.append('N') elif "PR" in tag[i_tag]: tag_rule.append('N') elif "DT" in tag[i_tag]: tag_rule.append('D') elif "LM" in tag[i_tag]: tag_rule.append('C') elif "CCU" in tag[i_tag]: tag_rule.append('P') elif "CC" in tag[i_tag]: tag_rule.append('C') elif "VB" in tag[i_tag]: tag_rule.append('V') elif "JJ" in tag[i_tag]: tag_rule.append('A') elif "RB" in tag[i_tag]: tag_rule.append('ADV') elif "CD" in tag[i_tag]: tag_rule.append('Q') elif "TS" in tag[i_tag]: tag_rule.append('D') elif "FW" in tag[i_tag]: tag_rule.append('N') elif "PMP" in tag[i_tag]: tag_rule.append('Period') elif "PMC" in tag[i_tag]: tag_rule.append('C') elif "PM" in tag[i_tag]: tag_rule.append('Sym') i_word = 0 word_repeated = False while i_word < i_tag: if word[i_tag] == word[i_word]: word_repeated = True i_word += 1 #print('i_tag: ', i_tag) if not word_repeated: sentence_word_tag += tag_rule[i_tag] + " -> " + "'" + word[i_tag] + "'" + '\n' i_tag += 1 # DP = D' + QP | D' + AP # D' = D + NP # # PP = P' + ADV | P' + TP # P' = P + DP # # AP = A' + ADV # A' = A + DP # # VP = V' + ADV | V' + DP # V' = V + DP ¦ V + PRN ¦ V' + CP # # NP = N' + attribute phrase # N' = N + PP sentence_split = sentence.split() grammar = CFG.fromstring(rule_perphrase_c + sentence_word_tag) # #test uncomment to test english structure # grammar = CFG.fromstring(""" # S -> NP VP # PP -> P NP # NP -> 'the' N | N PP | 'the' N PP # VP -> V NP | V PP | V NP PP # N -> 'cat' # N -> 'dog' # N -> 'rug' # V -> 'chased' # V -> 'sat' # P -> 'in' # P -> 'on'""") # sentence_split = 'the cat chased the dog on the rug'.split() rd = RecursiveDescentParser(grammar) sr = ShiftReduceParser(grammar) chart_parser = nltk.ChartParser(grammar) earley_chart_parser = nltk.EarleyChartParser(grammar) chart_parser = earley_chart_parser print(tag_rule) parse_tree = [] print('Parse') for tree in chart_parser.parse(sentence_split): parse_tree.append(tree) if len(parse_tree) > 0: print(parse_tree[0]) else: print('NO TREE')
def callback(self, text): """this function is to process the recognized text and send the processed information to Arduino which controls some devices""" global text_last global xm_speech_req global xm_speech_res #if len(text) != 0 : # if text[28] == '=': # text = text[29:] # elif text[29] == '=': # text = text[30:] if text != "you are right" and text != "no" and len(text) != 0: text_last = text print(text) t = type(text) print(t) if type(text) == str and len(text) != 0: print("recognized : " + text) if xm_speech_req.command == 1: #Answer a question if text == "what is the capital of china": print("Beijing") self.tts("Beijing") xm_speech_res.num = 1 elif text == "how many hours in a day": print("twenty four") self.tts("twenty four") xm_speech_res.num = 1 elif text == "how many season are there in one year": print("four") self.tts("four") xm_speech_res.num = 1 elif text == "how many seconds in one minute": print("sixty") self.tts("sixty") xm_speech_res.num = 1 elif text == "what is the world biggest island": print("green land") self.tts("green land") xm_speech_res.num = 1 elif text == "what is the biggest province of china": print("xin jiang") self.tts("xin jiang") xm_speech_res.num = 1 elif text == "how large is the area of china": print( "nine million and six hundred thousand saquare kilometers" ) self.tts( "nine million and six hundred thousand saquare kilometers" ) xm_speech_res.num = 1 elif text == "who was the first president of the usa": print("george washington") self.tts("george washington") xm_speech_res.num = 1 elif text == "what is china's national animal": print("panda") self.tts("panda") xm_speech_res.num = 1 elif text == "how many children did queen victoria have": print("nine children") self.tts("nine children") xm_speech_res.num = 1 elif text == "what was the former name of new york": print("new amsterdam") self.tts("new amsterdam") xm_speech_res.num = 1 elif xm_speech_req.command == 2: #GPSR if text != "you are right" and text != "no" and text != "stop here now": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: self.tts("OK") sen = text_last rd = RecursiveDescentParser(grammar) t = rd.parse_one(sen.split()) search(t) elif text == "stop here now": xm_speech_res.action.append('stop') xm_speech_res.num = 1 self.tts("OK") elif xm_speech_req.command == 3: #WhoIsWho if text != "you are right" and text != "no": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: self.tts("OK") print("text_last:" + text_last) #name if text_last.find('Tom') >= 0: xm_speech_res.name.append('Tom') xm_speech_res.num += 1 elif text_last.find('Micheal') >= 0: xm_speech_res.name.append('Micheal') xm_speech_res.num += 1 elif text_last.find('Jack') >= 0: xm_speech_res.name.append('Jack') xm_speech_res.num += 1 elif text_last.find('Kevin') >= 0: xm_speech_res.name.append('Kevin') xm_speech_res.num += 1 elif text_last.find('Rose') >= 0: xm_speech_res.name.append('Rose') xm_speech_res.num += 1 elif text_last.find('John') >= 0: xm_speech_res.name.append('John') xm_speech_res.num += 1 elif text_last.find('Mary') >= 0: xm_speech_res.name.append('Mary') xm_speech_res.num += 1 elif text_last.find('Fisher') >= 0: xm_speech_res.name.append('Fisher') xm_speech_res.num += 1 elif text_last.find('Adam') >= 0: xm_speech_res.name.append('Adam') xm_speech_res.num += 1 elif text_last.find('Daniel') >= 0: xm_speech_res.name.append('Daniel') xm_speech_res.num += 1 #object if text_last.find('ice tea') >= 0: xm_speech_res.object.append('ice tea') xm_speech_res.num += 1 elif text_last.find('safeguard') >= 0: xm_speech_res.object.append('safeguard') xm_speech_res.num += 1 elif text_last.find('napkin') >= 0: xm_speech_res.object.append('napkin') xm_speech_res.num += 1 elif text_last.find('chips') >= 0: xm_speech_res.object.append('chips') xm_speech_res.num += 1 elif text_last.find('laoganma') >= 0: xm_speech_res.object.append('laoganma') xm_speech_res.num += 1 elif text_last.find('porridge') >= 0: xm_speech_res.object.append('porridge') xm_speech_res.num += 1 elif text_last.find('milk') >= 0: xm_speech_res.object.append('milk') xm_speech_res.num += 1 elif text_last.find('water') >= 0: xm_speech_res.object.append('water') xm_speech_res.num += 1 elif text_last.find('cola') >= 0: xm_speech_res.object.append('cola') xm_speech_res.num += 1 elif text_last.find('sprite') >= 0: xm_speech_res.object.append('sprite') xm_speech_res.num += 1 elif xm_speech_req.command == 4: #SPR print(text) #ArenaQ if text == "How many bookshelf are in the living room": xm_speech_res.num = 1 self.tts("three") elif text == "In which room is the fridge": xm_speech_res.num = 1 self.tts("kitchen") elif text == "How many doors has the Arena": xm_speech_res.num = 1 self.tts("2") elif text == "In which room is the dining table": xm_speech_res.num = 1 tself.tts("diningroom") elif text == "Where is located the bed": xm_speech_res.num = 1 self.tts("bedroom") elif text == "How many fireplace are in the bedroom": xm_speech_res.num = 1 self.tts("zero") elif text == "In which room is the sofa": xm_speech_res.num = 1 self.tts("livingroom") elif text == "Where is located the fridge": xm_speech_res.num = 1 self.tts("kitchen") elif text == "Where is located the cupboard": xm_speech_res.num = 1 self.tts("diningroom") elif text == "In which room is the bookshelf": xm_speech_res.num = 1 self.tts("bedroom") #ObjectQ elif text == "how many hours in a day": xm_speech_res.num = 1 self.tts("twenty four") elif text == "how many season are there in one year": xm_speech_res.num = 1 self.tts("four") elif text == "how many seconds in one minute": xm_speech_res.num = 1 self.tts("sixty") elif text == "what is the world biggest island": xm_speech_res.num = 1 self.tts("green land") elif text == "what is the biggest province of china": xm_speech_res.num = 1 self.tts("shin junk") elif text == "how large is the area of china": xm_speech_res.num = 1 self.tts( "Nine million and six hundred thousand saquare kilometers" ) elif text == "Who was the first president of the USA": xm_speech_res.num = 1 self.tts("George Washington") elif text == "What is China's national animal": xm_speech_res.num = 1 self.tts("panda") elif text == "How many children did Queen Victoria have": xm_speech_res.num = 1 self.tts("Nine children") elif text == "What was the former name of New York": xm_speech_res.num = 1 self.tts("New Amsterdam") #PredefinedQ elif text == "How many people live in the Japan": xm_speech_res.num = 1 self.tts("A little over 80 million") elif text == "what is the capital of the united states": xm_speech_res.num = 1 self.tts("Washington DC") elif text == "what day is today": xm_speech_res.num = 1 self.tts("Saturday") elif text == "What city is the capital of the Japan": xm_speech_res.num = 1 self.tts("Tokyo") elif text == "Where does the term computer bug come from": xm_speech_res.num = 1 self.tts("From a moth trapped in a relay") elif text == "When was invented the B programming language": xm_speech_res.num = 1 self.tts("B was developed circa 1969 at Bell Labs") elif text == "Who invented the C programming language": xm_speech_res.num = 1 self.tts("Ken Thompson and Dennis Ritchie") elif text == "What is the highest point in Japan": xm_speech_res.num = 1 self.tts("The highest point in Japan is Mount Fuji") elif text == "What are the colours of the Japanese flag": xm_speech_res.num = 1 self.tts( "Japanese flag is a red circle centred over white") elif text == "Who invented the first compiler": xm_speech_res.num = 1 self.tts("Grace Brewster Murray Hopper invented it") #CrowdOperatorQ '''elif text == "number of male people sitting": xm_speech_res.num = 2 time.sleep(1) elif text == "number of female people sitting": xm_speech_res.num = 3 time.sleep(1) elif text == "number of male people standing": xm_speech_res.num = 4 time.sleep(1) elif text == "number of female people standing": xm_speech_res.num = 5 time.sleep(1) elif text == "number of people standing": xm_speech_res.num = 6 time.sleep(1) elif text == "number of people sitting": xm_speech_res.num = 7 time.sleep(1) ''' if text == 'number of male people': xm_speech_res.num = 2 time.sleep(1) if text == 'number of female people': xm_speech_res.num = 3 time.sleep(1) if text == 'number of people': xm_speech_res.num = 4 time.sleep(1) elif (xm_speech_req.command == 5): #help me carry if text != "you are right" and text != "no": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: if text_last.find("stop") >= 0: xm_speech_res.action.append('stop') elif text_last.find("follow") >= 0: xm_speech_res.action.append('follow') elif text_last.find("take") >= 0: xm_speech_res.action.append('take') if text_last.find("kitchen") >= 0: xm_speech_res.target.append('kitchen') elif text_last.find("livingroom") >= 0: xm_speech_res.target.append('livingroom') elif text_last.find("bedroom") >= 0: xm_speech_res.target.append('bedroom') elif text_last.find("diningroom") >= 0: xm_speech_res.target.append('diningroom') if text_last.find("bag") >= 0: xm_speech_res.object.append('bag') self.tts("OK") xm_speech_res.num = 1 print("text_last:" + text_last) elif (xm_speech_req.command == 6): #shopping if text != "you are right" and text != "no": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: self.tts("OK") print("text_last:" + text_last) num = 0 #follow if text_last.find('follow') >= 0: xm_speech_res.action.append('follow') num += 1 #object if text_last.find('ice tea') >= 0: xm_speech_res.object.append('ice tea') num += 1 if text_last.find('safeguard') >= 0: xm_speech_res.object.append('safeguard') num += 1 if text_last.find('napkin') >= 0: xm_speech_res.object.append('napkin') num += 1 if text_last.find('chips') >= 0: xm_speech_res.object.append('chips') num += 1 if text_last.find('laoganma') >= 0: xm_speech_res.object.append('laoganma') num += 1 if text_last.find('milk') >= 0: xm_speech_res.object.append('milk') num += 1 if text_last.find('water') >= 0: xm_speech_res.object.append('water') num += 1 if text_last.find('cola') >= 0: xm_speech_res.object.append('cola') num += 1 if text_last.find('porridge') >= 0: xm_speech_res.object.append('porridge') num += 1 if text_last.find('sprite') >= 0: xm_speech_res.object.append('sprite') num += 1 #方向 if text_last.find('right') >= 0: xm_speech_res.target.append('right') num += 1 if text_last.find('left') >= 0: xm_speech_res.target.append('left') num += 1 xm_speech_res.num = num
grammar = CFG.fromstring(""" VP -> GOAL_FIND O ENTITY_PLACE | GOAL_FIND ENTITY_PLACE NP -> P ENTITY_PLACE | ENTITY_PLACE GOAL_FIND -> 'find' GOAL_FIND -> 'show' GOAL_FIND -> 'tell' O -> 'me' P -> 'in' ENTITY_PLACE -> 'starbucks' ENTITY_PLACE -> 'Starbucks' ENTITY_PLACE -> 'Coffee Bean' ENTITY_PLACE -> 'Coffeebean' """) rd_parser = RecursiveDescentParser(grammar) # Parsing the sentence. parsed_words = [] for parsing in rd_parser.parse(tokenized): print(parsing) # Find GOAL and ENTITY for detect in parsing: if detect.label() == 'GOAL_FIND': usr_goal = detect.leaves()[0] if detect.label() == 'ENTITY_PLACE': usr_place = detect.leaves()[0] finding = sentence_generation('finding') finding = re.sub('<place>',usr_place,finding)
def callback(self, text): """this function is to process the recognized text and send the processed information to Arduino which controls some devices""" global text_last global xm_speech_req global xm_speech_res global sentences #if len(text) != 0 : # if text[28] == '=': # text = text[29:] # elif text[29] == '=': # text = text[30:] if text != "you are right" and text != "no" and len(text) != 0: text_last = text if type(text) == str and len(text) != 0: print("recognized : " + text) if xm_speech_req.command == 1: #Answer a question print('question: ' + text) file_handle_c = open( '/home/moonknight/catkin_ws/src/xm_speech/msc/bnf/c.txt', mode='rb') file_handle_d = open( '/home/moonknight/catkin_ws/src/xm_speech/msc/bnf/d.txt', mode='rb') c_contents = file_handle_c.readlines() d_contents = file_handle_d.readlines() t = '\"' + text + '\"' + '\n' if t in c_contents: c_index = c_contents.index(t) d = d_contents[c_index] print('answer: ' + d) xm_speech_res.num = 1 self.tts(d) elif xm_speech_req.command == 2: #GPSR if text != "you are right" and text != "no" and text != "you can stop here": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: self.tts("OK") sen = text_last for t in kitchen: sen = sen.replace(t, "kitchen") for t in livingroom: sen = sen.replace(t, "livingroom") for t in bedroom: sen = sen.replace(t, "bedroom") for t in hallway: sen = sen.replace(t, "hallway") if "bed" in sen and "bedroom" not in sen: sen = sen.replace("bed", "bedroom") if "tell the day of the month" in sen: sentences = "tell the day of the month" sen = sen.replace("tell the day of the month", "sentence") if "tell the day of the week" in sen: sentences = "tell the day of the week" sen = sen.replace("tell the day of the week", "sentence") if "tell the date" in sen: sentences = "tell the date" sen = sen.replace("tell the date", "sentence") for i in double_words: sen = sen.replace(i.replace("_", " "), i) print(sen) rd = RecursiveDescentParser(grammar) t = rd.parse_one(sen.split()) print(t) search(t) text_last = '' elif xm_speech_req.command == 3: #WhoIsWho if text != "you are right" and text != "no": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: self.tts("OK") print("text_last:" + text_last) #name for t in people: if t in text_last: xm_speech_res.name.append(t) xm_speech_res.num += 1 #object if text_last.find('ice tea') >= 0: xm_speech_res.object.append('ice_tea') xm_speech_res.num += 1 elif text_last.find('water') >= 0: xm_speech_res.object.append('water') xm_speech_res.num += 1 elif text_last.find('cracker') >= 0: xm_speech_res.object.append('cracker') xm_speech_res.num += 1 elif text_last.find('duck') >= 0: xm_speech_res.object.append('duck') xm_speech_res.num += 1 elif text_last.find('orange juice') >= 0: xm_speech_res.object.append('orange_juice') xm_speech_res.num += 1 elif text_last.find('porridge') >= 0: xm_speech_res.object.append('porridge') xm_speech_res.num += 1 elif text_last.find('milk') >= 0: xm_speech_res.object.append('milk') xm_speech_res.num += 1 elif text_last.find('milk tea') >= 0: xm_speech_res.object.append('milk_tea') xm_speech_res.num += 1 elif text_last.find('tissue') >= 0: xm_speech_res.object.append('tissue') xm_speech_res.num += 1 elif text_last.find('cola') >= 0: xm_speech_res.object.append('cola') xm_speech_res.num += 1 elif text_last.find('sprite') >= 0: xm_speech_res.object.append('sprite') xm_speech_res.num += 1 elif text_last.find('powder juice') >= 0: xm_speech_res.object.append('powder_juice') xm_speech_res.num += 1 elif text_last.find('green tea') >= 0: xm_speech_res.object.append('green_tea') xm_speech_res.num += 1 elif text_last.find('gum') >= 0: xm_speech_res.object.append('gum') xm_speech_res.num += 1 elif text_last.find('fanta') >= 0: xm_speech_res.object.append('fanta') xm_speech_res.num += 1 elif text_last.find('french fries') >= 0: xm_speech_res.object.append('french_fries') xm_speech_res.num += 1 elif text_last.find('yoghurt') >= 0: xm_speech_res.object.append('yoghurt') xm_speech_res.num += 1 elif text_last.find('noodle') >= 0: xm_speech_res.object.append('noodle') xm_speech_res.num += 1 elif text_last.find('crisp') >= 0: xm_speech_res.object.append('crisp') xm_speech_res.num += 1 elif text_last.find('herbal tea') >= 0: xm_speech_res.object.append('herbal_tea') xm_speech_res.num += 1 elif text_last.find('coconut') >= 0: xm_speech_res.object.append('coconut') xm_speech_res.num += 1 elif text_last.find('peartea') >= 0: xm_speech_res.object.append('pear_tea') xm_speech_res.num += 1 elif text_last.find('tea') >= 0: xm_speech_res.object.append('tea') xm_speech_res.num += 1 elif text_last.find('melon seeds') >= 0: xm_speech_res.object.append('melon_seeds') xm_speech_res.num += 1 elif text_last.find('redbull') >= 0: xm_speech_res.object.append('redbull') xm_speech_res.num += 1 elif text_last.find('toothpaste') >= 0: xm_speech_res.object.append('toothpaste') xm_speech_res.num += 1 elif text_last.find('grapejuice') >= 0: xm_speech_res.object.append('grape_juice') xm_speech_res.num += 1 elif text_last.find('soap') >= 0: xm_speech_res.object.append('soap') xm_speech_res.num += 1 elif text_last.find('shampoo') >= 0: xm_speech_res.object.append('shampoo') xm_speech_res.num += 1 text_last = '' elif xm_speech_req.command == 4: #SPR print('question: ' + text) file_handle_a = open( '/home/domistic/catkin_ws/src/xm_speech_for_linux/xm_speech/msc/bnf/a.txt', mode='rb') file_handle_b = open( '/home/domistic/catkin_ws/src/xm_speech_for_linux/xm_speech/msc/bnf/b.txt', mode='rb') a_contents = file_handle_a.readlines() b_contents = file_handle_b.readlines() t = text + '?\n' if t == a_contents[0]: print('finally') if t in a_contents: a_index = a_contents.index(t) b = b_contents[a_index] xm_speech_res.answer = b print('answer: ' + b) xm_speech_res.num = 1 self.tts(b) elif text == "number of people standing": xm_speech_res.num = 2 time.sleep(1) elif text == "number of people sitting": xm_speech_res.num = 3 time.sleep(1) elif text == "what is the number of the people who waving arms": xm_speech_res.num = 4 time.sleep(1) elif (xm_speech_req.command == 5): #help me carry if text != "you are right" and text != "no": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: if text_last.find("stop") >= 0: xm_speech_res.action.append('stop') elif text_last.find("follow") >= 0: xm_speech_res.action.append('follow') elif text_last.find("take") >= 0: xm_speech_res.action.append('take') if text_last.find("kitchen") >= 0: xm_speech_res.target.append('kitchen') elif text_last.find("livingroom") >= 0: xm_speech_res.target.append('livingroom') elif text_last.find("bedroom") >= 0: xm_speech_res.target.append('bedroom') elif text_last.find("diningroom") >= 0: xm_speech_res.target.append('diningroom') xm_speech_res.num = 1 print("text_last:" + text_last) text_last = '' if text == "you can stop here": xm_speech_res.action.append('stop') elif (xm_speech_req.command == 6): #shopping if text != "you are right" and text != "no": if len(text_last) != 0: self.tts(text_last) self.tts("am i right") if text == "you are right": if len(text_last) != 0: self.tts("OK") print("text_last:" + text_last) if text_last == "you can stop here": xm_speech_res.action.append('stop') xm_speech_res.num += 1 #follow elif text_last.find('follow') >= 0: xm_speech_res.action.append('follow') xm_speech_res.num += 1 #object elif text_last.find('ice tea') >= 0: xm_speech_res.object.append('ice_tea') xm_speech_res.num += 1 elif text_last.find('redbull') >= 0: xm_speech_res.object.append('redbull') xm_speech_res.num += 1 elif text_last.find('toothpaste') >= 0: xm_speech_res.object.append('toothpaste') xm_speech_res.num += 1 elif text_last.find(' Chips ') >= 0: xm_speech_res.object.append(' Chips ') xm_speech_res.num += 1 elif text_last.find('laoganma') >= 0: xm_speech_res.object.append('laoganma') xm_speech_res.num += 1 elif text_last.find('milk tea') >= 0: xm_speech_res.object.append('milk_tea') xm_speech_res.num += 1 elif text_last.find('water') >= 0: xm_speech_res.object.append('water') xm_speech_res.num += 1 elif text_last.find('cola') >= 0: xm_speech_res.object.append('cola') xm_speech_res.num += 1 elif text_last.find('porridge') >= 0: xm_speech_res.object.append('porridge') xm_speech_res.num += 1 elif text_last.find('sprite') >= 0: xm_speech_res.object.append('sprite') xm_speech_res.num += 1 elif text_last.find('shampoo') >= 0: xm_speech_res.object.append('shampoo') xm_speech_res.num += 1 elif text_last.find('water') >= 0: xm_speech_res.object.append('water') xm_speech_res.num += 1 elif text_last.find('cracker') >= 0: xm_speech_res.object.append('cracker') xm_speech_res.num += 1 elif text_last.find('duck') >= 0: xm_speech_res.object.append('duck') xm_speech_res.num += 1 elif text_last.find('orange juice') >= 0: xm_speech_res.object.append('orange_juice') xm_speech_res.num += 1 elif text_last.find('porridge') >= 0: xm_speech_res.object.append('porridge') xm_speech_res.num += 1 elif text_last.find('milk') >= 0: xm_speech_res.object.append('milk') xm_speech_res.num += 1 elif text_last.find('tissue') >= 0: xm_speech_res.object.append('tissue') xm_speech_res.num += 1 elif text_last.find('cola') >= 0: xm_speech_res.object.append('cola') xm_speech_res.num += 1 elif text_last.find('sprite') >= 0: xm_speech_res.object.append('sprite') xm_speech_res.num += 1 elif text_last.find('powder juice') >= 0: xm_speech_res.object.append('powder_juice') xm_speech_res.num += 1 elif text_last.find('green tea') >= 0: xm_speech_res.object.append('green_tea') xm_speech_res.num += 1 elif text_last.find('gum') >= 0: xm_speech_res.object.append('gum') xm_speech_res.num += 1 elif text_last.find('fanta') >= 0: xm_speech_res.object.append('fanta') xm_speech_res.num += 1 elif text_last.find('french fries') >= 0: xm_speech_res.object.append('french_fries') xm_speech_res.num += 1 elif text_last.find('yoghurt') >= 0: xm_speech_res.object.append('yoghurt') xm_speech_res.num += 1 elif text_last.find('noodle') >= 0: xm_speech_res.object.append('noodle') xm_speech_res.num += 1 elif text_last.find('crisp') >= 0: xm_speech_res.object.append('crisp') xm_speech_res.num += 1 elif text_last.find('herbal tea') >= 0: xm_speech_res.object.append('herbal_tea') xm_speech_res.num += 1 elif text_last.find('coconut') >= 0: xm_speech_res.object.append('coconut') xm_speech_res.num += 1 elif text_last.find('pear tea') >= 0: xm_speech_res.object.append('pear_tea') xm_speech_res.num += 1 elif text_last.find('tea') >= 0: xm_speech_res.object.append('tea') xm_speech_res.num += 1 elif text_last.find('melon seeds') >= 0: xm_speech_res.object.append('melon_seeds') xm_speech_res.num += 1 elif text_last.find('grape juice') >= 0: xm_speech_res.object.append('grape_juice') xm_speech_res.num += 1 elif text_last.find('soap') >= 0: xm_speech_res.object.append('soap') xm_speech_res.num += 1 #方向 if text_last.find('right') >= 0: xm_speech_res.target.append('right') xm_speech_res.num += 1 if text_last.find('left') >= 0: xm_speech_res.target.append('left') xm_speech_res.num += 1 if text_last.find('front') >= 0: xm_speech_res.target.append('front') xm_speech_res.num += 1 text_last = ''
from nltk.corpus import treebank from nltk import PCFG, CFG cfg_grammar = CFG.fromstring(""" S -> NP VP NP -> ART N | N N | N | NP PP VP -> V | V NP | V NP PP PP -> P NP ART -> 'a' N -> 'flower' | 'a' | 'blooms' V -> 'blooms' | 'flower' """) pcfg_grammar = PCFG.fromstring(""" S -> NP VP [1.0] NP -> ART N [0.53] | N N [0.09] | N [0.14] | NP PP [0.24] VP -> V [0.386] | V NP [0.393] | V NP PP [0.22] PP -> P NP [1.0] ART -> 'a' [1.0] N -> 'flower' [0.8] | 'a' [0.1] | 'blooms' [0.1] V -> 'blooms' [0.8] | 'flower' [0.2] """) from nltk.parse import RecursiveDescentParser print(cfg_grammar) rd = RecursiveDescentParser(pcfg_grammar) text = "a flower blooms".split() for t in rd.parse(text): print(t) #rd.draw()