fixes in the word completion

This commit is contained in:
kr.angelov
2014-03-12 10:54:29 +00:00
parent befa50eb5f
commit d653304dcf
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.GestureDetector;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.inputmethod.CompletionInfo;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -19,7 +20,7 @@ public class CompletionsView extends View {
private static final int OUT_OF_BOUNDS = -1; private static final int OUT_OF_BOUNDS = -1;
private TranslatorInputMethodService mService; private TranslatorInputMethodService mService;
private List<String> mSuggestions; private CompletionInfo[] mSuggestions;
private int mSelectedIndex; private int mSelectedIndex;
private int mTouchX = OUT_OF_BOUNDS; private int mTouchX = OUT_OF_BOUNDS;
private Drawable mSelectionHighlight; private Drawable mSelectionHighlight;
@@ -153,7 +154,7 @@ public class CompletionsView extends View {
} }
} }
int x = 0; int x = 0;
final int count = mSuggestions.size(); final int count = mSuggestions.length;
final int height = getHeight(); final int height = getHeight();
final Rect bgPadding = mBgPadding; final Rect bgPadding = mBgPadding;
final Paint paint = mPaint; final Paint paint = mPaint;
@@ -164,7 +165,7 @@ public class CompletionsView extends View {
final int y = (int) (((height - mPaint.getTextSize()) / 2) - mPaint.ascent()); final int y = (int) (((height - mPaint.getTextSize()) / 2) - mPaint.ascent());
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
String suggestion = mSuggestions.get(i); String suggestion = mSuggestions[i].getText().toString();
float textWidth = paint.measureText(suggestion); float textWidth = paint.measureText(suggestion);
final int wordWidth = (int) textWidth + X_GAP * 2; final int wordWidth = (int) textWidth + X_GAP * 2;
@@ -222,11 +223,11 @@ public class CompletionsView extends View {
} }
@SuppressLint("WrongCall") @SuppressLint("WrongCall")
public void setSuggestions(List<String> suggestions, boolean completions, public void setSuggestions(CompletionInfo[] suggestions, boolean completions,
boolean typedWordValid) { boolean typedWordValid) {
clear(); clear();
if (suggestions != null) { if (suggestions != null) {
mSuggestions = new ArrayList<String>(suggestions); mSuggestions = suggestions;
} }
mTypedWordValid = typedWordValid; mTypedWordValid = typedWordValid;
scrollTo(0, 0); scrollTo(0, 0);
@@ -238,7 +239,7 @@ public class CompletionsView extends View {
} }
public void clear() { public void clear() {
mSuggestions = EMPTY_LIST; mSuggestions = new CompletionInfo[0];
mTouchX = OUT_OF_BOUNDS; mTouchX = OUT_OF_BOUNDS;
mSelectedIndex = -1; mSelectedIndex = -1;
invalidate(); invalidate();
@@ -285,22 +286,6 @@ public class CompletionsView extends View {
return true; 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() { private void removeHighlight() {
mTouchX = OUT_OF_BOUNDS; mTouchX = OUT_OF_BOUNDS;
invalidate(); invalidate();

View File

@@ -5,6 +5,7 @@ import android.content.SharedPreferences;
import android.content.res.XmlResourceParser; import android.content.res.XmlResourceParser;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.view.inputmethod.CompletionInfo;
import org.grammaticalframework.pgf.Concr; import org.grammaticalframework.pgf.Concr;
import org.grammaticalframework.pgf.Expr; import org.grammaticalframework.pgf.Expr;
@@ -20,6 +21,7 @@ import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; 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>(); PriorityQueue<WordProb> queue = new PriorityQueue<WordProb>();
for (FullFormEntry entry : getSourceConcr().lookupWordPrefix(prefix)) { for (FullFormEntry entry : getSourceConcr().lookupWordPrefix(prefix)) {
WordProb wp = new WordProb(); WordProb wp = new WordProb();
@@ -348,13 +350,23 @@ public class Translator {
break; break;
} }
List<String> list = new ArrayList<String>(); CompletionInfo[] completions = new CompletionInfo[Math.min(queue.size(), 5)+1];
while (list.size() < 5 && queue.size() > 0) { completions[0] = new CompletionInfo(0, 0, prefix);
list.add(queue.poll().word); 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() { private PGF getGrammar() {

View File

@@ -241,12 +241,7 @@ public class TranslatorInputMethodService extends InputMethodService
return; return;
} }
List<String> stringList = new ArrayList<String>(); setSuggestions(completions, true, true);
for (int i = 0; i < completions.length; i++) {
CompletionInfo ci = completions[i];
if (ci != null) stringList.add(ci.getText().toString());
}
setSuggestions(stringList, true, true);
} }
} }
@@ -438,11 +433,10 @@ public class TranslatorInputMethodService extends InputMethodService
*/ */
private void updateCandidates() { private void updateCandidates() {
if (!mCompletionOn) { if (!mCompletionOn) {
if (mComposing.length() > 0) { if (mComposing.length() > 1) {
List<String> list = mCompletions =
mTranslator.lookupWordPrefix(mComposing.toString()); mTranslator.lookupWordPrefix(mComposing.toString());
list.add(0, mComposing.toString()); setSuggestions(mCompletions, true, true);
setSuggestions(list, true, true);
Log.d("KEYBOARD", mComposing.toString()); Log.d("KEYBOARD", mComposing.toString());
} else { } else {
setSuggestions(null, false, false); 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) { boolean typedWordValid) {
if (suggestions != null && suggestions.size() > 0) { if (completions != null && completions.length > 0) {
setCandidatesViewShown(true); setCandidatesViewShown(true);
} else if (isExtractViewShown()) { } else if (isExtractViewShown()) {
setCandidatesViewShown(true); setCandidatesViewShown(true);
} }
if (mCandidateView != null) { 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) { public void pickSuggestionManually(int index) {
if (mCompletionOn && mCompletions != null && index >= 0 if (mCompletions != null &&
&& index < mCompletions.length) { index >= 0 && index < mCompletions.length) {
CompletionInfo ci = mCompletions[index]; CompletionInfo ci = mCompletions[index];
getCurrentInputConnection().commitCompletion(ci);
if (mCompletionOn)
getCurrentInputConnection().commitCompletion(ci);
else {
mComposing.setLength(0);
mComposing.append(ci.getText());
commitTyped(getCurrentInputConnection());
}
if (mCandidateView != null) { if (mCandidateView != null) {
mCandidateView.clear(); mCandidateView.clear();
} }
updateShiftKeyState(getCurrentInputEditorInfo()); 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());
} }
} }