Ejemplo n.º 1
0
StringValueList* groupSymbols(char * input, char *alphabet)
{
	if(input == NULL || alphabet == NULL){
		return NULL;
	}

	char *inputCopy = NULL;
	StringValueList* auxExpandPrefix = NULL;
	StringValueList* symbolsGrouped = malloc(sizeof(StringValueList));
	initStringValueList(symbolsGrouped);
	int* symbolCount = NULL;
	char *bestCombination = NULL;
	double bestEvaluation = 0.0;
	double evaluation = 0.0;
	int inputLength = 0;

	
	inputLength = strlen(input);
	inputCopy = malloc((inputLength + 1) * sizeof(char));
	inputCopy = strncpy(inputCopy, input, inputLength);
	inputCopy[inputLength] = '\0';


	do{
		/* Contamos cada simbolo del alfabeto en input. */
		symbolCount = charCount(inputCopy, alphabet);
		bestEvaluation = 0;
		/* Buscamos el simbolo que tiene una evaluacion mayor para seguir buscando un patron a partir de el. */
		for(int i = 0; i < strlen(alphabet); i++){
			evaluation = evaluate(symbolCount[i], strlen(input), 1);
			if (evaluation > bestEvaluation){
				free(bestCombination);
				bestCombination = createString(alphabet[i]);
				bestEvaluation = evaluation;
			} 
		}
		free(symbolCount);

		/* A partir del simbolo mas puntos, se empiezan a buscar subcadenas a partir de el en input */
		//printf("Mejor simbolo encontrado: %s -> %.3f\n", bestCombination, bestEvaluation);
		auxExpandPrefix = malloc(sizeof(StringValueList));
		initStringValueList(auxExpandPrefix);
		expandBestCombination(inputCopy, alphabet, bestCombination, bestEvaluation, auxExpandPrefix);
		/* Insertamos el elemento de mas valor en la lista de simbolos agrupados. */
		int posMaxValue = getPosMaxStringValue(auxExpandPrefix);
		addStringValue(symbolsGrouped, strdup(auxExpandPrefix->string[posMaxValue]), auxExpandPrefix->value[posMaxValue]);
		freeStringValueList(auxExpandPrefix);
		/* Eliminamos de la copia de input las repeticiones de la agrupacion encontrada, sustituyendola por BLANK_CHAR */
		//printf("%s\n", inputCopy);
		deleteSubstring(inputCopy, symbolsGrouped->string[symbolsGrouped->length - 1]);
		
		inputLength = countValidCharacters(inputCopy);
		//printf("%s\n", inputCopy);
	/* Repetimos todo el proceso hasta que se haya agrupado todo el input */
	} while(inputLength != 0);

	free(inputCopy);
	free(bestCombination);
	return symbolsGrouped;
}
Ejemplo n.º 2
0
void expandBestCombination(char * input, char *alphabet, char *prefix, double prefixValue, StringValueList* expansionList)
{
	int nPositions1 = 0;
	int nPositions2 = 0;
	int nSymbols = strlen(alphabet);
	char * newPrefix1 = NULL;
	char * newPrefix2 = NULL;
	int * positions1 = NULL;
	int * positions2 = NULL;
	double newPrefixValue1 = 0.0;
	double newPrefixValue2 = 0.0;
	int newPrefixLenght = strlen(prefix) + 1;
	addStringValue(expansionList, strdup(prefix), prefixValue);
	for(int i = 0; i < nSymbols; i++){
		newPrefix1 = malloc((newPrefixLenght + 1) * sizeof(char));
		newPrefix1 = strncpy(newPrefix1, prefix, strlen(prefix));
		newPrefix1[newPrefixLenght - 1] = alphabet[i];
		newPrefix1[newPrefixLenght] = '\0';

		newPrefix2 = malloc((newPrefixLenght + 1) * sizeof(char));
		strncpy(newPrefix2 + 1, prefix, strlen(prefix));
		newPrefix2[0] = alphabet[i];
		newPrefix2[newPrefixLenght] = '\0';

		positions1 = searchSubstring(input, newPrefix1, &nPositions1, !OVERLAPPED_SEARCH);
		positions2 = searchSubstring(input, newPrefix2, &nPositions2, !OVERLAPPED_SEARCH);
		free(positions1);
		free(positions2);

		newPrefixValue1 = evaluate(nPositions1, strlen(input), newPrefixLenght);
		newPrefixValue2 = evaluate(nPositions2, strlen(input), newPrefixLenght);
		//printf("\t%s -> %.3f\n", newPrefix1, newPrefixValue1);
		//printf("\t%s -> %.3f\n", newPrefix2, newPrefixValue2);
		if(newPrefixValue1 >= newPrefixValue2 && newPrefixValue1 >= prefixValue * 0.7){
			//printf("Expandiendo: %s con valor: %.3f\n", newPrefix1, newPrefixValue1);
			expandBestCombination(input, alphabet, newPrefix1, newPrefixValue1, expansionList);
		}
		if(newPrefixValue2 >= newPrefixValue1 && newPrefixValue2 >= prefixValue * 0.7){
			//printf("Expandiendo: %s con valor: %.3f\n", newPrefix2, newPrefixValue2);
			expandBestCombination(input, alphabet, newPrefix2, newPrefixValue2, expansionList);
		}

		free(newPrefix1);
		free(newPrefix2);
	}
}
Ejemplo n.º 3
0
/*  scanXmlEntry
 *
 *  We've found a '<' character.  We ignore this if it is a comment or a
 *  directive.  If it is something else, then whatever it is we are looking
 *  at is bogus.  Returning NULL stops processing.
 */
