Exemplo n.º 1
0
int UserUpdatePhrase( ChewingData *pgdata, const uint16_t phoneSeq[], const char wordSeq[] )
{
	HASH_ITEM *pItem;
	UserPhraseData data;
	int len;

	len = ueStrLen( wordSeq );
	pItem = HashFindEntry( pgdata, phoneSeq, wordSeq );
	if ( ! pItem ) {
		if ( ! AlcUserPhraseSeq( &data, len, strlen( wordSeq ) ) ) {
			return USER_UPDATE_FAIL;
		}

		memcpy( data.phoneSeq, phoneSeq, len * sizeof( phoneSeq[ 0 ] ) );
		data.phoneSeq[ len ] = 0;
		strcpy( data.wordSeq, wordSeq );

		/* load initial freq */
		data.origfreq = LoadOriginalFreq( pgdata, phoneSeq, wordSeq, len );
		data.maxfreq = LoadMaxFreq( pgdata, phoneSeq, len );

		data.userfreq = data.origfreq;
		data.recentTime = pgdata->static_data.chewing_lifetime;
		pItem = HashInsert( pgdata, &data );
		LogUserPhrase( pgdata, phoneSeq, wordSeq, pItem->data.origfreq, pItem->data.maxfreq, pItem->data.userfreq, pItem->data.recentTime );
		HashModify( pgdata, pItem );
		return USER_UPDATE_INSERT;
	}
	else {
		pItem->data.maxfreq = LoadMaxFreq( pgdata, phoneSeq, len );
		pItem->data.userfreq = UpdateFreq(
			pItem->data.userfreq,
			pItem->data.maxfreq,
			pItem->data.origfreq,
			pgdata->static_data.chewing_lifetime - pItem->data.recentTime );
		pItem->data.recentTime = pgdata->static_data.chewing_lifetime;
		LogUserPhrase( pgdata, phoneSeq, wordSeq, pItem->data.origfreq, pItem->data.maxfreq, pItem->data.userfreq, pItem->data.recentTime );
		HashModify( pgdata, pItem );
		return USER_UPDATE_MODIFY;
	}
}
Exemplo n.º 2
0
int UserUpdatePhrase(ChewingData *pgdata, const uint16_t phoneSeq[], const char wordSeq[])
{
    int ret;
    int action;
    int phone_len;
    int word_len;

    int orig_freq;
    int max_freq;
    int user_freq;
    int recent_time;
    int orig_time;

    assert(pgdata);
    assert(phoneSeq);
    assert(wordSeq);

    phone_len = GetPhoneLen(phoneSeq);
    word_len = ueStrLen(wordSeq);

    if (phone_len != word_len) {
        LOG_WARN("Do not update userphrase because phoneSeq length %d != wordSeq length %d", phone_len, word_len);
        return USER_UPDATE_FAIL;
    }

    if (word_len > MAX_PHRASE_LEN) {
        LOG_WARN("wordSeq length %d > MAX_PHRASE_LEN (%d)", word_len, MAX_PHRASE_LEN);
        return USER_UPDATE_FAIL;
    }

    ret = UserBindPhone(pgdata, STMT_USERPHRASE_SELECT_BY_PHONE_PHRASE, phoneSeq, phone_len);
    if (ret != SQLITE_OK) {
        LOG_ERROR("UserBindPhone returns %d", ret);
        action = USER_UPDATE_FAIL;
        goto end;
    }

    ret = sqlite3_bind_text(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_SELECT_BY_PHONE_PHRASE],
                            BIND_USERPHRASE_PHRASE, wordSeq, -1, SQLITE_STATIC);
    if (ret != SQLITE_OK) {
        LOG_ERROR("sqlite3_bind_text returns %d", ret);
        action = USER_UPDATE_FAIL;
        goto end;
    }

    recent_time = GetCurrentLifeTime(pgdata);

    ret = sqlite3_step(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_SELECT_BY_PHONE_PHRASE]);
    if (ret == SQLITE_ROW) {
        action = USER_UPDATE_MODIFY;

        orig_freq = sqlite3_column_int(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_SELECT_BY_PHONE_PHRASE],
                                       SQL_STMT_USERPHRASE[STMT_USERPHRASE_SELECT_BY_PHONE_PHRASE].column
                                       [COLUMN_USERPHRASE_ORIG_FREQ]);

        max_freq = LoadMaxFreq(pgdata, phoneSeq, phone_len);

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

        orig_time = sqlite3_column_int(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_SELECT_BY_PHONE_PHRASE],
                                       SQL_STMT_USERPHRASE[STMT_USERPHRASE_SELECT_BY_PHONE_PHRASE].column
                                       [COLUMN_USERPHRASE_TIME]);

        user_freq = UpdateFreq(user_freq, max_freq, orig_freq, recent_time - orig_time);
    } else {
        action = USER_UPDATE_INSERT;

        orig_freq = LoadOriginalFreq(pgdata, phoneSeq, wordSeq, word_len);
        max_freq = LoadMaxFreq(pgdata, phoneSeq, phone_len);
        user_freq = orig_freq;
    }

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

    ret = sqlite3_bind_int(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_UPSERT],
                           BIND_USERPHRASE_TIME, recent_time);
    if (ret != SQLITE_OK) {
        LOG_ERROR("sqlite3_bind_int returns %d", ret);
        action = USER_UPDATE_FAIL;
        goto end;
    }

    ret = sqlite3_bind_int(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_UPSERT],
                           BIND_USERPHRASE_USER_FREQ, user_freq);
    if (ret != SQLITE_OK) {
        LOG_ERROR("sqlite3_bind_int returns %d", ret);
        action = USER_UPDATE_FAIL;
        goto end;
    }

    ret = sqlite3_bind_int(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_UPSERT],
                           BIND_USERPHRASE_MAX_FREQ, max_freq);
    if (ret != SQLITE_OK) {
        LOG_ERROR("sqlite3_bind_int returns %d", ret);
        action = USER_UPDATE_FAIL;
        goto end;
    }

    ret = sqlite3_bind_int(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_UPSERT],
                           BIND_USERPHRASE_ORIG_FREQ, orig_freq);
    if (ret != SQLITE_OK) {
        LOG_ERROR("sqlite3_bind_int returns %d", ret);
        action = USER_UPDATE_FAIL;
        goto end;
    }

    ret = UserBindPhone(pgdata, STMT_USERPHRASE_UPSERT, phoneSeq, phone_len);
    if (ret != SQLITE_OK) {
        LOG_ERROR("UserBindPhone returns %d", ret);
        action = USER_UPDATE_FAIL;
        goto end;
    }

    ret = sqlite3_bind_text(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_UPSERT],
                            BIND_USERPHRASE_PHRASE, wordSeq, -1, SQLITE_STATIC);
    if (ret != SQLITE_OK) {
        LOG_ERROR("sqlite3_bind_text returns %d", ret);
        action = USER_UPDATE_FAIL;
        goto end;
    }

    ret = sqlite3_step(pgdata->static_data.stmt_userphrase[STMT_USERPHRASE_UPSERT]);
    if (ret != SQLITE_DONE) {
        LOG_ERROR("sqlite3_step returns %d", ret);
        action = USER_UPDATE_FAIL;
        goto end;
    }

    LogUserPhrase(pgdata, phoneSeq, wordSeq, orig_freq, max_freq, user_freq, recent_time);

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

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

    return action;
}