<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import logging

from threading import Thread
from Queue import Queue, Empty
from collections import deque


from helpers import colored_print

from dialog_exceptions import UnsufficientInputError, UnidentifiedAnaphoraError

from dialog.sentence import *

from speaker_identification import SpeakerIdentifier
from parsing.parser import Parser

from interpretation.resolution import Resolver
from interpretation.content_analysis import ContentAnalyser
from interpretation.anaphora_matching import replacement
from verbalization.verbalization import Verbalizer

DIALOG_VERSION = "0.2"

class Dialog(Thread):
    
    #This holds the history of conversation of all the instances of Dialog.
    dialog_history = []
    
    def __init__(self):
        Thread.__init__(self)
        
        self.go_on = True
        self._logger = logging.getLogger('dialog')
        
        self._speaker = SpeakerIdentifier()
        self._parser = Parser()
        self._resolver = Resolver()
        self._content_analyser = ContentAnalyser()
        self._verbalizer = Verbalizer()
        
        self._nl_input_queue = Queue()
        self._sentence_output_queue = Queue()
        
        #true when the Dialog module is within an interaction with a speaker
        self.in_interaction = False
        
        #the current set of sentences that dialog is dealing with
        self.sentences = deque()
        
        #the current sentence being worked on. Specifically, if active_sentence
        #is set, the parser will _complete_ this sentence instead of creating a
        #new one.
        self.active_sentence = None
        
        #true when, during an interaction, more infos is needed.
        self.waiting_for_more_info = False
        
        #contains the last result of a failed resolution (including the object 
        #that could not be resolved. Cf doc of UnsufficientInputError for details.
        self._last_output = None
        
        #contains the last result of a failed resolution of an anaphoric words (it, one) (including the object 
        #that could not be resolved. Cf doc of UnidentifiedAnaphoraError for details.
        self._anaphora_input = None
        
        #the ID of the speaker we are talking with. used to resolve references
        #like 'me', 'you', etc.
        self.current_speaker = None
        
        #the set of the statements generated by the analysis of the last sentence.
        # For testing purposes.
        self.last_stmts_set = []
        
        #last utterance outputed to user. This is a tuple containing both the
        # Sentence object and the verbalized version. For testing purposes.
        self.last_sentence = (None, None)
    
    def run(self):
        while self.go_on:
                  
            try:               
                input = self._nl_input_queue.get(block = False).strip()
                self._logger.info(colored_print("###################################", 'green'))
                self._logger.info(colored_print("#             NL INPUT            #", 'green'))
                self._logger.info(colored_print("###################################\n", 'green'))
                self._logger.info(colored_print(input + "\n", 'blue'))
                self.in_interaction = True
                self.waiting_for_more_info = False
                
                try:
                    self._process(input)
                except UnsufficientInputError as uie:
                    self._logger.info(colored_print("##########################################", 'green'))
                    self._logger.info(colored_print("#  Missing content! Going back to human  #", 'green'))
                    self._logger.info(colored_print("##########################################", 'green'))
                    self._last_output = uie.value
                    
                    #Waiting for more info to solve content
                    self.waiting_for_more_info = True
                
                except UnidentifiedAnaphoraError as uae:
                    self._logger.info(colored_print("##########################################", 'green'))
                    self._logger.info(colored_print("#  Asking for confirmation to human      #", 'green'))
                    self._logger.info(colored_print("##########################################", 'green'))
                    self._anaphora_input = uae.value
                    
                    #waiting for more info to solve anaphora
                    self.waiting_for_more_info = True
                    
                    #Output towards human
                    sys.stdout.write(self._verbalizer.verbalize(uae.value['question']) + "\n")
                    self._logger.info("OUTPUT TO HUMAN:" + colored_print( \
                            self._verbalizer.verbalize(uae.value['question']), \
                            'red') + "\n")
                            
                            
            except Empty:
                pass
            
            try:
                output = self._sentence_output_queue.get(block = False)
                self._logger.debug(colored_print("> Got output to verbalize: ", 'bold'))
                
                nl_output = self._verbalizer.verbalize(output)
                sys.stdout.write(colored_print( \
                            nl_output , \
                            'red') + "\n")
                            
                # Store output 
                Dialog.dialog_history.extend(output)
                
                
            except Empty:
                pass
            
            
    def stop(self):
        while(not self._nl_input_queue.empty()):
            pass
        self.go_on = False

    def input(self, input, speaker = None):
        if speaker:
            self.current_speaker = speaker
        else:
            self.current_speaker = self._speaker.get_current_speaker_id()
        
        #Input for Unsifficient input  Error 
        if self.waiting_for_more_info and self._last_output:
            self._logger.info(colored_print("##########################################", 'green'))
            self._logger.info(colored_print("#   New content provided by human!       #", 'green'))
            self._logger.info(colored_print("##########################################", 'green'))            
            self._logger.info(colored_print(input + "\n", 'blue'))
            
            self._last_output['object_with_more_info'] = nom_gr_remerge(self._parser.parse(input, None),
                                                                self._last_output['status'],
                                                                self._last_output['object'])
            #No more info needed
            self.waiting_for_more_info = False
        
        #Input for Unidentified Anaphora Error
        if self.waiting_for_more_info and self._anaphora_input:
            self._logger.info(colored_print("##########################################", 'green'))
            self._logger.info(colored_print("#   New content provided by human!       #", 'green'))
            self._logger.info(colored_print("##########################################", 'green'))            
            self._logger.info(colored_print(input + "\n", 'blue'))
            
            self._anaphora_input['object_with_more_info'] = replacement(self._parser.parse(input, None),
                                                                                   self._anaphora_input['object'] , 
                                                                                   self._anaphora_input['objects_list'][1:],
                                                                                   self._anaphora_input['object_to_confirm'])
            #No more info needed
            self.waiting_for_more_info = False<commit_msg>Enhanced readability of messages in dialog_core<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import logging

