Example #1
0
/* find the maximum frequency of the same phrase */
static int LoadMaxFreq( ChewingData *pgdata, const uint16_t phoneSeq[], int len )
{
	const TreeType *tree_pos;
	Phrase *phrase = ALC( Phrase, 1 );
	int maxFreq = FREQ_INIT_VALUE;
	UserPhraseData *uphrase;

	tree_pos = TreeFindPhrase( pgdata, 0, len - 1, phoneSeq );
	if ( tree_pos ) {
		GetPhraseFirst( pgdata, phrase, tree_pos );
		do {
			if ( phrase->freq > maxFreq )
				maxFreq = phrase->freq;
		} while( GetVocabNext( pgdata, phrase ) );
	}
	free( phrase );

	uphrase = UserGetPhraseFirst( pgdata, phoneSeq );
	while ( uphrase ) {
		if ( uphrase->userfreq > maxFreq )
			maxFreq = uphrase->userfreq;
		uphrase = UserGetPhraseNext( pgdata, phoneSeq );
	}

	return maxFreq;
}
/* find the maximum frequency of the same phrase */
static int LoadMaxFreq(ChewingData *pgdata, const uint16_t phoneSeq[], int len)
{
    const TreeType *tree_pos;
    Phrase *phrase = ALC(Phrase, 1);
    int maxFreq = FREQ_INIT_VALUE;
    int max_userphrase_freq;
    int ret;

    tree_pos = TreeFindPhrase(pgdata, 0, len - 1, phoneSeq);
    if (tree_pos) {
        GetPhraseFirst(pgdata, phrase, tree_pos);
        do {
            if (phrase->freq > maxFreq)
                maxFreq = phrase->freq;
        } while (GetVocabNext(pgdata, phrase));
    }
    free(phrase);

    assert(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_GET_MAX_FREQ]);

    ret = UserBindPhone(pgdata, STMT_USERPHRASE_GET_MAX_FREQ, phoneSeq, len);
    if (ret != SQLITE_OK) {
        LOG_ERROR("UserBindPhone returns %d", ret);
        return maxFreq;
    }

    ret = sqlite3_step(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_GET_MAX_FREQ]);
    if (ret != SQLITE_ROW)
        return maxFreq;

    ret = sqlite3_reset(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_GET_MAX_FREQ]);
    if (ret != SQLITE_OK) {
        LOG_ERROR("sqlite3_reset returns %d", ret);
        return maxFreq;
    }

    max_userphrase_freq = sqlite3_column_int(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_GET_MAX_FREQ],
                                             SQL_STMT_USERPHRASE[STMT_USERPHRASE_GET_MAX_FREQ].column
                                             [COLUMN_USERPHRASE_USER_FREQ]);

    if (max_userphrase_freq > maxFreq)
        maxFreq = max_userphrase_freq;

    return maxFreq;
}
Example #3
0
static void ChoiceInfoAppendChi(ChewingData *pgdata, ChoiceInfo *pci, uint16_t phone)
{
    Phrase tempWord;
    int len;

    if (GetCharFirst(pgdata, &tempWord, phone)) {
        do {
            len = ueBytesFromChar(tempWord.phrase[0]);
            if (ChoiceTheSame(pci, tempWord.phrase, len))
                continue;
            assert(pci->nTotalChoice < MAX_CHOICE);
            memcpy(pci->totalChoiceStr[pci->nTotalChoice], tempWord.phrase, len);
            pci->totalChoiceStr[pci->nTotalChoice]
                [len] = '\0';
            pci->nTotalChoice++;
        } while (GetVocabNext(pgdata, &tempWord));
    }
}
/* load the orginal frequency from the static dict */
static int LoadOriginalFreq(ChewingData *pgdata, const uint16_t phoneSeq[], const char wordSeq[], int len)
{
    const TreeType *tree_pos;
    int retval;
    Phrase *phrase = ALC(Phrase, 1);

    tree_pos = TreeFindPhrase(pgdata, 0, len - 1, phoneSeq);
    if (tree_pos) {
        GetPhraseFirst(pgdata, phrase, tree_pos);
        do {
            /* find the same phrase */
            if (!strcmp(phrase->phrase, wordSeq)) {
                retval = phrase->freq;
                free(phrase);
                return retval;
            }
        } while (GetVocabNext(pgdata, phrase));
    }

    free(phrase);
    return FREQ_INIT_VALUE;
}
Example #5
0
/** @brief Loading all possible phrases of certain length.
 *
 * Loading all possible phrases of certain length into ChoiceInfo structure
 * from static and dynamic dictionaries, including number of total pages and
 * the number of current page.
 */
