Пример #1
0
static void command_sentence(char *file)
{
	char	line[MAX_LLEN];

	if (strlen(file) > 0)
	{
		command_input(file);
		strcpy(line, "");

		// Gather lines together

		while (strlen(line) < 9000 && read_line(line+strlen(line), 1, 0))
		{
			strcat(line, " ");
		}

		mptag->load(line);
	} else
	{
		// Read a single line from the current input file to get a sentence

		if (read_line(line, 1, 0))
		{
			mptag->load(line);
		}
	}

}
Пример #2
0
main()
#endif
{
	FILE			*fp = stdin;	  /* File pointer, default stdin */
	CRITICALS		*tuples;
	int				i;
	int				n;
	CUBE_INFO		*p;

	printf("\ntuples - version %1.2f\n\n", VERSION_NUM);

#ifndef NO_CMD_LINE
	/* Check the command line and open file if required */
	command_line(&fp,argc, argv, &n);
#else
	/* Read command info from standard input */
	command_input(&fp, &n);
#endif

	/* Read in info from the file, setting up Fsa and so on... */
	switch (read_file_info(fp)) {
		case FORMAT_ERROR:
			ERROR(FORMAT);
		case NOT_A_GEN:
			ERROR(GEN_ERR);
	}
	fclose(fp);	/* No more reading required */

	tuples = find_n_criticals(n);
	printf("Number of Critical %d-tuples = %d\n\n", n, tuples->num);

	for (i=0; i < tuples->num; i++) {
		printword(stdout, tuples->info[i]->word);
		printf("\t");
		for (p = tuples->info[i]->vert; p != NULL; p = p->next)
			printf("[%d,%d] ", p->pos, p->len);
		printf("\n");
	}
	/* Clean up */
	delete_criticals(tuples);
	return 0;
}
Пример #3
0
main(int argc, char **argv)
{
	int	i, j, t;
	double	m;
	char	line[MAX_LLEN], com[MAX_LLEN], arg1[MAX_LLEN], arg2[MAX_LLEN];
	int	used = 0;
	char	*entry;

	srandom(time(NULL));

	file_stack[0] = stdin;
	num_files = 1;

	mptag = new MPtag;

// #define INTERNAL_TOKENIZE

	mptag->set_untag("UNTAGGED");

#ifdef INTERNAL_TOKENIZE

	extern int process_tokenizer_args(int argc, char **argv);
	extern int run_tokenizer(FILE *ofp);

	process_tokenizer_args(argc, argv);
	int fd[2];
	pipe(fd);
	FILE *ifp = fdopen(fd[0], "r");
	FILE *ofp = fdopen(fd[1], "w");
	run_tokenizer(ofp);
	fclose(ofp);
	file_stack[0] = ifp;
	num_files = 1;
#else

	for (i = 1; i < argc; ++i)
	{
		if (argv[i][0] != '-' && num_files == 1)
		{
			num_files = 0;
			command_input(argv[i]);
		}
	}
#endif

	// Read the input file

	while (read_line(line, 0, 0))
	{
		get_command(line, com, arg1, arg2);
		fflush(stdout);

		// Options affecting the program

		if (strcmp(com, "input") == 0)
		{
			command_input(arg1);
		} else if (strcmp(com, "echo") == 0)
		{
			printf("%s\n", line + 5);
		} else if (strcmp(com, "verbose") == 0)
		{
			option_verbose = atoi(arg1);
			if (option_verbose) printf("verbose %s\n", arg1);
		} else if (strcmp(com, "exit") == 0)
		{
			exit(0);

		// Set options within the tagger

		} else if (strcmp(com, "adhoc") == 0)
		{
			if (option_verbose) printf("adhoc %s\n", arg1);
			if (strcmp(arg1, "none") == 0) mptag->set_adhoc_none();
			else if (strcmp(arg1, "medpost") == 0) mptag->set_adhoc_medpost();
			else if (strcmp(arg1, "penn") == 0) mptag->set_adhoc_penn();
		} else if (strcmp(com, "untag") == 0)
		{
			mptag->set_untag(arg1);

		// Initialize state transition probabilities (ngrams)

		} else if (strcmp(com, "ngrams") == 0)
		{
			mptag->read_ngrams(arg1);
		} else if (strcmp(com, "init") == 0)
		{
			if (option_verbose) printf("init\n");
			mptag->norm_ngrams();
		} else if (strcmp(com, "smooth") == 0)
		{
			if (option_verbose) printf("smooth\n");
			mptag->smooth_ngrams();

		// Initialize the lexicon

		} else if (strcmp(com, "lex") == 0)
		{
			if (option_verbose) printf("lex %s %s\n", arg1, arg2);
			if (mptag->lex) delete mptag->lex;
			mptag->lex = new MPlex(mptag->num_tags, atoi(arg1), arg2, OPTION_USE_CODES);
			mptag->backoff(NULL);
		} else if (strcmp(com, "addlex") == 0)
		{
			if (option_verbose) printf("addlex %s\n", arg1);
			if (mptag->lex) mptag->lex->addfile(arg1);
		} else if (strcmp(com, "rmlex") == 0)
		{
			if (option_verbose) printf("rmlex %s\n", arg1);
			if (mptag->lex) mptag->lex->rmfile(arg1);
		} else if (strcmp(com, "addsmoothing") == 0)
		{
			if (option_verbose) printf("add %g\n", mptag->add_smoothing);
			mptag->add_smoothing = atof(arg1);
		} else if (strcmp(com, "backoff") == 0)
		{
			if (option_verbose) printf("backoff %s\n", arg1);
			mptag->add_smoothing = 0.0;
			mptag->backoff(arg1);

		// Load a sentence

		} else if (strcmp(com, "sentence") == 0)
		{
			command_sentence(arg1);		// this is mptag->load(tokenized-text)

		// Perform tagging

		} else if (strcmp(com, "compute") == 0)
		{
			mptag->compute();
		} else if (strcmp(com, "viterbi") == 0)
		{
			mptag->viterbi();
		} else if (strcmp(com, "baseline") == 0)
		{
			mptag->baseline();

		// Invoke the printing functions of the tagger

		} else if (strcmp(com, "print") == 0)
		{
			mptag->print(0);
		} else if (strcmp(com, "printfull") == 0)
		{
			mptag->print(1);
		} else if (strcmp(com, "printsent") == 0)
		{
			if (option_verbose) printf("printsent\n");
			mptag->print(2);
		}
	}
}
Пример #4
0
main()
#endif
{
	FILE			*fp = stdin;	  /* File pointer, default stdin */
	CRITICALS		*tuples, *lower_dim_tuples;
	int				i, temp;
	BOUNDARY		**boundary;
	CUBE			*cube;
	int				n, num_rows, num_cols;
	MATRIX			matrix, tmatrix;

	printf("\ncmat - version %1.2f\n\n", VERSION_NUM);

#ifndef NO_CMD_LINE
	/* Check the command line and open file if required */
	command_line(&fp,argc, argv, &n);
#else
	/* Read command info from standard input */
	command_input(&fp, &n);
#endif

	/* Read in info from the file, setting up Fsa and so on... */
	switch (read_file_info(fp)) {
		case FORMAT_ERROR:
			ERROR(FORMAT);
		case NOT_A_GEN:
			ERROR(GEN_ERR);
	}
	fclose(fp);	/* No more reading required */

	tuples = find_n_criticals(n);
	/* remove_inverse_criticals(&tuples);  We are ignoring inverse tuples */

	/* Calc boundaries and store in an array */
	boundary = NEW(BOUNDARY *, tuples->num);

	for (i = 0; i < tuples->num; i++) {
		boundary[i] = calc_bound(cube =
					create_n_cube(tuples->info[i]->word,
					tuples->info[i]->vert, n), TRIVIAL | RETAIN_INV_TUPLES);
					/* We are ignoring inverse tuples */
		delete_cube(cube);
	}

	lower_dim_tuples = find_n_criticals(n-1);
	/* remove_inverse_criticals(&lower_dim_tuples);  Ignoring inverse tuples */

	num_rows = tuples->num;
	/* Create the matrix */
	matrix = make_matrix_n(boundary, lower_dim_tuples, &num_rows);
	num_cols = lower_dim_tuples->num;

	/* It is often useful to have rows and columns the other way */
	/* Get transpose, and swap rows and cols */
	tmatrix = transpose(matrix, num_cols, num_rows);
	temp = num_rows;
	num_rows = num_cols;
	num_cols = temp;

	/* Display....for now */
	printf("\nMatrix: rows = %d, cols = %d\n\n", num_rows, num_cols);
	show_matrix(stdout, tmatrix, num_cols, num_rows, 20);

	/*printf("\nMatrix after Gaussian Elimination...\n");
	gaussian_elim(matrix, num_cols, num_rows);
	show_matrix(stdout, matrix, num_cols, num_rows);

	rank_criticals_n = count_non_zero_rows(matrix, num_cols, num_rows);*/

	/* Clean up */
	for (i=0; i < tuples->num - Num_gens; i++)
	delete_boundary(boundary[i]);
	free(boundary);
	delete_criticals(tuples);
	delete_criticals(lower_dim_tuples);
	
	delete_matrix(matrix, num_cols);
	delete_matrix(tmatrix, num_rows);

	return 0;
}
Пример #5
0
/* command wrapper function: make sure the game is able to run commands, perform
 * logging and generate reasonable return values for api clients with no access
 * to internal state */
