Ejemplo n.º 1
0
    def start_socket(self, port=42424):
        """
        Listens socket for input phrases.
        """
        host = ""
        buf = 1024
        addr = (host, port)
        UDPSock = socket(AF_INET, SOCK_DGRAM)
        UDPSock.bind(addr)
        print("Listening socket")

        occupation = multiprocessing.Event()
        # listener_queue = multiprocessing.Queue(maxsize=0)
        # recognizer_queue = multiprocessing.Queue(maxsize=0)
        speaker_queue = multiprocessing.Queue(maxsize=0)
        speaker = multiprocessing.Process(target=speech.speaker,
                                          args=(
                                              occupation,
                                              speaker_queue,
                                          ))
        # recognizer = multiprocessing.Process(
        #     target=speech.recognizer,
        #     args=(recognizer_queue, listener_queue, ))
        # listener = multiprocessing.Process(
        #     target=speech.listener,
        #     args=(occupation, recognizer_queue, ))
        speaker.start()
        # recognizer.start()
        # listener.start()
        occupation.set()

        print("======")
        for state in self.expected:
            print("\t    %s" % (state))
        print("======")
        while True:
            # process routines answers
            answers = self.returns.get_returns()
            for answer in answers:
                tosay, questions = answer.accept()
                speaker_queue.put(tosay)
                print("Bot> " + tosay)
                self._extend_expected(questions)
            # process input
            (data, addr) = UDPSock.recvfrom(buf)
            input_phrase = data.decode("utf-8")
            print("You> " + input_phrase)
            while not input_phrase.strip():
                print("Empty input string")
                input_phrase = input("You> ")
            input_phrase = link_parser.parse(input_phrase)
            state = self.interpret(input_phrase)
            if state:
                tosay, questions = state.accept(input_phrase)
                for answer in tosay:
                    if answer != "":
                        speaker_queue.put(answer)
                        print("Bot> " + answer)
                self._extend_expected(questions)
        UDPSock.close()
Ejemplo n.º 2
0
 def _parse(self):
     """
     Parses input string using regex.
     """
     pattern = re.compile(r'`([a-zA-Z0-9_]*)((\:|\~|\<)([^`]+))?`')
     for inline in pattern.finditer(self.origin):
         if bool(inline.group(2)):
             if inline.group(3) == ':':
                 self._create_fixed_setter(
                     inline.group(1),
                     inline.group(4),
                     *inline.span())
             elif inline.group(3) == '~':
                 self._create_flexible_setter(
                     inline.group(1),
                     inline.group(4),
                     *inline.span())
             elif inline.group(3) == '<':
                 self._create_routine_request(
                     inline.group(1),
                     inline.group(4),
                     *inline.span())
         else:
             self._create_substition(inline.group(1), *inline.span())
     self._erase_fixed_setters()
     self._erase_flexible_setters()
     self._erase_routine_requests()
     try:
         self.latest = self.evaluate()
         self.parsed = link_parser.parse(self.latest)
         self._link_flexible_setters()
     except KeyError: # not all keys available now
         pass
Ejemplo n.º 3
0
 def start_text(self):
     """
     Interprets dialog in text mode.
     """
     print("======")
     for state in self.expected:
         print("\t    %s" % (state))
     print("======")
     while True:
         # process routines answers
         answers = self.returns.get_returns()
         for answer in answers:
             tosay, questions = answer.accept()
             print("Bot> " + tosay)
             self._extend_expected(questions)
         # process input
         input_phrase = input("You> ")
         while not input_phrase.strip():
             # print("Empty input string")
             # process routines second time! duplex tests
             answers = self.returns.get_returns()
             # print(answers)
             for answer in answers:
                 tosay, questions = answer.accept()
                 print("Bot> " + tosay)
                 self._extend_expected(questions)
             input_phrase = input("You> ")
         input_phrase = link_parser.parse(input_phrase)
         state = self.interpret(input_phrase)
         if state:
             tosay, questions = state.accept(input_phrase)
             for answer in tosay:
                 if answer != "":
                     print("Bot> " + answer)
             self._extend_expected(questions)
