/** * Add the given CharacterMatch to this collector. * The guts of this method will handle removal of duplicates and will throw out low scoring matches. * * @param match the match to add * @return true if the match if the top matches were changed, false if already at maxSize and the given match had lowest score */ public bool AddMatch(CharacterMatch match) { // First check the matchMap to see if there is already a CharacterMatch for the relevant Character. if (matchMap.ContainsKey(match.Character)) { CharacterMatch existingMatch = matchMap[match.Character]; if (match.Score > existingMatch.Score) { orderedMatches.Remove(existingMatch); matchMap[match.Character] = match; orderedMatches.Add(match); orderedMatches.Sort((x, y) => y.Score.CompareTo(x.Score)); } return(false); } matchMap[match.Character] = match; orderedMatches.Add(match); orderedMatches.Sort((x, y) => y.Score.CompareTo(x.Score)); if (orderedMatches.Count <= maxSize) { return(false); } CharacterMatch toRemove = orderedMatches[orderedMatches.Count - 1]; orderedMatches.RemoveAt(orderedMatches.Count - 1); matchMap.Remove(toRemove.Character); return(true); }
/** * Add the given CharacterMatch to this collector. * The guts of this method will handle removal of duplicates and will throw out low scoring matches. * * @param match the match to add * @return true if the match if the top matches were changed, false if already at maxSize and the given match had lowest score */ public bool AddMatch(CharacterMatch match) { // First check the matchMap to see if there is already a CharacterMatch for the relevant Character. if (matchMap.ContainsKey(match.Character)) { CharacterMatch existingMatch = matchMap[match.Character]; if (match.Score > existingMatch.Score) { orderedMatches.Remove(existingMatch); matchMap[match.Character] = match; orderedMatches.Add(match); orderedMatches.Sort((x, y) => y.Score.CompareTo(x.Score)); } return false; } matchMap[match.Character] = match; orderedMatches.Add(match); orderedMatches.Sort((x, y) => y.Score.CompareTo(x.Score)); if (orderedMatches.Count <= maxSize) return false; CharacterMatch toRemove = orderedMatches[orderedMatches.Count - 1]; orderedMatches.RemoveAt(orderedMatches.Count - 1); matchMap.Remove(toRemove.Character); return true; }
/** * Compute and return the closest matches based on the settings passed to the constructor. * @return the closet matches, lower indices are better matches, null if processing canceled prematurely */ public char[] DoMatching() { int strokeCount = inputCharacter.StrokeCount; int subStrokeCount = inputCharacter.SubStrokeCount; // Get the range of strokes to compare against based on the loosness. // Characters with fewer strokes than strokeCount - strokeRange // or more than strokeCount + strokeRange won't even be considered. int strokeRange = getStrokesRange(strokeCount); // Characters with stroke count >= minimumStrokes and <= maximumStrokes considered. int minimumStrokes = Math.Max(strokeCount - strokeRange, 1); int maximumStrokes = Math.Min(strokeCount + strokeRange, CharacterDescriptor.MAX_CHARACTER_STROKE_COUNT); // Get the range of substrokes to compare against based on looseness. // When trying to match sub stroke patterns, won't compare sub strokes // that are farther about in sequence than this range. This is to make // computing matches less expensive for low loosenesses. int subStrokesRange = getSubStrokesRange(subStrokeCount); // The data source might come from a resource file, or in memory, etc. StrokesDataScanner strokesScanner = strokesDataSource.GetStrokesScanner(searchTraditional, searchSimplified, minimumStrokes, maximumStrokes); // While there are more characters from the source, load them into the compare instance, // get the match, and add it to our matches. while (strokesScanner.LoadNextCharacterStrokeData(this.compareTo)) { // continue doing Character matches until the scanner tells us there are no // more characters to match. CharacterMatch match = this.compareToNext(strokeCount, subStrokeCount, subStrokesRange); // always add, it won't have any effect if it wasn't a good match this.matches.AddMatch(match); if (!running) { break; } } // Results available for us in the CharacterMatchCollector. char[] matches = this.matches.GetMatches(); #if DEBUG if (matches.Contains('中')) { throw new ZD.Common.DiagnosticException(true); } #endif // Only return matches if processing completed. if (running) { return(matches); } return(null); }