public Entry(string word, long frequency, Lexicon lexicon) { this.word = word; this.frequency = frequency; int key = -1; for (int i = 0; i < word.Length; ++i) { key = word[i] - 'a'; pts.Add(lexicon.keyPos[key]); } locationSample[0] = PathCalc.TemporalSampling(pts.ToArray()); bool ins = false; if (Vector2.Distance(pts[0], StartPoint) > eps) { ins = true; } if (ins) { pts.Insert(0, StartPoint); } locationSample[1] = PathCalc.TemporalSampling(pts.ToArray()); if (ins) { pts.RemoveAt(0); } for (int i = 0; i < (int)Parameter.Mode.End; ++i) { shapeSample[i] = PathCalc.Normalize(locationSample[i]); } }
public Candidate[] Recognize(Vector2[] rawStroke) { Vector2[] stroke = PathCalc.TemporalSampling(rawStroke); for (int i = 0; i < CandidatesNum; ++i) { candsTmp[i] = new Candidate(); } int h = history.Count; string pre = (h == 0) ? "<s>" : history[h - 1].word.ToLower(); Debug.Log(pre); foreach (Entry entry in dict) { if (rawStroke.Length == 1 && entry.word.Length != 1) { continue; } Candidate newCandidate = new Candidate { word = entry.word }; if (Parameter.mode == Parameter.Mode.AnyStart) { if (Vector2.Distance(entry.locationSample[0][0], stroke[0]) > AnyStartThr * Parameter.keyWidth) { continue; } entry.pts.Insert(0, stroke[0]); entry.locationSample[(int)Parameter.mode] = PathCalc.TemporalSampling(entry.pts.ToArray()); entry.shapeSample[(int)Parameter.mode] = PathCalc.Normalize(entry.locationSample[(int)Parameter.mode]); entry.pts.RemoveAt(0); } if (Vector2.Distance(stroke[SampleSize - 1], entry.locationSample[(int)Parameter.mode][SampleSize - 1]) > Parameter.endOffset * Parameter.keyWidth && (h >= words.Length || entry.word != words[h])) { continue; } float biF = 0; if (bigramMap.ContainsKey(pre + ' ' + entry.word)) { biF = bigramMap[pre + ' ' + entry.word]; } else { biF = katzAlpha[pre] * entry.frequency; if (biF == 0) { continue; } } newCandidate.language = Mathf.Log(biF); newCandidate.location = PathCalc.Match(stroke, entry.locationSample[(int)Parameter.mode], Parameter.locationFormula, (newCandidate.language - candsTmp[CandidatesNum - 1].confidence) / 100 * Parameter.keyboardWidth * SampleSize); if (newCandidate.location == Parameter.inf) { continue; } newCandidate.confidence = newCandidate.language - 100 * newCandidate.location; //if (newCandidate.confidence < candsTmp[CandidatesNum - 1].confidence) //continue; for (int i = 0; i < CandidatesNum; ++i) { if (newCandidate.confidence > candsTmp[i].confidence) { for (int j = CandidatesNum - 1; j > i; j--) { candsTmp[j] = candsTmp[j - 1]; } candsTmp[i] = newCandidate; break; } } } if (h < words.Length) { for (int i = 0; i < CandidatesNum; ++i) { if (candsTmp[i].word == words[h].ToLower()) { candsTmp[i].word = words[h]; } } } return(candsTmp); }