int nh_command(const char *cmd, int rep, struct nh_cmd_arg *arg)
{
    int cmdidx, cmdresult, pre_moves;
    unsigned int pre_rngstate;

    if (!program_state.game_running)
	return ERR_GAME_NOT_RUNNING;
    
    cmdidx = get_command_idx(cmd);
    if (program_state.viewing && (cmdidx < 0 || !(cmdlist[cmdidx].flags & CMD_NOTIME)))
	return ERR_COMMAND_FORBIDDEN; /*  */
	
    if (!api_entry_checkpoint()) {
	/* terminate() in end.c will arrive here */
	if (program_state.panicking)
	    return GAME_PANICKED;
	if (!program_state.gameover)
	    return GAME_SAVED;
	if (program_state.forced_exit)
	    return ERR_FORCED_EXIT;
	return GAME_OVER;
    }
    
    /* if the game is being restored, turntime is set in restore_read_command */
    turntime = time(NULL);
    log_command(cmdidx, rep, arg);
    
    pre_rngstate = mt_nextstate();
    pre_moves = moves;
    
    /* do the deed. command_input returns -1 if the command completed normally */
    cmdresult = command_input(cmdidx, rep, arg);
    
    /* make sure we actually want this command to be logged */
    if (cmdidx >= 0 && (cmdlist[cmdidx].flags & CMD_NOTIME) &&
	pre_rngstate == mt_nextstate() && pre_moves == moves)
	log_revert_command(); /* nope, cut it out of the log */
    else
	log_command_result(); /* log the result */

    api_exit(); /* no unsafe operations after this point */
    
    if (cmdresult != -1)
	return cmdresult;
    
    /*
     * performing a command can put the game into several different states:
     *  - the command completes immediately: a simple move or an attack etc
     *    multi == 0, occupation == NULL
     *  - if a count is given, the command will (usually) take count turns
     *    multi == count (> 0), occupation == NULL
     *  - the command may cause a delay: for ex. putting on or removing armor
     *    multi == -delay (< 0), occupation == NULL
     *    multi is incremented in you_moved
     *  - the command may take multiple moves, and require a callback to be
     *    run for each move. example: forcing a lock
     *    multi >= 0, occupation == callback
     */
    if (multi >= 0 && occupation)
	return OCCUPATION_IN_PROGRESS;
    else if (multi > 0)
	return MULTI_IN_PROGRESS;
    else if (multi < 0)
	return POST_ACTION_DELAY;
    
    return READY_FOR_INPUT;
}