fixes in the word completion

This commit is contained in:
kr.angelov
2014-03-12 10:54:29 +00:00
parent ccc43b28f0
commit 23f603dabc
3 changed files with 44 additions and 50 deletions

View File

@@ -10,6 +10,7 @@ import android.graphics.drawable.Drawable;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.CompletionInfo;
import java.util.ArrayList;
import java.util.List;
@@ -19,7 +20,7 @@ public class CompletionsView extends View {
private static final int OUT_OF_BOUNDS = -1;
private TranslatorInputMethodService mService;
private List<String> mSuggestions;
private CompletionInfo[] mSuggestions;
private int mSelectedIndex;
private int mTouchX = OUT_OF_BOUNDS;
private Drawable mSelectionHighlight;
@@ -153,7 +154,7 @@ public class CompletionsView extends View {
}
}
int x = 0;
final int count = mSuggestions.size();
final int count = mSuggestions.length;
final int height = getHeight();
final Rect bgPadding = mBgPadding;
final Paint paint = mPaint;
@@ -164,7 +165,7 @@ public class CompletionsView extends View {
final int y = (int) (((height - mPaint.getTextSize()) / 2) - mPaint.ascent());
for (int i = 0; i < count; i++) {
String suggestion = mSuggestions.get(i);
String suggestion = mSuggestions[i].getText().toString();
float textWidth = paint.measureText(suggestion);
final int wordWidth = (int) textWidth + X_GAP * 2;
@@ -222,11 +223,11 @@ public class CompletionsView extends View {
}
@SuppressLint("WrongCall")
public void setSuggestions(List<String> suggestions, boolean completions,
public void setSuggestions(CompletionInfo[] suggestions, boolean completions,
boolean typedWordValid) {
clear();
if (suggestions != null) {
mSuggestions = new ArrayList<String>(suggestions);
mSuggestions = suggestions;
}
mTypedWordValid = typedWordValid;
scrollTo(0, 0);
@@ -238,7 +239,7 @@ public class CompletionsView extends View {
}
public void clear() {
mSuggestions = EMPTY_LIST;
mSuggestions = new CompletionInfo[0];
mTouchX = OUT_OF_BOUNDS;
mSelectedIndex = -1;
invalidate();
@@ -284,22 +285,6 @@ public class CompletionsView extends View {
}
return true;
}
/**
* For flick through from keyboard, call this method with the x coordinate of the flick
* gesture.
* @param x
*/
@SuppressLint("WrongCall")
public void takeSuggestionAt(float x) {
mTouchX = (int) x;
// To detect candidate
onDraw(null);
if (mSelectedIndex >= 0) {
mService.pickSuggestionManually(mSelectedIndex);
}
invalidate();
}
private void removeHighlight() {
mTouchX = OUT_OF_BOUNDS;

View File

@@ -5,6 +5,7 @@ import android.content.SharedPreferences;
import android.content.res.XmlResourceParser;
import android.text.TextUtils;
import android.util.Log;
import android.view.inputmethod.CompletionInfo;
import org.grammaticalframework.pgf.Concr;
import org.grammaticalframework.pgf.Expr;
@@ -20,6 +21,7 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -332,7 +334,7 @@ public class Translator {
}
}
public List<String> lookupWordPrefix(String prefix) {
public CompletionInfo[] lookupWordPrefix(String prefix) {
PriorityQueue<WordProb> queue = new PriorityQueue<WordProb>();
for (FullFormEntry entry : getSourceConcr().lookupWordPrefix(prefix)) {
WordProb wp = new WordProb();
@@ -348,13 +350,23 @@ public class Translator {
break;
}
List<String> list = new ArrayList<String>();
while (list.size() < 5 && queue.size() > 0) {
list.add(queue.poll().word);
CompletionInfo[] completions = new CompletionInfo[Math.min(queue.size(), 5)+1];
completions[0] = new CompletionInfo(0, 0, prefix);
for (int i = 1; i < completions.length; i++) {
completions[i] = new CompletionInfo(i,i,queue.poll().word);
}
Collections.sort(list);
return list;
if (completions.length > 1) {
Arrays.sort(completions, 1, completions.length-1, new Comparator<CompletionInfo>() {
@Override
public int compare(CompletionInfo arg0, CompletionInfo arg1) {
// TODO Auto-generated method stub
return ((String) arg0.getText()).compareTo((String) arg1.getText());
}
});
}
return completions;
}
private PGF getGrammar() {

View File

@@ -241,12 +241,7 @@ public class TranslatorInputMethodService extends InputMethodService
return;
}
List<String> stringList = new ArrayList<String>();
for (int i = 0; i < completions.length; i++) {
CompletionInfo ci = completions[i];
if (ci != null) stringList.add(ci.getText().toString());
}
setSuggestions(stringList, true, true);
setSuggestions(completions, true, true);
}
}
@@ -438,11 +433,10 @@ public class TranslatorInputMethodService extends InputMethodService
*/
private void updateCandidates() {
if (!mCompletionOn) {
if (mComposing.length() > 0) {
List<String> list =
if (mComposing.length() > 1) {
mCompletions =
mTranslator.lookupWordPrefix(mComposing.toString());
list.add(0, mComposing.toString());
setSuggestions(list, true, true);
setSuggestions(mCompletions, true, true);
Log.d("KEYBOARD", mComposing.toString());
} else {
setSuggestions(null, false, false);
@@ -450,15 +444,15 @@ public class TranslatorInputMethodService extends InputMethodService
}
}
public void setSuggestions(List<String> suggestions, boolean completions,
public void setSuggestions(CompletionInfo[] completions, boolean isCompletions,
boolean typedWordValid) {
if (suggestions != null && suggestions.size() > 0) {
if (completions != null && completions.length > 0) {
setCandidatesViewShown(true);
} else if (isExtractViewShown()) {
setCandidatesViewShown(true);
}
if (mCandidateView != null) {
mCandidateView.setSuggestions(suggestions, completions, typedWordValid);
mCandidateView.setSuggestions(completions, isCompletions, typedWordValid);
}
}
@@ -590,19 +584,22 @@ public class TranslatorInputMethodService extends InputMethodService
}
public void pickSuggestionManually(int index) {
if (mCompletionOn && mCompletions != null && index >= 0
&& index < mCompletions.length) {
CompletionInfo ci = mCompletions[index];
getCurrentInputConnection().commitCompletion(ci);
if (mCompletions != null &&
index >= 0 && index < mCompletions.length) {
CompletionInfo ci = mCompletions[index];
if (mCompletionOn)
getCurrentInputConnection().commitCompletion(ci);
else {
mComposing.setLength(0);
mComposing.append(ci.getText());
commitTyped(getCurrentInputConnection());
}
if (mCandidateView != null) {
mCandidateView.clear();
}
updateShiftKeyState(getCurrentInputEditorInfo());
} else if (mComposing.length() > 0) {
// If we were generating candidate suggestions for the current
// text, we would commit one of them here. But for this sample,
// we will just commit the current text.
commitTyped(getCurrentInputConnection());
}
}