2012-05-17 10:53:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.latin;
|
|
|
|
|
2012-05-18 12:28:09 +00:00
|
|
|
import android.text.format.DateUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2012-08-29 08:26:00 +00:00
|
|
|
public final class UserHistoryForgettingCurveUtils {
|
2012-05-18 12:28:09 +00:00
|
|
|
private static final String TAG = UserHistoryForgettingCurveUtils.class.getSimpleName();
|
|
|
|
private static final boolean DEBUG = false;
|
2012-05-17 10:53:32 +00:00
|
|
|
private static final int FC_FREQ_MAX = 127;
|
|
|
|
/* package */ static final int COUNT_MAX = 3;
|
|
|
|
private static final int FC_LEVEL_MAX = 3;
|
|
|
|
/* package */ static final int ELAPSED_TIME_MAX = 15;
|
|
|
|
private static final int ELAPSED_TIME_INTERVAL_HOURS = 6;
|
2012-05-18 12:28:09 +00:00
|
|
|
private static final long ELAPSED_TIME_INTERVAL_MILLIS = ELAPSED_TIME_INTERVAL_HOURS
|
|
|
|
* DateUtils.HOUR_IN_MILLIS;
|
2012-05-17 10:53:32 +00:00
|
|
|
private static final int HALF_LIFE_HOURS = 48;
|
2012-06-07 17:23:39 +00:00
|
|
|
private static final int MAX_PUSH_ELAPSED = (FC_LEVEL_MAX + 1) * (ELAPSED_TIME_MAX + 1);
|
2012-05-17 10:53:32 +00:00
|
|
|
|
2012-05-18 12:28:09 +00:00
|
|
|
private UserHistoryForgettingCurveUtils() {
|
|
|
|
// This utility class is not publicly instantiable.
|
|
|
|
}
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
public static final class ForgettingCurveParams {
|
2012-05-18 12:28:09 +00:00
|
|
|
private byte mFc;
|
|
|
|
long mLastTouchedTime = 0;
|
2012-05-29 10:07:22 +00:00
|
|
|
private final boolean mIsValid;
|
2012-05-18 12:28:09 +00:00
|
|
|
|
|
|
|
private void updateLastTouchedTime() {
|
|
|
|
mLastTouchedTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
2012-05-29 10:07:22 +00:00
|
|
|
public ForgettingCurveParams(boolean isValid) {
|
|
|
|
this(System.currentTimeMillis(), isValid);
|
2012-05-18 12:28:09 +00:00
|
|
|
}
|
|
|
|
|
2012-05-29 10:07:22 +00:00
|
|
|
private ForgettingCurveParams(long now, boolean isValid) {
|
2012-06-08 07:00:02 +00:00
|
|
|
this(pushCount((byte)0, isValid), now, now, isValid);
|
2012-05-18 12:28:09 +00:00
|
|
|
}
|
|
|
|
|
2012-05-29 10:07:22 +00:00
|
|
|
/** This constructor is called when the user history bigram dictionary is being restored. */
|
2012-05-18 12:28:09 +00:00
|
|
|
public ForgettingCurveParams(int fc, long now, long last) {
|
2012-05-29 10:07:22 +00:00
|
|
|
// All words with level >= 1 had been saved.
|
|
|
|
// Invalid words with level == 0 had been saved.
|
|
|
|
// Valid words words with level == 0 had *not* been saved.
|
|
|
|
this(fc, now, last, fcToLevel((byte)fc) > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ForgettingCurveParams(int fc, long now, long last, boolean isValid) {
|
|
|
|
mIsValid = isValid;
|
2012-05-18 12:28:09 +00:00
|
|
|
mFc = (byte)fc;
|
|
|
|
mLastTouchedTime = last;
|
|
|
|
updateElapsedTime(now);
|
|
|
|
}
|
|
|
|
|
2012-05-29 10:07:22 +00:00
|
|
|
public boolean isValid() {
|
|
|
|
return mIsValid;
|
|
|
|
}
|
|
|
|
|
2012-05-18 12:28:09 +00:00
|
|
|
public byte getFc() {
|
|
|
|
updateElapsedTime(System.currentTimeMillis());
|
|
|
|
return mFc;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getFrequency() {
|
|
|
|
updateElapsedTime(System.currentTimeMillis());
|
|
|
|
return UserHistoryForgettingCurveUtils.fcToFreq(mFc);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int notifyTypedAgainAndGetFrequency() {
|
|
|
|
updateLastTouchedTime();
|
|
|
|
// TODO: Check whether this word is valid or not
|
|
|
|
mFc = pushCount(mFc, false);
|
|
|
|
return UserHistoryForgettingCurveUtils.fcToFreq(mFc);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateElapsedTime(long now) {
|
|
|
|
final int elapsedTimeCount =
|
|
|
|
(int)((now - mLastTouchedTime) / ELAPSED_TIME_INTERVAL_MILLIS);
|
|
|
|
if (elapsedTimeCount <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-07 17:23:39 +00:00
|
|
|
if (elapsedTimeCount >= MAX_PUSH_ELAPSED) {
|
|
|
|
mLastTouchedTime = now;
|
|
|
|
mFc = 0;
|
|
|
|
return;
|
|
|
|
}
|
2012-05-18 12:28:09 +00:00
|
|
|
for (int i = 0; i < elapsedTimeCount; ++i) {
|
|
|
|
mLastTouchedTime += ELAPSED_TIME_INTERVAL_MILLIS;
|
|
|
|
mFc = pushElapsedTime(mFc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-17 10:53:32 +00:00
|
|
|
/* package */ static int fcToElapsedTime(byte fc) {
|
|
|
|
return fc & 0x0F;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* package */ static int fcToCount(byte fc) {
|
|
|
|
return (fc >> 4) & 0x03;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* package */ static int fcToLevel(byte fc) {
|
|
|
|
return (fc >> 6) & 0x03;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int calcFreq(int elapsedTime, int count, int level) {
|
|
|
|
if (level <= 0) {
|
2012-05-18 12:28:09 +00:00
|
|
|
// Reserved words, just return -1
|
|
|
|
return -1;
|
2012-05-17 10:53:32 +00:00
|
|
|
}
|
|
|
|
if (count == COUNT_MAX) {
|
|
|
|
// Temporary promote because it's frequently typed recently
|
|
|
|
++level;
|
|
|
|
}
|
|
|
|
final int et = Math.min(FC_FREQ_MAX, Math.max(0, elapsedTime));
|
|
|
|
final int l = Math.min(FC_LEVEL_MAX, Math.max(0, level));
|
|
|
|
return MathUtils.SCORE_TABLE[l - 1][et];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pakcage */ static byte calcFc(int elapsedTime, int count, int level) {
|
|
|
|
final int et = Math.min(FC_FREQ_MAX, Math.max(0, elapsedTime));
|
|
|
|
final int c = Math.min(COUNT_MAX, Math.max(0, count));
|
|
|
|
final int l = Math.min(FC_LEVEL_MAX, Math.max(0, level));
|
|
|
|
return (byte)(et | (c << 4) | (l << 6));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int fcToFreq(byte fc) {
|
|
|
|
final int elapsedTime = fcToElapsedTime(fc);
|
|
|
|
final int count = fcToCount(fc);
|
|
|
|
final int level = fcToLevel(fc);
|
|
|
|
return calcFreq(elapsedTime, count, level);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte pushElapsedTime(byte fc) {
|
|
|
|
int elapsedTime = fcToElapsedTime(fc);
|
|
|
|
int count = fcToCount(fc);
|
|
|
|
int level = fcToLevel(fc);
|
|
|
|
if (elapsedTime >= ELAPSED_TIME_MAX) {
|
|
|
|
// Downgrade level
|
|
|
|
elapsedTime = 0;
|
|
|
|
count = COUNT_MAX;
|
|
|
|
--level;
|
|
|
|
} else {
|
|
|
|
++elapsedTime;
|
|
|
|
}
|
|
|
|
return calcFc(elapsedTime, count, level);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static byte pushCount(byte fc, boolean isValid) {
|
|
|
|
final int elapsedTime = fcToElapsedTime(fc);
|
|
|
|
int count = fcToCount(fc);
|
|
|
|
int level = fcToLevel(fc);
|
|
|
|
if ((elapsedTime == 0 && count >= COUNT_MAX) || (isValid && level == 0)) {
|
|
|
|
// Upgrade level
|
|
|
|
++level;
|
|
|
|
count = 0;
|
2012-05-18 12:28:09 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "Upgrade level.");
|
|
|
|
}
|
2012-05-17 10:53:32 +00:00
|
|
|
} else {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return calcFc(0, count, level);
|
|
|
|
}
|
|
|
|
|
2012-05-18 12:28:09 +00:00
|
|
|
// TODO: isValid should be false for a word whose frequency is 0,
|
|
|
|
// or that is not in the dictionary.
|
2012-05-28 10:08:34 +00:00
|
|
|
/**
|
|
|
|
* Check wheather we should save the bigram to the SQL DB or not
|
|
|
|
*/
|
|
|
|
public static boolean needsToSave(byte fc, boolean isValid, boolean addLevel0Bigram) {
|
2012-05-18 12:28:09 +00:00
|
|
|
int level = fcToLevel(fc);
|
2012-05-28 10:08:34 +00:00
|
|
|
if (level == 0) {
|
|
|
|
if (isValid || !addLevel0Bigram) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-18 12:28:09 +00:00
|
|
|
}
|
|
|
|
final int elapsedTime = fcToElapsedTime(fc);
|
|
|
|
return (elapsedTime < ELAPSED_TIME_MAX - 1 || level > 0);
|
|
|
|
}
|
|
|
|
|
2012-09-27 09:16:16 +00:00
|
|
|
private static final class MathUtils {
|
2012-05-17 10:53:32 +00:00
|
|
|
public static final int[][] SCORE_TABLE = new int[FC_LEVEL_MAX][ELAPSED_TIME_MAX + 1];
|
|
|
|
static {
|
|
|
|
for (int i = 0; i < FC_LEVEL_MAX; ++i) {
|
2012-06-08 08:05:28 +00:00
|
|
|
final float initialFreq;
|
2012-05-17 10:53:32 +00:00
|
|
|
if (i >= 2) {
|
2012-06-08 07:00:02 +00:00
|
|
|
initialFreq = FC_FREQ_MAX;
|
2012-05-17 10:53:32 +00:00
|
|
|
} else if (i == 1) {
|
2012-06-08 08:05:28 +00:00
|
|
|
initialFreq = FC_FREQ_MAX / 2;
|
2012-05-17 10:53:32 +00:00
|
|
|
} else if (i == 0) {
|
2012-06-08 08:05:28 +00:00
|
|
|
initialFreq = FC_FREQ_MAX / 4;
|
2012-05-17 10:53:32 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (int j = 0; j < ELAPSED_TIME_MAX; ++j) {
|
2012-06-08 08:05:28 +00:00
|
|
|
final float elapsedHours = j * ELAPSED_TIME_INTERVAL_HOURS;
|
2012-06-08 10:05:47 +00:00
|
|
|
final float freq = initialFreq
|
2012-08-12 02:10:48 +00:00
|
|
|
* (float)Math.pow(initialFreq, elapsedHours / HALF_LIFE_HOURS);
|
2012-05-17 10:53:32 +00:00
|
|
|
final int intFreq = Math.min(FC_FREQ_MAX, Math.max(0, (int)freq));
|
|
|
|
SCORE_TABLE[i][j] = intFreq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|