Exemple #1
0
def RunSarmata(path):
    recognized = None
    wave_file = path
    grammar_file = "grammars/grandma.abnf"
    args = SarmataArgs(wave_file, grammar_file)

    settings = SarmataSettings()
    settings.process_args(args)  # load settings from cmd
    if args.grammar is not None:
        settings.load_grammar(args.grammar)

    can_define_grammar = False
    if args.define_grammar:
        if not settings.grammar_name:
            print("Bad usage. Set BOTH grammar_name and grammar file when define grammar is set True.")
            sys.exit(1)
        can_define_grammar = True

    recognizer = SarmataRecognizer(args.address)

    if can_define_grammar:
        define_grammar_response = recognizer.define_grammar(args.grammar_name, settings.grammar)
        if define_grammar_response.ok:
            if args.grammar is None:
                print("Grammar " + args.grammar_name + " removed")
            else:
                print("Grammar " + args.grammar + " defined as " + args.grammar_name)
        else:
            print("Define grammar error: " + define_grammar_response.error)

    # --------------------------
    # recognize section
    # --------------------------
    if args.wave is not None or args.mic:
        validate_recognition_settings(settings)

        with create_audio_stream(args) as stream:
            # generate id
            session_id = stream.session_id()
            settings.set_session_id(session_id)

            results = recognizer.recognize(stream, settings)
            recognized = print_results(results, stream)
    return recognized
        if define_grammar_response.ok:
            if args.grammar is None:
                print("Grammar " + args.grammar_name + " removed")
            else:
                print("Grammar " + args.grammar + " defined as " +
                      args.grammar_name)
        else:
            print("Define grammar error: " + define_grammar_response.error)

    # --------------------------
    # recognize section
    # --------------------------
    if args.wave is not None or args.mic:
        validate_recognition_settings(settings)

        with create_audio_stream(args) as stream:
            # generate id
            f_min_dif = open("min_difference.txt")
            min_dif = float(f_min_dif.read())
            f_result = open("result.txt", "w+")
            f_result.write('')
            winsound.PlaySound("ring2.wav", winsound.SND_FILENAME)
            session_id = stream.session_id()
            settings.set_session_id(session_id)
            results = recognizer.recognize(stream, settings)
            [results_speech, results_rr] = print_results(results, stream)

            #result = results_speech[results_rr.index(max(results_rr))]

            #if len(results_rr) >= 1:
            #if results_rr.index(max(results_rr)) == 0:
    def menu_choice_recognition(self, grammar_file):
        vr = VoiceRecording()
        vr.record_voice()

        wave_file = "waves/output6.wav"
        args = SarmataArgs(wave_file, grammar_file)

        settings = SarmataSettings()
        settings.process_args(args)  # load settings from cmd
        if args.grammar is not None:
            settings.load_grammar(args.grammar)

        can_define_grammar = False
        if args.define_grammar:
            if not settings.grammar_name:
                print(
                    "Bad usage. Set BOTH grammar_name and grammar file when define grammar is set True."
                )
                sys.exit(1)
            can_define_grammar = True

        recognizer = SarmataRecognizer(args.address)

        if can_define_grammar:
            define_grammar_response = recognizer.define_grammar(
                args.grammar_name, settings.grammar)
            if define_grammar_response.ok:
                if args.grammar is None:
                    print("Grammar " + args.grammar_name + " removed")
                else:
                    print("Grammar " + args.grammar + " defined as " +
                          args.grammar_name)
            else:
                print("Define grammar error: " + define_grammar_response.error)

        # --------------------------
        # recognize section
        # --------------------------
        if args.wave is not None or args.mic:
            validate_recognition_settings(settings)

            try:
                with create_audio_stream(args) as stream:
                    # generate id
                    session_id = stream.session_id()
                    settings.set_session_id(session_id)

                    results = recognizer.recognize(stream, settings)
                    res_semantic_interpretation = print_results(
                        results, stream)

                    if res_semantic_interpretation == None:
                        raise Exception

                    else:
                        print(res_semantic_interpretation)
                        # zwraca interpretacje
                        return res_semantic_interpretation
            except Exception:
                print("Bład")
                return -1