
#include "quiz.h"


#define MAX_SPLIT_WORDS		30
#define DEBUG(parms)


int num_split_words = 0;
int cur_split_word = 0;
struct thai_phrase *split_words[ MAX_SPLIT_WORDS ];


struct thai_phrase *
dict_search ( sentence , cur_try )
char *sentence;			/* sentence of thai characters */
struct thai_phrase *cur_try;	/* current try, NULL means first try */
{
	struct thai_phrase *best , *p;
	int best_len , past_cur_try , max_len;
	int i;


	if ( cur_try != NULL )
		max_len = strlen ( cur_try->thai );
	else
		max_len = 1000;
	past_cur_try = FALSE;
	best = NULL;
	best_len = 0;

	/* ok, now we scan through the dictionary looking for the longest */
	/* entry which is either before and shorter than the current try */
	/* or else after and shorter than or equal to the current try. */
	/* Looking for the longest entry first means that longer phrases */
	/* in the dictionary will be used first (they are more likely to */
	/* be accurate) */

	for ( p = word_head.next; p != NULL; p = p->next ) {

		/* compare sentence to thai phrase */

		for ( i = 0; p->thai[i] != '\0'; i++ ) {
			if ( sentence[i] != p->thai[i] )
				break;
		}

		if ( p->thai[i] == '\0' ) {

			/* all of phrase matched start of sentence */

			if ( i > best_len ) {	/* otherwise dont bother */
				if ( past_cur_try ) {
					if ( i <= max_len ) {
						best = p;
						best_len = i;
					}
				}
				else {
					if ( i < max_len ) {
						best = p;
						best_len = i;
					}
				}
			}
		}
		if ( p == cur_try )
			past_cur_try = TRUE;
	}
	if ( best == NULL ) {
		DEBUG ( ( "best is null! scan for %s\n" , sentence ) );
	}
	else {
		DEBUG ( ( "scan for %s -- best is %s (%s)\n" , sentence , best->english , best->thai ) );
	}
	return ( best );
}



/* returns NULL if it works, or else a pointer to the bit it does not know */

char *
split_sentence ( sentence )
char *sentence;
{
	char *parse;
	char *best_parse;	/* keep track of longest parse made */
	struct thai_phrase *p;
	int cur_word;


	best_parse = sentence;
	parse = sentence;
	split_words[0] = NULL;	/* mark as look for first entry */
	cur_word = 0;

	while ( cur_word >= 0 ) {
		DEBUG ( ( "Scanned up to char %d\n" , parse - sentence ) );
		if ( split_words[ cur_word ] != NULL )
			parse -= strlen ( split_words[ cur_word ]->thai );
		DEBUG ( ( "backed up to char %d\n" , parse - sentence ) );
		p = dict_search ( parse , split_words[ cur_word ] );
		if ( p != NULL ) {
			parse += strlen ( p->thai );
			DEBUG ( ( "skipped forward up to char %d\n" , parse - sentence ) );
			if ( parse > best_parse )
				best_parse = parse;
			split_words[ cur_word ] = p;
			cur_word++;
			DEBUG ( ( "cur word ++ now %d\n" , cur_word ) );
			split_words[ cur_word ] = NULL;
			if ( *parse == '\0' ) {
				num_split_words = cur_word;
				cur_split_word = 0;
				DEBUG ( ( "GOT IT! %d words\n" , num_split_words ) );
				return ( NULL );	/* DONE! */
			}
		}
		else {
			/* search failed, back up a word and try something else */
			cur_word--;
			DEBUG ( ( "cur word -- now %d\n" , cur_word ) );
		}
	}

	/* failed, return pointer to likey word that was not in dictionary */
	/* and let user try to work out what word was not known */

	DEBUG ( ( "Failed: closest was %d chars\n" , best_parse - sentence ) );
	return ( best_parse );
}
