示例#1
0
    def transform_utterance(self, utt, override_input_filter=False):
        """
        Computes per-utterance attributes of an individual utterance or string. For utterances which do not contain all of the `input_field` attributes as specified in the constructor, or for utterances which return `False` on `input_filter`, this call will not annotate the utterance. For strings, will convert the string to an utterance and return the utterance, annotating it if `input_field` is not set to `None` at initialization.

        :param utt: utterance or a string
        :param override_input_filter: ignore `input_filter` and compute attribute for all utterances
        :return: the utterance
        """

        if isinstance(utt, str):
            utt = Utterance(text=utt, speaker=Speaker(id="speaker"))
        if self.input_field is None:
            text_entry = utt.text
        else:
            if not override_input_filter:
                if not self.input_filter(utt, self.aux_input): 
                    return utt 
            if isinstance(self.input_field, str):
                text_entry = utt.retrieve_meta(self.input_field)
            elif isinstance(self.input_field, list):
                text_entry = {field: utt.retrieve_meta(field) for field in self.input_field}
                if sum(x is None for x in text_entry.values()) > 0:
                    return utt
        if text_entry is None:
            return utt
        if len(self.aux_input) == 0:
            result = self.proc_fn(text_entry)
        else:
            result = self.proc_fn(text_entry, self.aux_input)
        if self.multi_outputs:
            for res, out in zip(result, self.output_field):
                utt.add_meta(out, res)
        else:
            utt.add_meta(self.output_field, result)
        return utt
示例#2
0
    def transform_utterance(self, utt: Utterance, spacy_nlp: Callable[[str], Doc] = None, markers: bool = False):
        """
        Extract politeness strategies for raw string inputs (or individual utterances)
        
        :param utt: the utterance to be annotated with politeness strategies. 
        :spacy_nlp: if provided, will use this SpaCy object to do parsing; otherwise will initialize an object via `load('en')`.
        :return: the utterance with politeness annotations.
        """
        
        if isinstance(utt, str):
            utt = Utterance(text=utt, speaker=Speaker(id='speaker'))
        
        if self.parse_attribute_name not in utt.meta:
            
            if spacy_nlp is None:
                raise ValueError('spacy object required')
            
            parses = process_text(utt.text, spacy_nlp=spacy_nlp)
            utt.add_meta(self.parse_attribute_name, parses)
        
        parsed = utt.retrieve_meta(self.parse_attribute_name)
        for i, sent in enumerate(parsed):
            for p in sent["toks"]:
                p["tok"] = p['tok'].lower()
        parses = [x["toks"] for x in parsed]
        
        utt.meta[self.strategy_attribute_name], marks = self._extractor_lookup[self.strategy_collection](parses)

        if markers:
            utt.meta[self.marker_attribute_name] = marks
        
        return utt