Example #1
0
int main_dump(int argc, char *argv[], const char *argv0)
{
    int ret = 0, arg_used = 0;
    dump_option_t opt;
    const char *command = argv[0];
    FILE *fpo = stdout, *fpe = stderr;
    crfsuite_model_t *model = NULL;

    /* Parse the command-line option. */
    dump_option_init(&opt);
    arg_used = option_parse(++argv, --argc, parse_dump_options, &opt);
    if (arg_used < 0) {
        ret = 1;
        goto force_exit;
    }

    /* Show the help message for this command if specified. */
    if (opt.help) {
        show_usage(fpo, argv0, command);
        goto force_exit;
    }

    /* Check for the existence of the model file. */
    if (argc <= arg_used) {
        fprintf(fpe, "ERROR: No model specified.\n");
        ret = 1;
        goto force_exit;
    }

    /* Create a model instance corresponding to the model file. */
    if ((ret = crfsuite_create_instance_from_file(argv[arg_used], (void**)&model, FTYPE_NONE))) {
        goto force_exit;
    }

    /* Dump the model. */
    if ((ret = model->dump(model, fpo))) {
        goto force_exit;
    }

force_exit:
    SAFE_RELEASE(model);
    dump_option_finish(&opt);
    return ret;
}
Example #2
0
static void Run(const SUttProcessor *self, SUtterance *utt,
		s_erc *error)
{
	SCrfSuiteUttProc *crfsuiteProc = (SCrfSuiteUttProc*)self;

	crfsuite_model_t * ptr_model = malloc (sizeof(crfsuite_tagger_t));
	crfsuite_tagger_t * ptr_tagger = malloc (sizeof(crfsuite_tagger_t));
	crfsuite_dictionary_t * ptr_attrs = malloc (sizeof(crfsuite_dictionary_t));
	crfsuite_dictionary_t * ptr_labels = malloc (sizeof(crfsuite_dictionary_t));
	crfsuite_instance_t * instance = malloc (sizeof(crfsuite_instance_t));

	/* Initialize model object */
	if ( crfsuite_create_instance_from_file( crfsuiteProc->model_file,
	                                              (void**)&ptr_model
	                                            ) != 0 ) {
		goto exit_cleanup;
	}

	const SRelation* phrase = SUtteranceGetRelation(utt, "Phrase", error);
	if (S_CHK_ERR(error, S_CONTERR,
			  "Run",
			  "Call to \"SUtteranceGetRelation\" failed"))
		return;

	SItem* itrPhrase = SRelationHead( phrase, error );
	if (S_CHK_ERR(error, S_CONTERR,
			  "Run",
			  "Call to \"SRelationHead\" failed"))
		return;

	while ( itrPhrase != NULL )
	{
		/* Obtain the dictionary interface representing the labels in the model. */
		if ( ptr_model->get_labels(ptr_model, &ptr_labels) != 0) {
			goto exit_cleanup;
		}

		/* Obtain the dictionary interface representing the attributes in the model. */
		if ( ptr_model->get_attrs(ptr_model, &ptr_attrs) != 0) {
			goto exit_cleanup;
		}

		/* Obtain the tagger interface. */
		if ( ptr_model->get_tagger(ptr_model, &ptr_tagger) != 0) {
			goto exit_cleanup;
		}

		instance = create_phrase_instance ( itrPhrase, ptr_attrs, ptr_labels, error );
		int *output = calloc(sizeof(int), instance->num_items);
		floatval_t score = 0;

		/* Set the instance to the tagger. */
		if ( ptr_tagger->set(ptr_tagger, instance) != 0) {
			goto exit_cleanup;
		}

		/* Obtain the viterbi label sequence. */
		if (ptr_tagger->viterbi(ptr_tagger, output, &score) != 0) {
			goto exit_cleanup;
		}

		/* Extract the output and insert in the POS attribute */

		const SItem* tokenTMP = SItemPathToItem ( itrPhrase, "daughter.R:Token", error );
		if (S_CHK_ERR(error, S_CONTERR,
				  "Run",
				  "Call to \"SItemPathToItem\" failed"))
			return;

		SItem* token = SItemParent (tokenTMP, error);
		if (S_CHK_ERR(error, S_CONTERR,
				  "Run",
				  "Call to \"SItemParent\" failed"))
			return;

		const SItem* lastToken = SItemPathToItem ( itrPhrase, "n.daughter.R:Token.parent", error );
		if (S_CHK_ERR(error, S_CONTERR,
				  "Run",
				  "Call to \"SItemPathToItem\" failed"))
			return;

		int i = 0;

		while ( token != NULL && token != lastToken )
		{
			const char * str = malloc (sizeof (char)*16);
			ptr_labels->to_string (ptr_labels, output[i], &str);

			i += 1;

			SItemSetString (token, "POS", str, error);
			if (S_CHK_ERR(error, S_CONTERR,
					  "Run",
					  "Call to \"SItemSetString\" failed"))
				return;

			token = SItemNext(token, error);
			if (S_CHK_ERR(error, S_CONTERR,
					  "Run",
					  "Call to \"SItemNext\" failed"))
				return;
		}

		free(output);
		crfsuite_instance_finish(instance);

		itrPhrase = SItemNext(itrPhrase, error);
		if (S_CHK_ERR(error, S_CONTERR,
				  "Run",
				  "Call to \"SItemNext\" failed"))
			return;
	}

	/* here all is OK */

	S_UNUSED(utt);

exit_cleanup:
	if ( ptr_model != NULL )
		free ( ptr_model );

	if ( ptr_tagger != NULL )
		free ( ptr_tagger );

	if ( ptr_attrs != NULL )
		free ( ptr_attrs );

	if ( ptr_labels != NULL )
		free ( ptr_labels );

	if ( instance != NULL )
		free ( instance );

}