Skip to content

Commit 674b256

Browse files
committed
WIP: Show word suggestions/correction according to a dictionary
1 parent 49765ad commit 674b256

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

srcs/juloo.keyboard2/CurrentlyTypedWord.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ public void selection_updated(int oldSelStart, int newSelStart, int newSelEnd)
6363
{
6464
// Avoid the expensive [refresh_current_word] call when [typed] was called
6565
// before.
66-
boolean new_has_sel = newSelStart != newSelEnd;
67-
if (!_enabled || (newSelStart == _cursor && new_has_sel == _has_selection))
68-
return;
69-
_has_selection = new_has_sel;
70-
refresh_current_word();
71-
_cursor = newSelStart;
66+
return;
67+
// boolean new_has_sel = newSelStart != newSelEnd;
68+
// if (!_enabled || (newSelStart == _cursor && new_has_sel == _has_selection))
69+
// return;
70+
// _has_selection = new_has_sel;
71+
// refresh_current_word();
72+
// _cursor = newSelStart;
7273
}
7374

7475
public void event_sent(int code, int meta)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package juloo.keyboard2;
2+
3+
import android.content.res.Resources;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import juloo.cdict.Cdict;
7+
8+
public final class Dictionary
9+
{
10+
public static Cdict main_fr;
11+
12+
private static boolean inited = false;
13+
14+
public static void init(Resources res)
15+
{
16+
if (inited)
17+
return;
18+
inited = true;
19+
try
20+
{
21+
InputStream input = res.openRawResource(R.raw.main_fr);
22+
main_fr = Cdict.of_bytes(input.readAllBytes());
23+
}
24+
catch (IOException _exn) {}
25+
}
26+
}

srcs/juloo.keyboard2/Keyboard2.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ KeyboardData loadPinentry(int layout_id)
113113
public void onCreate()
114114
{
115115
super.onCreate();
116+
Dictionary.init(getResources());
116117
SharedPreferences prefs = DirectBootAwarePreferences.get_shared_preferences(this);
117118
_handler = new Handler(getMainLooper());
118119
_keyeventhandler = new KeyEventHandler(this.new Receiver());

srcs/juloo.keyboard2/Suggestions.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
11
package juloo.keyboard2;
22

3-
import java.util.List;
43
import java.util.Arrays;
4+
import java.util.List;
5+
import juloo.cdict.Cdict;
56

67
/** Keep track of the word being typed and provide suggestions for
78
[CandidatesView]. */
89
public final class Suggestions
910
{
1011
Callback _callback;
12+
Cdict _dict;
1113

1214
public Suggestions(Callback c)
1315
{
1416
_callback = c;
17+
_dict = Dictionary.main_fr;
1518
}
1619

1720
public void currently_typed_word(String word)
1821
{
19-
if (word.equals(""))
22+
if (word.length() < 2)
2023
{
2124
_callback.set_suggestions(NO_SUGGESTIONS);
2225
}
2326
else
2427
{
25-
// TODO
26-
_callback.set_suggestions(Arrays.asList(word));
28+
Cdict.Result r = _dict.find(word);
29+
String[] suggestions = new String[3];
30+
int i = 0;
31+
if (r.found)
32+
suggestions[i++] = word;
33+
int[] suffixes = _dict.suffixes(r, 3);
34+
int[] dist = _dict.distance(word, 1, 3);
35+
for (int j = 0; j < 3 && i < 3; j++)
36+
{
37+
if (suffixes.length > j)
38+
suggestions[i++] = _dict.word(suffixes[j]);
39+
if (dist.length > j && i < 3)
40+
suggestions[i++] = _dict.word(dist[j]);
41+
}
42+
_callback.set_suggestions(Arrays.asList(suggestions));
2743
}
2844
}
2945

0 commit comments

Comments
 (0)