<commit_before>package com.jetbrains.idear;

import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.diagnostic.Logger;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;

public class ASRServiceImpl implements ASRService {

    private final Thread speechThread = new Thread(new ASRControlLoop(), "ARS Thread");

    private static final String ACOUSTIC_MODEL = "resource:/edu.cmu.sphinx.models.en-us/en-us";
    private static final String DICTIONARY_PATH = "resource:/edu.cmu.sphinx.models.en-us/cmudict-en-us.dict";
    private static final String GRAMMAR_PATH = "resource:/com.jetbrains.idear/dialog/";

    private static final Logger logger = Logger.getInstance(ASRServiceImpl.class);

    private LiveSpeechRecognizer recognizer;
    private Robot robot;

    private AtomicReference<Status> status = new AtomicReference<>(Status.INACTIVE);

    /* package */ void init() {
        Configuration configuration = new Configuration();

        configuration.setAcousticModelPath(ACOUSTIC_MODEL);
        configuration.setDictionaryPath(DICTIONARY_PATH);
        configuration.setGrammarPath(GRAMMAR_PATH);
        configuration.setUseGrammar(true);

        configuration.setGrammarName("dialog");

        try {

            recognizer = new LiveSpeechRecognizer(configuration);

        } catch (IOException e) {
            System.err.println("Couldn't initialize speech recognizer.");
        }

        try {

            robot = new Robot();

        } catch (AWTException e) {
            e.printStackTrace();
        }

        // Fire up control-loop

        speechThread.start();
    }

    /* package */ void dispose() {

        // Deactivate in the first place, therefore actually
        // prevent activation upon the user-input

        deactivate();

        terminate();
    }

    private Status setStatus(Status s) {
        return status.getAndSet(s);
    }

    @Override
    public Status getStatus() {
        return status.get();
    }

    @Override
    public Status activate() {
        if (getStatus() == Status.ACTIVE)
            return Status.ACTIVE;

        recognizer.startRecognition(true);

        return setStatus(Status.ACTIVE);
    }

    @Override
    public Status deactivate() {
        if (getStatus() == Status.INACTIVE)
            return Status.INACTIVE;

        recognizer.stopRecognition();

        return status = Status.INACTIVE;
    }

    private class ASRControlLoop implements Runnable {
        @Override
        public void run() {
            while (!isTerminated()) {
                if (isActive()) {
                    String result = recognizer.getResult().getHypothesis();

                    logger.debug("Recognized: " + result);

                    if (result.startsWith("open")) {
                        if (result.endsWith("settings")) {
                            invokeAction(IdeActions.ACTION_SHOW_SETTINGS);<commit_msg>(ARS): Ignore exceptions from Sphinx during deactivation<commit_after>package com.jetbrains.idear;

import com.intellij.ide.DataManager;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.diagnostic.Logger;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;

public class ASRServiceImpl implements ASRService {

    private final Thread speechThread = new Thread(new ASRControlLoop(), "ARS Thread");

    private static final String ACOUSTIC_MODEL = "resource:/edu.cmu.sphinx.models.en-us/en-us";
    private static final String DICTIONARY_PATH = "resource:/edu.cmu.sphinx.models.en-us/cmudict-en-us.dict";
    private static final String GRAMMAR_PATH = "resource:/com.jetbrains.idear/dialog/";

    private static final Logger logger = Logger.getInstance(ASRServiceImpl.class);

    private LiveSpeechRecognizer recognizer;
    private Robot robot;

    private AtomicReference<Status> status = new AtomicReference<>(Status.INACTIVE);

    /* package */ void init() {
        Configuration configuration = new Configuration();

        configuration.setAcousticModelPath(ACOUSTIC_MODEL);
        configuration.setDictionaryPath(DICTIONARY_PATH);
        configuration.setGrammarPath(GRAMMAR_PATH);
        configuration.setUseGrammar(true);

        configuration.setGrammarName("dialog");

        try {

            recognizer = new LiveSpeechRecognizer(configuration);

        } catch (IOException e) {
            System.err.println("Couldn't initialize speech recognizer.");
        }

        try {

            robot = new Robot();

        } catch (AWTException e) {
            e.printStackTrace();
        }

        // Fire up control-loop

        speechThread.start();
    }

    /* package */ void dispose() {

        // Deactivate in the first place, therefore actually
        // prevent activation upon the user-input

        deactivate();

        terminate();
    }

    private Status setStatus(Status s) {
        return status.getAndSet(s);
    }

    @Override
    public Status getStatus() {
        return status.get();
    }

    @Override
    public Status activate() {
        if (getStatus() == Status.ACTIVE)
            return Status.ACTIVE;

        recognizer.startRecognition(true);

        return setStatus(Status.ACTIVE);
    }

    @Override
    public Status deactivate() {
        if (getStatus() == Status.INACTIVE)
            return Status.INACTIVE;

        Status s;

        try {

            recognizer.stopRecognition();

        } finally {
            s = setStatus(Status.INACTIVE);
        }

        return s;
    }

    private void terminate() {
        setStatus(Status.TERMINATED);
    }

    private class ASRControlLoop implements Runnable {
        @Override
        public void run() {
            while (!isTerminated()) {
                if (isActive()) {
                    String result = recognizer.getResult().getHypothesis();

                    logger.debug("Recognized: " + result);

                    if (result.startsWith("open")) {
                        if (result.endsWith("settings")) {
                            invokeAction(IdeActions.ACTION_SHOW_SETTINGS);
{'fraction_non_alphanumeric': 0.10128343899798979, 'fraction_numerical': 0.0, 'mean_word_length': 2.4532835024025625, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6530208', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1724, 'n_words': 376}