static char const*
scanXmlEntry( char const* pzName, tOptionValue* pRes )
{
    size_t nameLen = 1, valLen = 0;
    char const*   pzScan = ++pzName;
    char const*   pzVal;
    tOptionValue  valu;
    tOptionValue* pNewVal;
    tOptionLoadMode save_mode = option_load_mode;

    if (! IS_VAR_FIRST_CHAR(*pzName)) {
        switch (*pzName) {
        default:
            pzName = NULL;
            break;

        case '!':
            pzName = strstr( pzName, "-->" );
            if (pzName != NULL)
                pzName += 3;
            break;

        case '?':
            pzName = strchr( pzName, '>' );
            if (pzName != NULL)
                pzName++;
            break;
        }
        return pzName;
    }

    pzScan++;
    while (IS_VALUE_NAME_CHAR( (int)*pzScan ))  { pzScan++; nameLen++; }
    if (nameLen > 64)
        return NULL;
    valu.valType = OPARG_TYPE_STRING;

    switch (*pzScan) {
    case ' ':
    case '\t':
        pzScan = parseAttributes(NULL, (char *)(intptr_t)pzScan,
	    &option_load_mode, &valu );
        if (*pzScan == '>') {
            pzScan++;
            break;
        }

        if (*pzScan != '/') {
            option_load_mode = save_mode;
            return NULL;
        }
        /* FALLTHROUGH */

    case '/':
        if (*++pzScan != '>') {
            option_load_mode = save_mode;
            return NULL;
        }
        addStringValue(&(pRes->v.nestVal), pzName, nameLen, NULL, (size_t)0);
        option_load_mode = save_mode;
        return pzScan+1;

    default:
        option_load_mode = save_mode;
        return NULL;

    case '>':
        pzScan++;
        break;
    }

    pzVal = pzScan;

    {
        char z[68];
        char* pzD = z;
        int  ct = nameLen;
        char const* pzS = pzName;

        *(pzD++) = '<';
        *(pzD++) = '/';

        do  {
            *(pzD++) = *(pzS++);
        } while (--ct > 0);
        *(pzD++) = '>';
        *pzD = NUL;

        pzScan = strstr( pzScan, z );
        if (pzScan == NULL) {
            option_load_mode = save_mode;
            return NULL;
        }
        valLen = (pzScan - pzVal);
        pzScan += nameLen + 3;
        while (IS_WHITESPACE_CHAR(*pzScan))  pzScan++;
    }

    switch (valu.valType) {
    case OPARG_TYPE_NONE:
        addStringValue( &(pRes->v.nestVal), pzName, nameLen, NULL, (size_t)0);
        break;

    case OPARG_TYPE_STRING:
        pNewVal = addStringValue(
            &(pRes->v.nestVal), pzName, nameLen, pzVal, valLen);

        if (option_load_mode == OPTION_LOAD_KEEP)
            break;
        mungeString( pNewVal->v.strVal, option_load_mode );
        break;

    case OPARG_TYPE_BOOLEAN:
        addBoolValue( &(pRes->v.nestVal), pzName, nameLen, pzVal, valLen );
        break;

    case OPARG_TYPE_NUMERIC:
        addNumberValue( &(pRes->v.nestVal), pzName, nameLen, pzVal, valLen );
        break;

    case OPARG_TYPE_HIERARCHY:
    {
        char* pz = AGALOC( valLen+1, "hierarchical scan" );
        if (pz == NULL)
            break;
        memcpy( pz, pzVal, valLen );
        pz[valLen] = NUL;
        addNestedValue( &(pRes->v.nestVal), pzName, nameLen, pz, valLen );
        AGFREE(pz);
        break;
    }

    case OPARG_TYPE_ENUMERATION:
    case OPARG_TYPE_MEMBERSHIP:
    default:
        break;
    }

    option_load_mode = save_mode;
    return pzScan;
}
Ejemplo n.º 4
0
void ScanParseSkel::addBufferAsStringValue(void)
{
    addStringValue(_pLexer->YYText());
}
Ejemplo n.º 5
0
/*  scanNameEntry
 *
 *  We have an entry that starts with a name.  Find the end of it, cook it
 *  (if called for) and create the name/value association.
 */
