def __init__(self): self.session = qi.Session() try: self.session.connect("tcp://" + Settings.ROBOT_IP + ":" + str(Settings.PORT)) except RuntimeError: print( "Can't connect to Naoqi at ip \"130.239.182.9\" on port 9559.\n" "Please check your script arguments. Run with -h option for help." ) self.speech = self.session.service("ALAnimatedSpeech") #self.microphone = SystemMicrophone(16000, 1) self.microphone = PepperMicrophone(self.session) self.utterance = Utterance(self.microphone, self.on_utterance) self.recognition = GoogleASR() self.utterance.start() Thread(target=self.voice_activation).start() print("\nBooted Echo App")
def test_parse(self): tcs = [ '(강남)[placeName]에서 출발하는 (출근)[busType] 버스 알려줘', '(황골 마을)[placeName] (퇴근)[busType] 버스 알려줘' ] vocabs = { 'text': Vocabulary(), 'named_entity': Vocabulary(), 'label': Vocabulary() } named_entity = NamedEntity() for tc in tcs: utterance = Utterance.parse(tc, vocabs, named_entity) print(utterance)
class EchoTest(): def __init__(self): self.session = qi.Session() try: self.session.connect("tcp://" + Settings.ROBOT_IP + ":" + str(Settings.PORT)) except RuntimeError: print( "Can't connect to Naoqi at ip \"130.239.182.9\" on port 9559.\n" "Please check your script arguments. Run with -h option for help." ) self.speech = self.session.service("ALAnimatedSpeech") #self.microphone = SystemMicrophone(16000, 1) self.microphone = PepperMicrophone(self.session) self.utterance = Utterance(self.microphone, self.on_utterance) self.recognition = GoogleASR() self.utterance.start() Thread(target=self.voice_activation).start() print("\nBooted Echo App") def voice_activation(self): while True: value = float(self.utterance.activation()) #self.led.set((value if value > self.utterance.VOICE_THRESHOLD else 0, 0, value)) #print('voice activation:'+str(value)) sleep(0.2) def on_utterance(self, audio): hypotheses = self.recognition.transcribe(audio) if hypotheses: transcript, confidence = hypotheses[0] print(transcript + " [{:3.0%}]".format(confidence)) self.say(transcript) def say(self, text): self.utterance.stop() self.speech.say(text) self.utterance.start()
def parse_speech_data(self, seg, el): utter_list = [] text = el.tail.strip() if text: # split up separate lines speaker_utters = re.split('\s*\n\s*', text) # each line is treated as a separate utterance for i in range(len(speaker_utters)): # split at the '.' operator, if present. # Transcribers use this to "split apart" segments that # LENA has mistakenly put together. if (re.search(r'\s*\.\s*', speaker_utters[i]) is not None): have_multi_utters = True multi_utters = re.split(r'\s*\.\s*', speaker_utters[i]) for j in range(len(multi_utters)): utter = Utterance() utter.seg = seg utter.is_dot_split = have_multi_utters self.total_utters += 1 # First line has the speaker indicated by LENA. # Subsequent lines are considered to be other speakers. if i == 0: self.assign_speaker(el, utter) self._assign_utter_attribs(utter, multi_utters[j], remove_bad_trans_codes) utter_list.append(utter) # If the <sync> tag was not transcribed, just create an empty Utterance for it else: utter = Utterance() utter.seg = seg if seg.speakers: #assume first speaker for now... utter.speaker = seg.speakers[0] self.total_utters += 1 utter_list.append(utter) return utter_list
def fn(interaction_example): keep = False raw_utterances = interaction_example["interaction"] identifier = interaction_example["id"] snippet_bank = [] utterance_examples = [] anon_tok_to_ent = {} for utterance in raw_utterances: available_snippets = [ snippet for snippet in snippet_bank if snippet.index <= 1 ] proc_utterance = Utterance(utterance, available_snippets, nl_to_sql_dict, parameters, anon_tok_to_ent, anonymizer) keep_utterance = proc_utterance.keep if keep_utterance: keep = True utterance_examples.append(proc_utterance) # Update the snippet bank, and age each snippet in it. if parameters.use_snippets: snippets = sql_util.get_subtrees( proc_utterance.anonymized_gold_query, proc_utterance.available_snippets) for snippet in snippets: snippet.assign_id(len(snippet_bank)) snippet_bank.append(snippet) for snippet in snippet_bank: snippet.increase_age() interaction = Interaction(utterance_examples, snippet_bank, anon_tok_to_ent, identifier, parameters) return interaction, keep
def from_local(cls, path: str): with open(os.path.join(path, 'config.json'), encoding='utf-8') as fp: config = json.loads(fp.read()) named_entity = NamedEntity() for path, _, files in os.walk(path): for file in files: name, extension = os.path.splitext(file) if extension == '.ne': with open(os.path.join(path, file), encoding='utf-8') as fp: words = set( filter( None, map(lambda line: line.replace(' ', '').strip(), fp.readlines()))) named_entity.push(name, words) vocabs = { 'text': Vocabulary(), 'named_entity': Vocabulary(), 'label': Vocabulary() } with open(os.path.join(path, 'training'), encoding='utf-8') as fp: utterances = set( map( lambda line: Utterance.parse(line, vocabs, named_entity), set( filter(None, map(lambda line: line.strip(), fp.readlines()))))) max_length = cls.__get_max_text_length(utterances) return Bot(name=config['name'], submission_time=datetime.datetime.utcnow(), max_text_length=max_length, max_named_entity_size=3, hyper_params=config['hyper_params'], utterances=utterances, vocabs=vocabs, named_entity=named_entity)
def fetch(cls, bot_name: str): client = MongoClient(CONFIG['default']['mongodb']) db = client['bot-nlu'] bot = db.bot.find_one({'name': bot_name}) if not bot: raise AttributeError(bot_name + ' not found') vocabs = pickle.loads(bot['vocabs']) named_entity = pickle.loads(bot['named_entities']) utterances = set( Utterance.parse(utterance, vocabs, named_entity) for utterance in bot['utterances']) return Bot(name=bot['name'], submission_time=bot['submission_time'], max_text_length=bot['max_text_length'], max_named_entity_size=bot['max_named_entity_size'], hyper_params=bot['hyper_params'], utterances=utterances, vocabs=vocabs, named_entity=named_entity)
def predict(self, text: str): model_path = self.__get_model_path() if not os.path.exists(model_path): raise EnvironmentError('Should be trained.') utterance = Utterance.parse(text, self.__vocabs, self.__named_entity) dataset = DatasetGenerator.generate(self.__max_text_length, self.__max_named_entity_size, {utterance}) dataset = dataset.batch(1) predictions = self.__slot_tagger.predict(dataset) prediction = predictions[0][:len(utterance)] labels = list( map(lambda num: self.__vocabs['label'].restore(num), prediction)) slots = [] for token, label in zip(utterance.tokens, labels): if label.startswith('b-'): slots.append({ 'text': [token], 'slot': label.replace('b-', '', 1) }) elif label.startswith('i-'): slot = label.replace('i-', '', 1) if len(slots) > 0 and slots[-1]['slot'] == slot: slots[-1]['text'].append(token) for slot in slots: if len(slot['text']) == 1: slot['text'] = slot['text'][0]['text'] else: start = slot['text'][0]['span'].lower end = slot['text'][-1]['span'].upper slot['text'] = utterance.plain_text[start:end] return slots
def get_data(row1, row2, vocabulary_gloss, model, fw, precondition): """ Pre-process data, creates embedding, part of speech, etc... of row1 and row2, computes linguistic similarities between both utterances, return these measures and complementary information in a dictionnary. :param row1: A row of a pre-processed Dataframe, the row contain all useful informations about an utterance :type row1: pandas.Series :param row2: A row of a pre-processed Dataframe, the row contain all useful informations about an utterance :type row2: pandas.Series :param vocabulary_gloss: List of five dictionnaries mapping words to their age of acquisition, according to different thresholds :type vocabulary_gloss: list of dict{str:int} :param model: The spacy model that will be used to create embeddings of utterances, tokenise them and create parts of speech :type model: spacy.lang.en.English (or other languages) :param fw: The list of function words of the specified language, retrieved from Spacy :type fw: set :param precondition: Can have three values: "normal", "rand_in" and "rand_ex", define if row1 and row2 are respectively strictly consecutives, or at least in the same transcript, or finally chosen at random in the whole CHILDES corpus :type precondition: str :returns: A dictionnary containing all the linguistic similarities measures and relevant informations about the couple of utterance represented by row1 and row2 :rtype: None """ # pre-process further the utterances represented by row1 and row2, create embeddings, # parts of speech, search for unknown and function words ... utt1 = Utterance(row1.gloss, row1.speaker_id, row1.type) utt2 = Utterance(row2.gloss, row2.speaker_id, row2.type) utt1.expand(model, fw) utt2.expand(model, fw) condition = get_condition(row1, row2, precondition) if row1.speaker_code in settings.child_cond: child_row = row1 adult_row = row2 child_utt = utt1 adult_utt = utt2 else: child_row = row2 adult_row = row1 child_utt = utt2 adult_utt = utt1 [vocabulary_1, vocabulary_3, vocabulary_10, vocabulary_20, vocabulary_50] = vocabulary_gloss lexical_unigrams_nbr = get_simple_ngrams_nbr(utt1.tokens_gloss, utt2.tokens_gloss, 1) lexical_bigrams_nbr = get_simple_ngrams_nbr(utt1.tokens_gloss, utt2.tokens_gloss, 2) lexical_trigrams_nbr = get_simple_ngrams_nbr(utt1.tokens_gloss, utt2.tokens_gloss, 3) syntax_unigrams_nbr = get_simple_ngrams_nbr(utt1.pos_gloss, utt2.pos_gloss, 1) syntax_bigrams_nbr = get_simple_ngrams_nbr(utt1.pos_gloss, utt2.pos_gloss, 2) syntax_trigrams_nbr = get_simple_ngrams_nbr(utt1.pos_gloss, utt2.pos_gloss, 3) syntax_minus_lexic_bigrams_nbr = get_syntax_minus_lexical_ngrams_nbr( utt1.tokens_gloss, utt2.tokens_gloss, utt1.pos_gloss, utt2.pos_gloss, 2) syntax_minus_lexic_trigrams_nbr = get_syntax_minus_lexical_ngrams_nbr( utt1.tokens_gloss, utt2.tokens_gloss, utt1.pos_gloss, utt2.pos_gloss, 3) oov_nbr_1, oov_list_1 = out_of_child_vocab(adult_utt.tokens_gloss, child_row.target_child_age, vocabulary_1) oov_nbr_3, oov_list_3 = out_of_child_vocab(adult_utt.tokens_gloss, child_row.target_child_age, vocabulary_3) oov_nbr_10, oov_list_10 = out_of_child_vocab(adult_utt.tokens_gloss, child_row.target_child_age, vocabulary_10) oov_nbr_20, oov_list_20 = out_of_child_vocab(adult_utt.tokens_gloss, child_row.target_child_age, vocabulary_20) oov_nbr_50, oov_list_50 = out_of_child_vocab(adult_utt.tokens_gloss, child_row.target_child_age, vocabulary_50) semantic_similarity = get_cosine_similarity(utt1.embedding_gloss, utt2.embedding_gloss) lev_dist = nltk.edit_distance(utt1.tokens_gloss, utt2.tokens_gloss) res = { "condition": condition, "child_age": child_row.target_child_age, "child_sex": child_row.target_child_sex, "child_id": child_row.target_child_id, "parent_sex": parent_sex(adult_row), "child_utterance_order": child_row.utterance_order, "adult_utterance_order": adult_row.utterance_order, "child_transcript_id": child_row.transcript_id, "adult_transcript_id": adult_row.transcript_id, "child_corpus_name": child_row.corpus_name, "adult_corpus_name": adult_row.corpus_name, "semantic_similarity": semantic_similarity, "editdistance": lev_dist, "child_utt": child_utt.modified_gloss, "adult_utt": adult_utt.modified_gloss, "child_tokens": str(list(child_utt.tokens_gloss)), "adult_tokens": str(list(adult_utt.tokens_gloss)), "child_tokens_nbr": child_utt.length_gloss, "adult_tokens_nbr": adult_utt.length_gloss, "child_num_morphemes": child_row.num_morphemes, "adult_num_morphemes": adult_row.num_morphemes, "child_pos": child_utt.pos_gloss, "adult_pos": adult_utt.pos_gloss, "child_pos_nbr": len(child_utt.pos_gloss), "adult_pos_nbr": len(adult_utt.pos_gloss), "child_unknown_words": str(list(child_utt.gloss_unknowns)), "adult_unknown_words": str(list(adult_utt.gloss_unknowns)), "child_unknown_words_nbr": child_utt.gloss_unknowns_nbr, "adult_unknown_words_nbr": adult_utt.gloss_unknowns_nbr, "child_stopwords": str(list(child_utt.gloss_stopw)), "adult_stopwords": str(list(adult_utt.gloss_stopw)), "child_stopwords_nbr": child_utt.gloss_stopw_nbr, "adult_stopwords_nbr": adult_utt.gloss_stopw_nbr, "child_final_tokens": str(list(child_utt.final_tokens_gloss)), "adult_final_tokens": str(list(adult_utt.final_tokens_gloss)), "child_final_tokens_nbr": child_utt.final_tokens_gloss_nbr, "adult_final_tokens_nbr": adult_utt.final_tokens_gloss_nbr, "lexical_unigrams_nbr": lexical_unigrams_nbr, "lexical_bigrams_nbr": lexical_bigrams_nbr, "lexical_trigrams_nbr": lexical_trigrams_nbr, "syntax_unigrams_nbr": syntax_unigrams_nbr, "syntax_bigrams_nbr": syntax_bigrams_nbr, "syntax_trigrams_nbr": syntax_trigrams_nbr, "syntax_minus_lexic_bigrams_nbr": syntax_minus_lexic_bigrams_nbr, "syntax_minus_lexic_trigrams_nbr": syntax_minus_lexic_trigrams_nbr, "out_of_child_vocab_nbr_1": oov_nbr_1, "ooc_vocab_words_1": oov_list_1, "out_of_child_vocab_nbr_3": oov_nbr_3, "ooc_vocab_words_3": oov_list_3, "out_of_child_vocab_nbr_10": oov_nbr_10, "ooc_vocab_words_10": oov_list_10, "out_of_child_vocab_nbr_20": oov_nbr_20, "ooc_vocab_words_20": oov_list_20, "out_of_child_vocab_nbr_50": oov_nbr_50, "ooc_vocab_words_50": oov_list_50 } return res