Ejemplo n.º 4
0
 def _parse(self):
     """
     Parses input string using regex.
     """
     pattern = re.compile(r'`([a-zA-Z0-9_]*)((\:|\~|\<)([^`]+))?`')
     for inline in pattern.finditer(self.origin):
         if bool(inline.group(2)):
             if inline.group(3) == ':':
                 self._create_fixed_setter(inline.group(1), inline.group(4),
                                           *inline.span())
             elif inline.group(3) == '~':
                 self._create_flexible_setter(inline.group(1),
                                              inline.group(4),
                                              *inline.span())
             elif inline.group(3) == '<':
                 self._create_routine_request(inline.group(1),
                                              inline.group(4),
                                              *inline.span())
         else:
             self._create_substition(inline.group(1), *inline.span())
     self._erase_fixed_setters()
     self._erase_flexible_setters()
     self._erase_routine_requests()
     try:
         self.latest = self.evaluate()
         self.parsed = link_parser.parse(self.latest)
         self._link_flexible_setters()
     except KeyError:  # not all keys available now
         pass
Ejemplo n.º 5
0
 def start_text(self):
     """
     Interprets dialog in text mode.
     """
     print("======")
     for state in self.expected:
         print("\t    %s" % (state))
     print("======")
     while True:
         # process routines answers
         answers = self.returns.get_returns()
         for answer in answers:
             tosay, questions = answer.accept()
             print("Bot> "+tosay)
             self._extend_expected(questions)
         # process input
         input_phrase = input("You> ")
         while not input_phrase.strip():
             # print("Empty input string")
             # process routines second time! duplex tests
             answers = self.returns.get_returns()
             # print(answers)
             for answer in answers:
                 tosay, questions = answer.accept()
                 print("Bot> "+tosay)
                 self._extend_expected(questions)
             input_phrase = input("You> ")
         input_phrase = link_parser.parse(input_phrase)
         state = self.interpret(input_phrase)
         if state:
             tosay, questions = state.accept(input_phrase)
             for answer in tosay:
                 if answer != "":
                     print("Bot> "+answer)
             self._extend_expected(questions)
Ejemplo n.º 6
0
    def start_socket(self, port=42424):
        """
        Listens socket for input phrases.
        """
        host = ""
        buf = 1024
        addr = (host, port)
        UDPSock = socket(AF_INET, SOCK_DGRAM)
        UDPSock.bind(addr)
        print("Listening socket")

        occupation = multiprocessing.Event()
        # listener_queue = multiprocessing.Queue(maxsize=0)
        # recognizer_queue = multiprocessing.Queue(maxsize=0)
        speaker_queue = multiprocessing.Queue(maxsize=0)
        speaker = multiprocessing.Process(
            target=speech.speaker,
            args=(occupation, speaker_queue, ))
        # recognizer = multiprocessing.Process(
        #     target=speech.recognizer,
        #     args=(recognizer_queue, listener_queue, ))
        # listener = multiprocessing.Process(
        #     target=speech.listener,
        #     args=(occupation, recognizer_queue, ))
        speaker.start()
        # recognizer.start()
        # listener.start()
        occupation.set()

        print("======")
        for state in self.expected:
            print("\t    %s" % (state))
        print("======")
        while True:
            # process routines answers
            answers = self.returns.get_returns()
            for answer in answers:
                tosay, questions = answer.accept()
                speaker_queue.put(tosay)
                print("Bot> "+tosay)
                self._extend_expected(questions)
            # process input
            (data, addr) = UDPSock.recvfrom(buf)
            input_phrase = data.decode("utf-8")
            print("You> "+input_phrase)
            while not input_phrase.strip():
                print("Empty input string")
                input_phrase = input("You> ")
            input_phrase = link_parser.parse(input_phrase)
            state = self.interpret(input_phrase)
            if state:
                tosay, questions = state.accept(input_phrase)
                for answer in tosay:
                    if answer != "":
                        speaker_queue.put(answer)
                        print("Bot> "+answer)
                self._extend_expected(questions)
        UDPSock.close()
