CommandResult ExpressionInterpreter::interpretCommand(string userInput)
{
    CommandResult result = CommandSuccess;
    string text;
    stringstream ss;
    ss << userInput;

    ExpressionList currentState;
    currentState.push_back(mpRoot);
    ExpressionList nextState;

    while (result == CommandSuccess && !ss.eof())
    {
        ss >> text;
        transform(text.begin(), text.end(), text.begin(), ::tolower);

        ExpressionList::const_iterator iter = currentState.begin();
        ExpressionList::const_iterator end = currentState.end();
        for (; iter != end; ++iter)
        {
            Expression* expr = *iter;
            ExpressionList exprNextList = expr->getNextExpressionClosure(text);
            nextState.splice(nextState.end(), exprNextList);
        }

        if (nextState.size() > 0)
        {
            currentState = nextState;
            nextState.clear();
        }
        else
        {
            mErrorText = "'" + text + "' not recognized.";
            result = CommandInvalid;
        }
    }

    //remove impossible expressions in the final state before checking for ambiguity
    nextState.clear();
    ExpressionList::const_iterator iter = currentState.begin();
    ExpressionList::const_iterator end = currentState.end();
    for (; iter != end; ++iter)
    {
        Expression* expr = *iter;
        if (expr->isExecutable())
        {
            nextState.push_back(expr);
        }
        else
        {
            ExpressionList children = expr->getNextExpressions();

            bool flag = false;

            ExpressionList::const_iterator iter = children.begin();
            ExpressionList::const_iterator end = children.end();
            for (; iter != end; ++iter)
            {
                if ((*iter)->getName()[0] == '[')
                {
                    flag = true;
                }
            }

            if (flag || children.size() == 0)
            {
                nextState.push_back(expr);
            }
        }
    }

    currentState = nextState;

    if (currentState.size() != 1)
    {
        mErrorText = "'" + text + "' ambiguous or incomplete.";
        result = CommandInvalid;
    }

    //run command if executable and non-ambiguous
    if (result == CommandSuccess)
    {
        Expression* expr = *(currentState.begin());

        ExpressionList executables = expr->getClosureExecutables(false);
        if (executables.size() == 1)
        {
            ilmErrorTypes initResult = ilm_init();
            if (ILM_SUCCESS != initResult)
            {
                mErrorText = ILM_ERROR_STRING(initResult);
                result = CommandExecutionFailed;
            }
            else
            {
                Expression* exec = executables.front();
                exec->execute();
                ilm_commitChanges();
                ilm_destroy();
            }
        }
        else if (executables.size() == 0)
        {
            mErrorText = "command is incomplete.";
            result = CommandIncomplete;
        }
        else
        {
            mErrorText = "command is ambiguous.";
            result = CommandIncomplete;
        }
    }

    return result;
}
Пример #2
0
int main(int argc, char *argv[])
{
    ilmErrorTypes result = ILM_FAILED;
    t_ilm_layer layerid = (t_ilm_layer)LAYER_CHROMAKEY;
    t_ilm_surface surfaceid = (t_ilm_surface)SURFACE_CHROMAKEY;
    char* bitmapFile = NULL;
    int o;
    static const char opts[] = "f:c:h";
    static const struct option longopts[] = {
        { "file",  1, NULL, 'f' },
        { "color", 1, NULL, 'c' },
        { "help",  1, NULL, 'h' },
        { NULL, }
    };
    unsigned int rgb[] = {255, 255, 255}; /* Default color key is 'White' */

    while (o = getopt_long(argc, argv, opts, longopts, &o), o > 0) {
        switch (o) {
        case 'f':
            bitmapFile = optarg;
            break;
        case 'c':
            sscanf(optarg, "%d,%d,%d", &rgb[0], &rgb[1], &rgb[2]);
            break;
        case 'h':
            print_usage();
            return 0;
        }
    }

    if (bitmapFile == NULL) {
        print_usage();
        return -1;
    }

    printf("[INFO] bitmap file = %s\n", bitmapFile);
    printf("[INFO] color key   = R(%d),G(%d),B(%d)\n", rgb[0], rgb[1], rgb[2]);

    /* signal handling */
    signal(SIGINT,  sigFunc);
    signal(SIGKILL, sigFunc);

    gRun = 1;
    memset(&g_wlContextStruct, 0x00, sizeof(g_wlContextStruct));

    g_wlContextStruct.ctx_bmp = bmpaccessor_open(bitmapFile);
    if (NULL == g_wlContextStruct.ctx_bmp)
    {
        printf("Failed to create bmp accessor\n");
        return -1;
    }

    do {
        createWLContext();

        result = ilm_init();
        if (ILM_SUCCESS == result)
        {
            printf("ilm_init success\n");
        }
        else
        {
            printf("ilm_init failed\n");
            break;
        }
        result = createILMAttribute(&layerid, &surfaceid, rgb);
        if (result != ILM_SUCCESS)
        {
            break;
        }

        t_ilm_layer layer[] = {1000, 2000, 3000, LAYER_CHROMAKEY};
        ilm_displaySetRenderOrder(0, &layer[0], 4);

        ilm_commitChanges();

        while(gRun) {
            drawImage();
        }

        ilm_surfaceRemove(surfaceid);
        ilm_layerRemove(layerid);
    } while(0);

    ilm_destroy();
    destroyWLContext();

    bmpaccessor_close(g_wlContextStruct.ctx_bmp);

    return 0;
}
void kb (int argc, char *argv[],
         float ip,	/* word insertion penalty */
         float lw,	/* langauge weight */
         float pip)	/* phone insertion penalty */
{
    char *pname = argv[0];
    char hmm_file_name[256];
    int32 num_phones, num_ci_phones;
    int32 i, use_darpa_lm;

    /* FIXME: This is evil.  But if we do it, let's prototype it
       somewhere, OK? */
    unlimit ();		/* Remove memory size limits */

    language_weight = lw;
    insertion_penalty = ip;
    phone_insertion_penalty = pip;

    pconf (argc, argv, kb_param, 0, 0, 0);

    if ((phone_file_name == 0) ||
            (dict_file_name == 0))
        pusage (pname, (Config_t *)kb_param);

    log_info("%s(%d): Reading phone file [%s]\n",
             __FILE__, __LINE__, phone_file_name);
    if (phone_read (phone_file_name))
        exit (-1);
    if (useWDPhonesOnly)
        phone_add_diphones();

    num_ci_phones = phoneCiCount();

    /* Read the distribution map file */
    log_info("%s(%d): Reading map file [%s]\n", __FILE__, __LINE__, mapFileName);

    read_map (mapFileName, TRUE /* useCiTrans compress */);
    log_info("%s(%d): Reading dict file [%s]\n",
             __FILE__, __LINE__, dict_file_name);

    word_dict = dict_new ();
    if (dict_read (word_dict, dict_file_name, phrase_dict_file_name,
                   noise_dict_file_name, !useWDPhonesOnly))
        exit (-1);

    use_darpa_lm = TRUE;

    if (use_darpa_lm) {
        lmSetStartSym (lm_start_sym);
        lmSetEndSym (lm_end_sym);

        /*
         * Read control file describing multiple LMs, if specified.
         * File format (optional stuff is indicated by enclosing in []):
         *
         *   [{ LMClassFileName LMClassFilename ... }]
         *   TrigramLMFileName LMName [{ LMClassName LMClassName ... }]
         *   TrigramLMFileName LMName [{ LMClassName LMClassName ... }]
         *   ...
         * (There should be whitespace around the { and } delimiters.)
         *
         * This is an extension of the older format that had only TrigramLMFilenName
         * and LMName pairs.  The new format allows a set of LMClass files to be read
         * in and referred to by the trigram LMs.  (Incidentally, if one wants to use
         * LM classes in a trigram LM, one MUST use the -lmctlfn flag.  It is not
         * possible to read in a class-based trigram LM using the -lmfn flag.)
         *
         * No "comments" allowed in this file.
         */
        if (lm_ctl_filename) {
            FILE *ctlfp;
            char lmfile[4096], lmname[4096], str[4096];
            lmclass_set_t lmclass_set;
            lmclass_t *lmclass, cl;
            int32 n_lmclass, n_lmclass_used;

            lmclass_set = lmclass_newset();

            E_INFO("Reading LM control file '%s'\n", lm_ctl_filename);

            ctlfp = CM_fopen (lm_ctl_filename, "r");
            if (fscanf (ctlfp, "%s", str) == 1) {
                if (strcmp (str, "{") == 0) {
                    /* Load LMclass files */
                    while ((fscanf (ctlfp, "%s", str) == 1) && (strcmp (str, "}") != 0))
                        lmclass_set = lmclass_loadfile (lmclass_set, str);

                    if (strcmp (str, "}") != 0)
                        E_FATAL("Unexpected EOF(%s)\n", lm_ctl_filename);

                    if (fscanf (ctlfp, "%s", str) != 1)
                        str[0] = '\0';
                }
            } else
                str[0] = '\0';

            /* Fill in dictionary word id information for each LMclass word */
            for (cl = lmclass_firstclass(lmclass_set);
                    lmclass_isclass(cl);
                    cl = lmclass_nextclass(lmclass_set, cl)) {
                kb_init_lmclass_dictwid (cl);
            }

            /* At this point if str[0] != '\0', we have an LM filename */
            n_lmclass = lmclass_get_nclass(lmclass_set);
            lmclass = (lmclass_t *) CM_calloc (n_lmclass, sizeof(lmclass_t));

            /* Read in one LM at a time */
            while (str[0] != '\0') {
                strcpy (lmfile, str);
                if (fscanf (ctlfp, "%s", lmname) != 1)
                    E_FATAL("LMname missing after LMFileName '%s'\n", lmfile);

                n_lmclass_used = 0;

                if (fscanf (ctlfp, "%s", str) == 1) {
                    if (strcmp (str, "{") == 0) {
                        /* LM uses classes; read their names */
                        while ((fscanf (ctlfp, "%s", str) == 1) &&
                                (strcmp (str, "}") != 0)) {
                            if (n_lmclass_used >= n_lmclass)
                                E_FATAL("Too many LM classes specified for '%s'\n",
                                        lmfile);
                            lmclass[n_lmclass_used] = lmclass_get_lmclass (lmclass_set,
                                                      str);
                            if (! (lmclass_isclass(lmclass[n_lmclass_used])))
                                E_FATAL("LM class '%s' not found\n", str);
                            n_lmclass_used++;
                        }
                        if (strcmp (str, "}") != 0)
                            E_FATAL("Unexpected EOF(%s)\n", lm_ctl_filename);

                        if (fscanf (ctlfp, "%s", str) != 1)
                            str[0] = '\0';
                    }
                } else
                    str[0] = '\0';

                if (n_lmclass_used > 0)
                    lm_read_clm (lmfile, lmname,
                                 language_weight, unigramWeight, insertion_penalty,
                                 lmclass, n_lmclass_used);
                else
                    lm_read (lmfile, lmname,
                             language_weight, unigramWeight, insertion_penalty);
            }

            fclose (ctlfp);
            NoLangModel = FALSE;
        }

        /* Read "base" LM file, if specified */
        if (lm_file_name) {
            lmSetStartSym (lm_start_sym);
            lmSetEndSym (lm_end_sym);
            lm_read (lm_file_name, "", language_weight, unigramWeight, insertion_penalty);

            /* Make initial OOV list known to this base LM */
            lm_init_oov ();

            NoLangModel = FALSE;
        }

#ifdef USE_ILM
        /* Init ILM module (non-std-Darpa LM, eg ug/bg cache LM) */
        ilm_init ();
#endif
    }

#if 0
    /* Compute the phrase lm probabilities */
    computePhraseLMProbs ();
#endif

    num_phones = phone_count ();
    numSmds = hmm_num_sseq();
    smds = (SMD *) CM_calloc (numSmds, sizeof (SMD));

    /*
     * Read the hmm's into the SMD structures
     */
    if (useBigHmmFiles) {
        for (i = 0; i < num_ci_phones; i++) {
            sprintf (hmm_file_name, "%s.%s", phone_from_id (i),
                     hmm_ext);

            hmm_tied_read_big_bin (hmm_dir_list, hmm_file_name, smds,
                                   transSmooth, NUMOFCODEENTRIES, TRUE,
                                   transWeight);
        }
    } else {
        for (i = 0; i < num_phones; i++) {
            if ((!useCiTrans) || (phone_id_to_base_id(i) == i)) {
                sprintf (hmm_file_name, "%s.%s", phone_from_id (i), hmm_ext);
                hmm_tied_read_bin (hmm_dir_list, hmm_file_name,
                                   &smds[hmm_pid2sid(i)], transSmooth,
                                   NUMOFCODEENTRIES, TRUE, transWeight);
            }
        }
    }

    /*
     *  Use Ci transitions ?
     */
    if (useCiTrans) {
        for (i = 0; i < num_phones; i++) {
            if (hmm_pid2sid(phone_id_to_base_id(i)) != hmm_pid2sid(i)) {
                /*
                 * Just make a copy of the CI phone transitions
                 */
                memcpy (&smds[hmm_pid2sid(i)], &smds[hmm_pid2sid(phone_id_to_base_id(i))],
                        sizeof (SMD));
            }
        }
    }
    /*
     * Read the distributions
     */
    read_dists (hmm_dir, code1_ext, code2_ext, code3_ext, code4_ext,
                NUMOFCODEENTRIES, hmm_smooth_min, useCiPhonesOnly);
    if (Use8BitSenProb)
        SCVQSetSenoneCompression (8);

    /*
     * Map the distributions to the correct locations
     */
    remap (smds);
}