from threading import Thread
from Queue import Queue, Empty
from collections import deque


from helpers import colored_print

from dialog_exceptions import UnsufficientInputError, UnidentifiedAnaphoraError

from dialog.sentence import *

from speaker_identification import SpeakerIdentifier
from parsing.parser import Parser

from interpretation.resolution import Resolver
from interpretation.content_analysis import ContentAnalyser
from interpretation.anaphora_matching import replacement
from verbalization.verbalization import Verbalizer

DIALOG_VERSION = "0.2"

class Dialog(Thread):
    
    #This holds the history of conversation of all the instances of Dialog.
    dialog_history = []
    
    def __init__(self):
        Thread.__init__(self)
        
        self.go_on = True
        self._logger = logging.getLogger('dialog')
        
        self._speaker = SpeakerIdentifier()
        self._parser = Parser()
        self._resolver = Resolver()
        self._content_analyser = ContentAnalyser()
        self._verbalizer = Verbalizer()
        
        self._nl_input_queue = Queue()
        self._sentence_output_queue = Queue()
        
        #true when the Dialog module is within an interaction with a speaker
        self.in_interaction = False
        
        #the current set of sentences that dialog is dealing with
        self.sentences = deque()
        
        #the current sentence being worked on. Specifically, if active_sentence
        #is set, the parser will _complete_ this sentence instead of creating a
        #new one.
        self.active_sentence = None
        
        #true when, during an interaction, more infos is needed.
        self.waiting_for_more_info = False
        
        #contains the last result of a failed resolution (including the object 
        #that could not be resolved. Cf doc of UnsufficientInputError for details.
        self._last_output = None
        
        #contains the last result of a failed resolution of an anaphoric words (it, one) (including the object 
        #that could not be resolved. Cf doc of UnidentifiedAnaphoraError for details.
        self._anaphora_input = None
        
        #the ID of the speaker we are talking with. used to resolve references
        #like 'me', 'you', etc.
        self.current_speaker = None
        
        #the set of the statements generated by the analysis of the last sentence.
        # For testing purposes.
        self.last_stmts_set = []
        
        #last utterance outputed to user. This is a tuple containing both the
        # Sentence object and the verbalized version. For testing purposes.
        self.last_sentence = (None, None)
    
    def run(self):
        while self.go_on:
                  
            try:               
                input = self._nl_input_queue.get(block = False).strip()
                self._logger.info(colored_print("###################################", 'green'))
                self._logger.info(colored_print("#             NL INPUT            #", 'green'))
                self._logger.info(colored_print("###################################\n", 'green'))
                self._logger.info(colored_print("- " + input + "\n", 'blue'))
                self.in_interaction = True
                self.waiting_for_more_info = False
                
                try:
                    self._process(input)
                except UnsufficientInputError as uie:
                    self._logger.info(colored_print("Not enough informations! Going back to human:", 'magenta'))
                    self._last_output = uie.value
                    
                    #Waiting for more info to solve content
                    self.waiting_for_more_info = True
                    
                    #Output towards human
                    sys.stdout.write(self._verbalizer.verbalize(uie.value['question']) + "\n")
                    self._logger.info(colored_print("- " +  \
                            self._verbalizer.verbalize(uie.value['question']), \
                            'blue') + "\n")
                
                except UnidentifiedAnaphoraError as uae:
                    self._logger.info(colored_print("Not sure of the interpretation...  Asking for confirmation to human:", 'magenta'))
                    self._anaphora_input = uae.value
                    
                    #waiting for more info to solve anaphora
                    self.waiting_for_more_info = True
                    
                    #Output towards human
                    sys.stdout.write(self._verbalizer.verbalize(uae.value['question']) + "\n")
                    self._logger.info("- " + colored_print( \
                            self._verbalizer.verbalize(uae.value['question']), \
                            'blue') + "\n")
                            
                            
            except Empty:
                pass
            
            try:
                output = self._sentence_output_queue.get(block = False)
                self._logger.debug(colored_print("> Got output to verbalize: ", 'bold'))
                
                nl_output = self._verbalizer.verbalize(output)
                sys.stdout.write(colored_print( \
                            nl_output , \
                            'red') + "\n")
                            
                # Store output 
                Dialog.dialog_history.extend(output)
                
                
            except Empty:
                pass
            
            
    def stop(self):
        while(not self._nl_input_queue.empty()):
            pass
        self.go_on = False

    def input(self, input, speaker = None):
        if speaker:
            self.current_speaker = speaker
        else:
            self.current_speaker = self._speaker.get_current_speaker_id()
        
        #Input for Unsifficient input  Error 
        if self.waiting_for_more_info and self._last_output:
            self._logger.info(colored_print("New content provided by human! merging it with the previous sentence.", 'magenta'))
            
            self._last_output['object_with_more_info'] = nom_gr_remerge(self._parser.parse(input, None),
                                                                self._last_output['status'],
                                                                self._last_output['object'])
            #No more info needed
            self.waiting_for_more_info = False
        
        #Input for Unidentified Anaphora Error
        if self.waiting_for_more_info and self._anaphora_input:
            self._logger.info(colored_print("Ok. Got a confirmation. Processing it.", 'magenta'))
            
            self._anaphora_input['object_with_more_info'] = replacement(self._parser.parse(input, None),
                                                                                   self._anaphora_input['object'] , 
                                                                                   self._anaphora_input['objects_list'][1:],
                                                                                   self._anaphora_input['object_to_confirm'])
            #No more info needed
            self.waiting_for_more_info = False
{'fraction_non_alphanumeric': 0.1046549373498669, 'fraction_numerical': 0.0005193793416866844, 'mean_word_length': 1.6077535127814457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4195554', 'n_tokens_mistral': 3721, 'n_tokens_neox': 3572, 'n_words': 945}