static char const*
scanNameEntry(char const* pzName, tOptionValue* pRes)
{
    tOptionValue* pNV;
    char const * pzScan = pzName+1; /* we know first char is a name char */
    char const * pzVal;
    size_t       nameLen = 1;
    size_t       dataLen = 0;

    /*
     *  Scan over characters that name a value.  These names may not end
     *  with a colon, but they may contain colons.
     */
    while (IS_VALUE_NAME_CHAR(*pzScan))   { pzScan++; nameLen++; }
    if (pzScan[-1] == ':')                { pzScan--; nameLen--; }
    while (IS_HORIZ_WHITE_CHAR(*pzScan))    pzScan++;

re_switch:
    switch (*pzScan) {
    case '=':
    case ':':
        while (IS_HORIZ_WHITE_CHAR( (int)*++pzScan ))  ;
        if ((*pzScan == '=') || (*pzScan == ':'))
            goto default_char;
        goto re_switch;

    case '\n':
    case ',':
        pzScan++;
        /* FALLTHROUGH */

    case NUL:
        addStringValue(&(pRes->v.nestVal), pzName, nameLen, NULL, (size_t)0);
        break;

    case '"':
    case '\'':
        pzVal = pzScan;
        pzScan = scanQuotedString( pzScan );
        dataLen = pzScan - pzVal;
        pNV = addStringValue( &(pRes->v.nestVal), pzName, nameLen, pzVal,
                              dataLen );
        if ((pNV != NULL) && (option_load_mode == OPTION_LOAD_COOKED))
            ao_string_cook( pNV->v.strVal, NULL );
        break;

    default:
    default_char:
        /*
         *  We have found some strange text value.  It ends with a newline
         *  or a comma.
         */
        pzVal = pzScan;
        for (;;) {
            char ch = *(pzScan++);
            switch (ch) {
            case NUL:
                pzScan--;
                dataLen = pzScan - pzVal;
                goto string_done;
                /* FALLTHROUGH */

            case '\n':
                if (   (pzScan > pzVal + 2)
                    && (pzScan[-2] == '\\')
                    && (pzScan[ 0] != NUL))
                    continue;
                /* FALLTHROUGH */

            case ',':
                dataLen = (pzScan - pzVal) - 1;
            string_done:
                pNV = addStringValue( &(pRes->v.nestVal), pzName, nameLen,
                                      pzVal, dataLen );
                if (pNV != NULL)
                    removeLineContinue( pNV->v.strVal );
                goto leave_scan_name;
            }
        }
        break;
    } leave_scan_name:;

    return pzScan;
}