Esempio n. 1
0
void letterChain(char *inFile, char *outFile, int maxSize)
/* letterChain - Make Markov chain of letters in text. */
{
struct dlList *ll = dlListNew();
int llSize = 0;
int c;
FILE *in = mustOpen(inFile, "r");
FILE *out;
struct dlNode *node;
UBYTE *s;
struct trie *trie;

AllocVar(trie);

while ((c = getc(in)) >= 0)
    {
    if (llSize < maxSize)
        {
	s = needMem(1);
	*s = c;
	dlAddValTail(ll, s);
	++llSize;
	if (llSize == maxSize)
	    addToTrie(trie, ll);
	}
    else
        {
	node = dlPopHead(ll);
	s = node->val;
	*s = c;
	dlAddTail(ll, node);
	addToTrie(trie, ll);
	}
    }
if (llSize < maxSize)
    addToTrie(trie, ll);
while ((node = dlPopHead(ll)) != NULL)
    {
    addToTrie(trie, ll);
    freeMem(node->val);
    freeMem(node);
    }
dlListFree(&ll);
carefulClose(&in);

out = mustOpen(outFile, "w");
rDumpTrie(0, trie->useCount, trie, out);

carefulClose(&out);
}
Esempio n. 2
0
void addToTrie(Node *node, int from, int to) {
	int ch;

	node->degree++;
	if (from == to)
		return;
	
	ch = string[from] - ALPHA_BASE;
	if (node->children[ch] == NULL)
		node->children[ch] = createNode();
	
	addToTrie(node->children[ch], from+1, to);
}
Esempio n. 3
0
int main(int argc, char **argv) {
	int i, n;
	Node *root;
	while (scanf("%s", string) != EOF) {
		poolPos = 0;
		n = strlen(string);
		root = createNode();
		for (i = 0; i < n; i++)
			addToTrie(root, i, n);
		
		findSuffix(root);
		getSuffix(root, 0);
		printf("%s\n", suffix);
		
//		destroyNode(root);
	}
	
	return EXIT_SUCCESS;
}
Esempio n. 4
0
// Builds add, rep and rem tries from given content of transformations file
int trieFromFile(char *data) {
    char *string1;
    char *string2;
    wchar_t *wstr1;
    wchar_t *wstr2;
    int w1;
    int w2;
    double v;
    int datalen;
    int i;
    int j;

    datalen = strlen(data);
    string1 = malloc(2);
    string2 = malloc(2);

    i = 0;
    j = 0;


    while(i < datalen) {
        /*
        *   Setting weights of default edit operations:
        *   the line must begin with character '>', followed
        *   by "add" for addition, "rep" for replacement and
        *   "del" for deletion operation; after the operation
        *    marker, ':' is placed and finally comes the new
        *    weight as double.
        */
        if(data[i] == '>') {
            i = i +1;
            /* Change weight of default add operation */
            if(strncmp((char *)(data+i), "add", 3) == 0) {
                i +=4;
                j = i;
                while(data[j] != '\n' && data[j] != '\r' && j < strlen(data))
                    j++;
                add = findValue(data, i, j);
                if(data[j] == '\r') /* In case we are under Windows */
                    j = j + 2;
                else j = j+1;
                i = j;
            }
            /* Change weight of default replace operation */
            else if(strncmp((char *)(data+i), "rep", 3) == 0) {
                i +=4;
                j = i;
                while(j < strlen(data) && data[j] != '\n' && data[j] != '\r') j++;
                rep = findValue(data, i, j);
                if(data[j] == '\r') /* In case we are under Windows */
                    j = j + 2;
                else j = j+1;
                i = j;
            }
            /* Change weight of default remove operation */
            else if(strncmp((char *)(data+i), "rem", 3) == 0) {
                i +=4;
                j = i;
                while(j < strlen(data) && data[j] != '\n' && data[j] != '\r') j++;
                rem = findValue(data, i, j);
                if(data[j] == '\r') /* In case we are under Windows */
                    j = j + 2;
                else j = j+1;
                i = j;
            }
        }
        /*
        *  Setting weights of generalized edit distance transformations;
        */
        else {
            j = i;
            /* Locate the left side string of transformation */
            while(data[j] != ':') j++;
            string1 = (char *)realloc(string1, (j-i+1));

            if(string1 == NULL) {
                perror("Memory");
                exit(1);
            }
            string1[j-i] ='\0';
            strncpy(string1, (data +i), (j-i));


            /* Locate the right side string of transformation */
            j++;
            i = j;
            while(data[j] != ':') j++;
            string2 = (char *)realloc(NULL, (j-i+1));

            if(string2 == NULL) {
                perror("Memory");
                exit(1);
            }
            string2[(j-i)]='\0';
            strncpy(string2, (data +i), (j-i));
            j++;
            i = j;
            while(data[j] != '\n' && data[j] != '\r' && j < datalen) j++;

            /* Find the weight of transformation  */
            v =  findValue(data, i, j);

            /* According to the type of transformation, add into suitable trie */
            if(strlen(string1) == 0) {
                /* Add to add-operations trie */
                w2 = mbstowcs(NULL, string2, 0);
                wstr2 = (wchar_t *)localeToWchar(string2);
                if(caseInsensitiveMode)
                    wstr2 = makeStringToIgnoreCase(wstr2, w2);
                addToARTrie(addT, wstr2, w2, v);
                free(wstr2);
                free(string2);
            } 			else if(strlen(string2) == 0) {
                /* Add to remove-operations trie */
                w1 = mbstowcs(NULL, string1, 0);
                wstr1 = (wchar_t *)localeToWchar(string1);
                if(caseInsensitiveMode)
                    wstr1 = makeStringToIgnoreCase(wstr1, w1);
                addToARTrie(remT, wstr1, w1, v);
            } else {
                /* Add to replace-operations trie */
                wstr2 = (wchar_t *)localeToWchar(string2);
                wstr1 = (wchar_t *)localeToWchar(string1);
                w1 = mbstowcs(NULL, string1, 0);
                w2 = mbstowcs(NULL, string2, 0);
                if(caseInsensitiveMode) {
                    wstr1 = makeStringToIgnoreCase(wstr1, w1);
                    wstr2 = makeStringToIgnoreCase(wstr2, w2);
                }
                addToTrie(wstr1, w1, wstr2,v);
                free(string2);
                free(wstr1);
            }
            if(data[j] == '\r') /* In case we are under Windows */
                j = j + 2;
            else
                j = j+1;
            i = j;
        }
    }
    free(string1);
    /* Release the memory under data. */
    munmap(data, strlen(data));
    return 0;
}