abouttreesummaryrefslogcommitdiff
path: root/srcs/juloo.keyboard2/suggestions/Suggestions.java
blob: 89ed86ea04cc399a23537ddcbe30adbc762fd752 (plain)
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
package juloo.keyboard2.suggestions;

import java.util.Arrays;
import java.util.List;
import juloo.cdict.Cdict;
import juloo.keyboard2.dict.Dictionaries;
import juloo.keyboard2.Config;

/** Keep track of the word being typed and provide suggestions for
    [CandidatesView]. */
public final class Suggestions
{
  Callback _callback;
  Config _config;

  /** The suggestion displayed at the center of the candidates view and entered
      by the space bar. */
  public String best_suggestion = null;

  public Suggestions(Callback c, Config conf)
  {
    _callback = c;
    _config = conf;
  }

  public void currently_typed_word(String word)
  {
    Cdict dict = _config.current_dictionary;
    if (word.length() < 2 || dict == null)
    {
      set_suggestions(NO_SUGGESTIONS);
    }
    else
    {
      Cdict.Result r = dict.find(word);
      String[] suggestions = new String[3];
      int i = 0;
      if (r.found)
        suggestions[i++] = word;
      int[] suffixes = dict.suffixes(r, 3);
      int[] dist = dict.distance(word, 1, 3);
      for (int j = 0; j < 3 && i < 3; j++)
      {
        if (suffixes.length > j)
          suggestions[i++] = dict.word(suffixes[j]);
        if (dist.length > j && i < 3)
          suggestions[i++] = dict.word(dist[j]);
      }
      set_suggestions(Arrays.asList(suggestions));
    }
  }

  void set_suggestions(List<String> ws)
  {
    _callback.set_suggestions(ws);
    best_suggestion = (ws.size() > 0) ? ws.get(0) : null;
  }

  static final List<String> NO_SUGGESTIONS = Arrays.asList();

  public static interface Callback
  {
    public void set_suggestions(List<String> suggestions);
  }
}