Ejemplo n.º 7
0
 def start_offline(self):
     """
     Interprets dialog with offline TTS and STT.
     """
     import dialog.speech as speech
     occupation = multiprocessing.Event()
     listener_queue = multiprocessing.Queue(maxsize=0)
     recognizer_queue = multiprocessing.Queue(maxsize=0)
     speaker_queue = multiprocessing.Queue(maxsize=0)
     speaker = multiprocessing.Process(target=speech.speaker_pico,
                                       args=(
                                           occupation,
                                           speaker_queue,
                                       ))
     recognizer = multiprocessing.Process(target=speech.recognizer_sphinx,
                                          args=(
                                              recognizer_queue,
                                              listener_queue,
                                          ))
     listener = multiprocessing.Process(target=speech.listener,
                                        args=(
                                            occupation,
                                            recognizer_queue,
                                        ))
     speaker.start()
     recognizer.start()
     listener.start()
     occupation.set()
     print("======")
     for state in self.expected:
         print("\t    %s" % (state))
     print("======")
     while True:
         # process routines answers
         answers = self.returns.get_returns()
         for answer in answers:
             tosay, questions = answer.accept()
             speaker_queue.put(tosay)
             print("Bot> " + tosay)
             self._extend_expected(questions)
         # process input
         if not listener_queue.empty():
             input_phrase = listener_queue.get()
             input_phrase = link_parser.parse(input_phrase)
             state = self.interpret(input_phrase)
             if state:
                 tosay, questions = state.accept(input_phrase)
                 for answer in tosay:
                     if answer != "":
                         speaker_queue.put(answer)
                         print("Bot> " + answer)
                 self._extend_expected(questions)
Ejemplo n.º 8
0
 def _update_parsed(self):
     """
     Method evaulates string and if it's changed, 
     parses the new one with link-parser.
     """
     # TODO: probably add check for changed vaules before eval
     try:
         if self.evaluate() != self.latest:
             self.latest = self.evaluate()
             self.parsed = link_parser.parse(self.latest)
             self._link_flexible_setters()
     except KeyError:
         print("WARNING: Can't evaluate")
Ejemplo n.º 9
0
 def _update_parsed(self):
     """
     Method evaulates string and if it's changed, 
     parses the new one with link-parser.
     """
     # TODO: probably add check for changed vaules before eval
     try:
         if self.evaluate() != self.latest:
             self.latest = self.evaluate()
             self.parsed = link_parser.parse(self.latest)
             self._link_flexible_setters()
     except KeyError:
         print("WARNING: Can't evaluate")
Ejemplo n.º 10
0
 def start_offline(self):
     """
     Interprets dialog with offline TTS and STT.
     """
     import dialog.speech as speech
     occupation = multiprocessing.Event()
     listener_queue = multiprocessing.Queue(maxsize=0)
     recognizer_queue = multiprocessing.Queue(maxsize=0)
     speaker_queue = multiprocessing.Queue(maxsize=0)
     speaker = multiprocessing.Process(
         target=speech.speaker_pico,
         args=(occupation, speaker_queue, ))
     recognizer = multiprocessing.Process(
         target=speech.recognizer_sphinx,
         args=(recognizer_queue, listener_queue, ))
     listener = multiprocessing.Process(
         target=speech.listener,
         args=(occupation, recognizer_queue, ))
     speaker.start()
     recognizer.start()
     listener.start()
     occupation.set()
     print("======")
     for state in self.expected:
         print("\t    %s" % (state))
     print("======")
     while True:
         # process routines answers
         answers = self.returns.get_returns()
         for answer in answers:
             tosay, questions = answer.accept()
             speaker_queue.put(tosay)
             print("Bot> "+tosay)
             self._extend_expected(questions)
         # process input
         if not listener_queue.empty():
             input_phrase = listener_queue.get()
             input_phrase = link_parser.parse(input_phrase)
             state = self.interpret(input_phrase)
             if state:
                 tosay, questions = state.accept(input_phrase)
                 for answer in tosay:
                     if answer != "":
                         speaker_queue.put(answer)
                         print("Bot> "+answer)
                 self._extend_expected(questions)
