async def on_recognize(
        self,
        turn_context: TurnContext,
        state: Dict[str, object],
        options: PromptOptions,
    ) -> PromptRecognizerResult:
        if not turn_context:
            raise TypeError("turn_context can’t be none")

        if turn_context.activity.type == ActivityTypes.message:
            utterance = turn_context.activity.text

        turn_context.activity.locale = self._default_locale

        recognizer_result = PromptRecognizerResult()

        recognizer = NumberRecognizer(turn_context.activity.locale)

        if (self._prompt_type == NumberWithTypePromptType.Ordinal):
            model = recognizer.get_ordinal_model()
        elif (self._prompt_type == NumberWithTypePromptType.Percentage):
            model = recognizer.get_percentage_model()

        model_result = model.parse(utterance)
        if len(model_result) > 0 and len(model_result[0].resolution) > 0:
            recognizer_result.succeeded = True
            recognizer_result.value = model_result[0].resolution["value"]

        return recognizer_result
Esempio n. 2
0
    def _recognize_ordinal(utterance: str, culture: str) -> List[ModelResult]:
        model: OrdinalModel = NumberRecognizer(culture).get_ordinal_model(
            culture)

        return list(
            map(ChoiceRecognizers._found_choice_constructor,
                model.parse(utterance)))
Esempio n. 3
0
    def perform_ner(self, filepath: str, entity_type: str = 'number', update: bool = False) -> str:
        newfilepath = os.path.join(
            self.storage_path, entity_type + '_' + os.path.basename(filepath))
        if not update and os.path.exists(newfilepath):
            return newfilepath
        wfile = open(newfilepath, 'w')

        filepath = os.path.abspath(filepath)
        rfile = open(filepath, 'r')

        recognizer = NumberRecognizer(Culture.English)
        model = recognizer.get_number_model()
        for line in rfile:
            answer = next(rfile)

            answer_json = json.loads(answer)

            text = str(answer_json['body'])
            try:
                result = model.parse(text)
                if result:
                    for x in result:
                        if x.type_name == entity_type:
                            wfile.write(line)
                            wfile.write(answer)
                            break
            except Exception as e:
                print(e)
        return newfilepath