def __init__(self): self.model = None self.r = sr.Recognizer() self.nlp = spacy.load(conf.get_property('spacy')['model']) self.espeak = conf.get_property('espeak') # Load low-level Duckling model self.duckling = Duckling() self.duckling.load( languages=conf.get_property('duckling')['languages']) # Remember tasks self.task_memory = []
''' Asistente conversacional Aragón Open Data_v1.0.0 Copyright © 2020 Gobierno de Aragón (España) Author: Instituto Tecnológico de Aragón ([email protected]) All rights reserved ''' import json from datetime import datetime import requests from duckling import Duckling from pprint import pprint from typing import Dict, Any, List, Union import re duckling = Duckling() duckling.load(languages=["es"]) ######################### # NUMBERS & MONTHS MAPS # ######################### map_numbers_es = { "un": 1, "uno": 1, "dos": 2, "tres": 3, "cuatro": 4, "cinco": 5, "seis": 6, "siete": 7,
def test_not_load(): duckling = Duckling() assert duckling._is_loaded is False with pytest.raises(RuntimeError): duckling.parse('')
def duckling(): return Duckling()
def __init__(self): self.d = Duckling() self.d.load()
class AppointmentForm(FormAction): """Example of a custom form action""" def __init__(self): self.d = Duckling() self.d.load() def name(self): # type: () -> Text """Unique identifier of the form""" return "appointment_form" @staticmethod def required_slots(tracker: Tracker) -> List[Text]: """A list of required slots that the form has to fill""" return ["date", "time", "pet", "petName"] def slot_mappings(self): return { "date": [self.from_entity(entity="date"), self.from_text()], "time": [self.from_entity(entity="time"), self.from_text()], "pet": [self.from_entity(entity="pet"), self.from_text()], "petName": [self.from_entity(entity="petName"), self.from_text()], } @staticmethod def pet_db(): # type: () -> List[Text] """Database of supported cuisines""" return [ "dog", "cat", "bird", "tortoise", "rabbit", "guinea pig", "mouse", "hamster", "turtles" ] def validate_date(self, value: Text, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> Dict[Text, Any]: """Validate date value.""" date = date_validator(value) if (date != None): return {"date": date} else: dispatcher.utter_template('utter_wrong_date', tracker) # validation failed, set this slot to None, meaning the # user will be asked for the slot again return {"date": None} def validate_pet(self, value: Text, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> Dict[Text, Any]: """Validate pet value.""" if value.lower() in self.pet_db(): # validation succeeded return {"pet": value} else: dispatcher.utter_template('utter_wrong_pet', tracker) # validation failed, set this slot to None, meaning the # user will be asked for the slot again return {"pet": None} def validate_time(self, value: Text, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> Dict[Text, Any]: """Validate time value.""" time = time_validator(value) if (time != None): return {"time": time} else: dispatcher.utter_template('utter_wrong_time', tracker) # validation failed, set slot to None return {"time": None} def validate_petName(self, value: Text, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> Dict[Text, Any]: """Validate petName value.""" print(value) return {"petName": value} def submit(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict]: """Define what the form has to do after all required slots are filled""" # utter submit template print("petName: " + tracker.get_slot("petName")) print("pet: " + tracker.get_slot("pet")) print("time: " + tracker.get_slot("time")) print("date: " + tracker.get_slot("date")) url = 'https://dimo1.ii.uam.es:8443/VeterinarioApi/Rasa' myobj = { 'petName': tracker.get_slot("petName"), 'pet': tracker.get_slot("pet"), 'time': tracker.get_slot("time"), 'date': tracker.get_slot("date"), } x = requests.post(url, json=myobj) print(x.text) dispatcher.utter_message(x.text) #dispatcher.utter_template('utter_submit', tracker) return [ SlotSet("petName", None), SlotSet("pet", None), SlotSet("time", None), SlotSet("date", None) ]
import typing from typing import Any, Optional, Text, Dict, List, Type from rasa.nlu.components import Component from rasa.nlu.config import RasaNLUModelConfig from rasa.nlu.training_data import Message, TrainingData if typing.TYPE_CHECKING: from rasa.nlu.model import Metadata from duckling import Duckling duck = Duckling() duck.load() class MyComponent(Component): """A new component""" # Which components are required by this component. # Listed components should appear before the component itself in the pipeline. @classmethod def required_components(cls) -> List[Type[Component]]: """Specify which components need to be present in the pipeline.""" return [] # Defines the default configuration parameters of a component # these values can be overwritten in the pipeline configuration # of the model. The component should choose sensible defaults # and should be able to create reasonable results with the defaults. defaults = {}
class Brain: MODULE_BASE_PATH = 'lucia.tasks.' def __init__(self): self.model = None self.r = sr.Recognizer() self.nlp = spacy.load(conf.get_property('spacy')['model']) self.espeak = conf.get_property('espeak') # Load low-level Duckling model self.duckling = Duckling() self.duckling.load( languages=conf.get_property('duckling')['languages']) # Remember tasks self.task_memory = [] def create_model(self): # Create a DeepSpeech model with model path self.model = Model(conf.get_property('deepspeech')['model_path']) # Enable decoding using an external scorer self.model.enableExternalScorer( conf.get_property('deepspeech')['scorer_path']) def listen(self, debug_mode=False): while True: with sr.Microphone(sample_rate=conf.get_property( 'speech_recognition')['audio_rate']) as source: # Listen for a while and adjust the energy threshold to start and stop recording voice to account for ambient noise self.r.adjust_for_ambient_noise( source, duration=conf.get_property( 'speech_recognition')['energy_threshold']) self.r.dynamic_energy_threshold = True if debug_mode is False: print("Say something") audio = self.r.listen(source) # Speech to text audio = np.frombuffer(audio.frame_data, np.int16) text = self.model.stt(audio) self.speak(text) else: text = input() # Wake up on hearing the wake word #if any(subtext in text for subtext in conf.get_property('wake_words')): # self.understand(text) self.understand(text) def speak(self, text): subprocess.call('espeak-ng -v {}+{}{} "{}"'.format( self.espeak['language'], self.espeak['gender'], self.espeak['pitch'], text), shell=True) def understand(self, sentence): # Break paragraph into sentences tokenized_sentence = sent_tokenize(sentence) # Break sentence into words for sent in tokenized_sentence: tokenized_word = word_tokenize(sent) # Tag corpora with universal POS tagset # For tag list, read https://www.nltk.org/book/ch05.html#tab-universal-tagset pos_tags = nltk.pos_tag(tokenized_word, tagset='universal') # Divide sentence into noun phrases with regular expression grammar = 'NOUN: {<DET>?<ADJ>*<NOUN>}' cp = nltk.RegexpParser(grammar) # Find chunk structure cs = cp.parse(pos_tags) # B-{tag} beginning, I-{tag} inside, O-{tag} outside iob_tags = np.asarray(tree2conlltags(cs)).tolist() # Recognize named entities doc = self.nlp(sent) # Parse word into numeral, ordinal, and time parse = lambda ne: dict([[ _['dim'], _['value']['value'] ] for _ in self.duckling.parse( ne, dim_filter=conf.get_property('duckling')['dimensions'])]) # [Word, character positions and entity type]. For all entity types, read https://spacy.io/api/annotation#named-entities ne = list([ ent.text, ent.start_char, ent.end_char, ent.label_, parse(ent.text) ] for ent in doc.ents) ne_tags = [_.ent_type_ for _ in doc] # Merge iob tags and named entity tags tagged_sent = [ list(np.append(iob_tags[i], ne_tags[i])) for i in range(len(iob_tags)) ] tagged_sent = ''.join(str(x) for x in tagged_sent) self.decide(tagged_sent, ne) def think(self, pattern, tagged_sent): # Match tagged sentence against combinations of POS tags, words in any order: (?=.*\bword\b)(?=.*\bADJ\bNOUN\b).* r = re.compile( '(?=.*\\b' + pattern.replace(' ', '\\b.*\\b').replace(' ', '\\b)(?=.*\\b') + '\\b).*') return r.search(tagged_sent) def decide(self, tagged_sent, named_entity): for task in conf.get_property('tasks'): for pattern in conf.get_property('tasks')[task]: # If sentence matches any pattern, dynamtically create class if self.think(pattern, tagged_sent): # Split module name and class name with dot module = importlib.import_module(self.MODULE_BASE_PATH + task.rsplit('.', 1)[0]) instance = getattr(module, task.rsplit('.', 1)[1])() print(instance) # Search whether task_memory contains the same class instance _run = False for mem in self.task_memory: if type(instance) == type(mem): mem.run(self, tagged_sent, named_entity) _run = True break if not _run: # If not exists, store new class instance in task_memory self.task_memory = [ instance.run(self, tagged_sent, named_entity) ] + self.task_memory break
from duckling import Duckling import calendar import re from datetime import datetime present = datetime.now() d = Duckling() d.load() def clean_date(date): date_in_no = re.sub('[a-zA-Z-:+.]', '', date) temp = 0 year = '' month = '' day = '' for i in date_in_no: temp += 1 if temp <= 4: year += i if temp == 5 or temp == 6: month += i if temp == 7 or temp == 8: day += i return year, month, day def get_end_date(grain, date): date_in_no = re.sub('[a-zA-Z-:+.]', '', date) temp = 0 year = ''