Ejemplo n.º 11
0
 def start_server(self, to_user, to_ds):
     """
     Interprets dialog
     """
     # occupation = multiprocessing.Event()
     # listener_queue = multiprocessing.Queue(maxsize=0)
     # recognizer_queue = multiprocessing.Queue(maxsize=0)
     # speaker_queue = multiprocessing.Queue(maxsize=0)
     # speaker = multiprocessing.Process(
     #     target=speech.speaker,
     #     args=(occupation, speaker_queue, ))
     # recognizer = multiprocessing.Process(
     #     target=speech.recognizer,
     #     args=(recognizer_queue, listener_queue, ))
     # listener = multiprocessing.Process(
     #     target=speech.listener,
     #     args=(occupation, recognizer_queue, ))
     # speaker.start()
     # recognizer.start() # IN CASE OF SPEECH RECOGNITION
     # listener.start()   # IN CASE OF SPEECH RECOGNITION
     # occupation.set()
     # print("======")
     # for state in self.expected:
     #     print("\t%s" % (state))
     # print("======")
     while True:
         # process routines answers
         answers = self.returns.get_returns()
         for answer in answers:
             tosay, questions = answer.accept()
             to_user.put(json.dumps({'type': 'phrase', 'text': tosay}))
             self._extend_expected(questions)
         # process input
         if not to_ds.empty():
             input_origin = to_ds.get()
             # input_phrase = listener_queue.get() # IN CASE OF SPEECH RECOGNITION
             input_phrase = link_parser.parse(input_origin)
             states_probability = []
             for state in self.expected:
                 # print(state, state.compare(input_phrase))
                 states_probability.append((state, state.compare(input_phrase)))
             states_probability = sorted(states_probability, key=lambda x: x[1], reverse=True)
             # print("======")
             # for state in states_probability:
             #     print("%.2f\t%s" % (state[1], state[0]))
             # print("======")
             state = states_probability[0][0]
             if states_probability[0][1] < 0.2:
                 # print("Bot> ???")
                 to_user.put(json.dumps({
                     'type': 'origin',
                     'text': input_origin
                 }))
                 to_user.put(json.dumps({'type': 'unknown'}))
                 #TODO: save unknown request, with state!!!
             else:
                 to_user.put(json.dumps({
                     'type': 'interpretation',
                     'origin': input_origin,
                     'phrase': str(state),
                     'similarity': states_probability[0][1]
                 }))
                 tosay, questions = state.accept(input_phrase)
                 for answer in tosay:
                     if answer != "":
                         # speaker_queue.put(answer)
                         # print("Bot> "+answer)
                         to_user.put(json.dumps({'type': 'phrase', 'text': answer}))
                 self._extend_expected(questions)
Ejemplo n.º 12
0
 def start_server(self, to_user, to_ds):
     """
     Interprets dialog
     """
     # occupation = multiprocessing.Event()
     # listener_queue = multiprocessing.Queue(maxsize=0)
     # recognizer_queue = multiprocessing.Queue(maxsize=0)
     # speaker_queue = multiprocessing.Queue(maxsize=0)
     # speaker = multiprocessing.Process(
     #     target=speech.speaker,
     #     args=(occupation, speaker_queue, ))
     # recognizer = multiprocessing.Process(
     #     target=speech.recognizer,
     #     args=(recognizer_queue, listener_queue, ))
     # listener = multiprocessing.Process(
     #     target=speech.listener,
     #     args=(occupation, recognizer_queue, ))
     # speaker.start()
     # recognizer.start() # IN CASE OF SPEECH RECOGNITION
     # listener.start()   # IN CASE OF SPEECH RECOGNITION
     # occupation.set()
     # print("======")
     # for state in self.expected:
     #     print("\t%s" % (state))
     # print("======")
     while True:
         # process routines answers
         answers = self.returns.get_returns()
         for answer in answers:
             tosay, questions = answer.accept()
             to_user.put(json.dumps({'type': 'phrase', 'text': tosay}))
             self._extend_expected(questions)
         # process input
         if not to_ds.empty():
             input_origin = to_ds.get()
             # input_phrase = listener_queue.get() # IN CASE OF SPEECH RECOGNITION
             input_phrase = link_parser.parse(input_origin)
             states_probability = []
             for state in self.expected:
                 # print(state, state.compare(input_phrase))
                 states_probability.append(
                     (state, state.compare(input_phrase)))
             states_probability = sorted(states_probability,
                                         key=lambda x: x[1],
                                         reverse=True)
             # print("======")
             # for state in states_probability:
             #     print("%.2f\t%s" % (state[1], state[0]))
             # print("======")
             state = states_probability[0][0]
             if states_probability[0][1] < 0.2:
                 # print("Bot> ???")
                 to_user.put(
                     json.dumps({
                         'type': 'origin',
                         'text': input_origin
                     }))
                 to_user.put(json.dumps({'type': 'unknown'}))
                 #TODO: save unknown request, with state!!!
             else:
                 to_user.put(
                     json.dumps({
                         'type': 'interpretation',
                         'origin': input_origin,
                         'phrase': str(state),
                         'similarity': states_probability[0][1]
                     }))
                 tosay, questions = state.accept(input_phrase)
                 for answer in tosay:
                     if answer != "":
                         # speaker_queue.put(answer)
                         # print("Bot> "+answer)
                         to_user.put(
                             json.dumps({
                                 'type': 'phrase',
                                 'text': answer
                             }))
                 self._extend_expected(questions)