1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
/*
* Copyright (C) 2008 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;
import com.android.inputmethod.keyboard.ProximityInfo;
import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
import java.util.ArrayList;
/**
* Abstract base class for a dictionary that can do a fuzzy search for words based on a set of key
* strokes.
*/
public abstract class Dictionary {
/**
* The weight to give to a word if it's length is the same as the number of typed characters.
*/
protected static final int FULL_WORD_SCORE_MULTIPLIER = 2;
public static final int UNIGRAM = 0;
public static final int BIGRAM = 1;
public static final int NOT_A_PROBABILITY = -1;
/**
* Interface to be implemented by classes requesting words to be fetched from the dictionary.
* @see #getWords(WordComposer, CharSequence, WordCallback, ProximityInfo)
*/
public interface WordCallback {
/**
* Adds a word to a list of suggestions. The word is expected to be ordered based on
* the provided score.
* @param word the character array containing the word
* @param spaceIndices the indices of inserted spaces
* @param wordOffset starting offset of the word in the character array
* @param wordLength length of valid characters in the character array
* @param score the score of occurrence. This is normalized between 1 and 255, but
* can exceed those limits
* @param dicTypeId of the dictionary where word was from
* @param dataType tells type of this data, either UNIGRAM or BIGRAM
* @return true if the word was added, false if no more words are required
*/
boolean addWord(char[] word, int[] spaceIndices, int wordOffset, int wordLength, int score,
int dicTypeId, int dataType);
}
/**
* Searches for words in the dictionary that match the characters in the composer. Matched
* words are added through the callback object.
* @param composer the key sequence to match
* @param prevWordForBigrams the previous word, or null if none
* @param callback the callback object to send matched words to as possible candidates
* @param proximityInfo the object for key proximity. May be ignored by some implementations.
* @return the list of suggestions
* @see WordCallback#addWord(char[], int, int, int, int, int)
*/
abstract public ArrayList<SuggestedWordInfo> getWords(final WordComposer composer,
final CharSequence prevWordForBigrams, final WordCallback callback,
final ProximityInfo proximityInfo);
/**
* Searches for pairs in the bigram dictionary that matches the previous word and all the
* possible words following are added through the callback object.
* @param composer the key sequence to match
* @param previousWord the word before
* @param callback the callback object to send possible word following previous word
* @return the list of suggestions
*/
public abstract ArrayList<SuggestedWordInfo> getBigrams(final WordComposer composer,
final CharSequence previousWord, final WordCallback callback);
/**
* Checks if the given word occurs in the dictionary
* @param word the word to search for. The search should be case-insensitive.
* @return true if the word exists, false otherwise
*/
abstract public boolean isValidWord(CharSequence word);
public int getFrequency(CharSequence word) {
return NOT_A_PROBABILITY;
}
/**
* Compares the contents of the character array with the typed word and returns true if they
* are the same.
* @param word the array of characters that make up the word
* @param length the number of valid characters in the character array
* @param typedWord the word to compare with
* @return true if they are the same, false otherwise.
*/
protected boolean same(final char[] word, final int length, final CharSequence typedWord) {
if (typedWord.length() != length) {
return false;
}
for (int i = 0; i < length; i++) {
if (word[i] != typedWord.charAt(i)) {
return false;
}
}
return true;
}
/**
* Override to clean up any resources.
*/
public void close() {
// empty base implementation
}
/**
* Subclasses may override to indicate that this Dictionary is not yet properly initialized.
*/
public boolean isInitialized() {
return true;
}
}
|