static void SetChoiceInfo(ChewingData *pgdata)
{
    Phrase tempPhrase;
    int len;
    UserPhraseData *pUserPhraseData;
    uint16_t userPhoneSeq[MAX_PHONE_SEQ_LEN];

    ChoiceInfo *pci = &(pgdata->choiceInfo);
    AvailInfo *pai = &(pgdata->availInfo);
    uint16_t *phoneSeq = pgdata->phoneSeq;
    uint16_t *phoneSeqAlt = pgdata->phoneSeqAlt;
    int cursor = PhoneSeqCursor(pgdata);
    int candPerPage = pgdata->config.candPerPage;

    /* Clears previous candidates. */
    memset(pci->totalChoiceStr, '\0', MAX_CHOICE * MAX_PHRASE_LEN * MAX_UTF8_SIZE + 1);

    pci->nTotalChoice = 0;
    len = pai->avail[pai->currentAvail].len;
    assert(len);

    /* secondly, read tree phrase */
    if (len == 1) {             /* single character */
        ChoiceInfoAppendChi(pgdata, pci, phoneSeq[cursor]);

        if (phoneSeq[cursor] != phoneSeqAlt[cursor]) {
            ChoiceInfoAppendChi(pgdata, pci, phoneSeqAlt[cursor]);
        }

        if (pgdata->bopomofoData.kbtype == KB_HSU || pgdata->bopomofoData.kbtype == KB_DVORAK_HSU) {
            switch (phoneSeq[cursor]) {
            case 0x2800:       /* 'ㄘ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x30); /* 'ㄟ' */
                break;
            case 0x80:         /* 'ㄧ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x20); /* 'ㄝ' */
                break;
            case 0x2A00:       /* 'ㄙ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x1);  /* '˙' */
                break;
            case 0xA00:        /* 'ㄉ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x2);  /* 'ˊ' */
                break;
            case 0x800:        /* 'ㄈ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x3);  /* 'ˇ' */
                break;
            case 0x18:         /* 'ㄜ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x1200);       /* 'ㄍ' */
                break;
            case 0x10:         /* 'ㄛ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x1600);       /* 'ㄏ' */
                break;
            case 0x1E00:       /* 'ㄓ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x1800);       /* 'ㄐ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x4);  /* 'ˋ' */
                break;
            case 0x58:         /* 'ㄤ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x1400);       /* 'ㄎ' */
                break;
            case 0x68:         /* 'ㄦ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x1000);       /* 'ㄌ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x60); /* 'ㄥ' */
                break;
            case 0x2200:       /* 'ㄕ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x1C00);       /* 'ㄒ' */
                break;
            case 0x2000:       /* 'ㄔ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x1A00);       /* 'ㄑ' */
                break;
            case 0x50:         /* 'ㄣ' */
                ChoiceInfoAppendChi(pgdata, pci, 0xE00);        /* 'ㄋ' */
                break;
            case 0x48:         /* 'ㄢ' */
                ChoiceInfoAppendChi(pgdata, pci, 0x600);        /* 'ㄇ' */
                break;
            default:
                break;
            }
        }
    }
    /* phrase */
    else {
        if (pai->avail[pai->currentAvail].id) {
            GetPhraseFirst(pgdata, &tempPhrase, pai->avail[pai->currentAvail].id);
            do {
                if (ChoiceTheSame(pci, tempPhrase.phrase, len * ueBytesFromChar(tempPhrase.phrase[0]))) {
                    continue;
                }
                ueStrNCpy(pci->totalChoiceStr[pci->nTotalChoice], tempPhrase.phrase, len, 1);
                pci->nTotalChoice++;
            } while (GetVocabNext(pgdata, &tempPhrase));
        }

        memcpy(userPhoneSeq, &phoneSeq[cursor], sizeof(uint16_t) * len);
        userPhoneSeq[len] = 0;
        pUserPhraseData = UserGetPhraseFirst(pgdata, userPhoneSeq);
        if (pUserPhraseData) {
            do {
                /* check if the phrase is already in the choice list */
                if (ChoiceTheSame(pci, pUserPhraseData->wordSeq, len * ueBytesFromChar(pUserPhraseData->wordSeq[0])))
                    continue;
                /* otherwise store it */
                ueStrNCpy(pci->totalChoiceStr[pci->nTotalChoice], pUserPhraseData->wordSeq, len, 1);
                pci->nTotalChoice++;
            } while ((pUserPhraseData = UserGetPhraseNext(pgdata, userPhoneSeq)) != NULL);
        }
        UserGetPhraseEnd(pgdata, userPhoneSeq);

    }

    /* magic number */
    pci->nChoicePerPage = candPerPage;
    assert(pci->nTotalChoice > 0);
    pci->nPage = CEIL_DIV(pci->nTotalChoice, pci->nChoicePerPage);
    pci->pageNo = 0;
    pci->isSymbol = WORD_CHOICE;
}