forked from GitHub/gf-core
an improvised dictionary mode in the Android translator
This commit is contained in:
@@ -14,7 +14,7 @@
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
android:theme="@style/AppTheme" android:name="GFTranslator">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:label="@string/app_name" >
|
||||
@@ -24,6 +24,7 @@
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name="LexicalEntryActivity"></activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
|
||||
BIN
src/ui/android/res/drawable-xhdpi/ic_dictionary.png
Normal file
BIN
src/ui/android/res/drawable-xhdpi/ic_dictionary.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.5 KiB |
24
src/ui/android/res/layout/activity_lexical_entry.xml
Normal file
24
src/ui/android/res/layout/activity_lexical_entry.xml
Normal file
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_width="match_parent"
|
||||
>
|
||||
|
||||
<org.grammaticalframework.ui.android.LanguageSelector
|
||||
android:id="@+id/show_language"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentTop="true"
|
||||
android:padding="0dp"
|
||||
/>
|
||||
|
||||
<ListView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_below="@id/show_language"
|
||||
android:orientation="vertical"
|
||||
android:id="@android:id/list" >
|
||||
</ListView>
|
||||
</RelativeLayout>
|
||||
@@ -1,11 +1,27 @@
|
||||
<TextView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:layout_marginRight="32dp"
|
||||
android:layout_gravity="left"
|
||||
android:padding="8dp"
|
||||
android:textSize="20sp"
|
||||
android:background="@drawable/first_person_utterance_bg"
|
||||
/>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/first_person_utterance_bg" >
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:layout_gravity="left"
|
||||
android:padding="8dp"
|
||||
android:textSize="20sp"
|
||||
/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/show_word"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_toRightOf="@id/text"
|
||||
android:padding="8dp"
|
||||
android:src="@drawable/ic_dictionary"
|
||||
android:visibility="gone"
|
||||
/>
|
||||
|
||||
</RelativeLayout>
|
||||
@@ -3,7 +3,9 @@ package org.grammaticalframework.ui.android;
|
||||
import android.content.Context;
|
||||
import android.util.AttributeSet;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ScrollView;
|
||||
import android.widget.TextView;
|
||||
|
||||
@@ -12,6 +14,8 @@ public class ConversationView extends ScrollView {
|
||||
private LayoutInflater mInflater;
|
||||
|
||||
private ViewGroup mContent;
|
||||
|
||||
private OnWordSelectedListener mListener;
|
||||
|
||||
public ConversationView(Context context, AttributeSet attrs, int defStyle) {
|
||||
super(context, attrs, defStyle);
|
||||
@@ -33,15 +37,21 @@ public class ConversationView extends ScrollView {
|
||||
}
|
||||
|
||||
public void addFirstPersonUtterance(CharSequence text) {
|
||||
addUtterance(R.layout.first_person_utterance, text);
|
||||
View view =
|
||||
mInflater.inflate(R.layout.first_person_utterance, mContent, false);
|
||||
TextView textview = (TextView) view.findViewById(R.id.text);
|
||||
textview.setText(text);
|
||||
mContent.addView(view);
|
||||
post(new Runnable() {
|
||||
public void run() {
|
||||
fullScroll(FOCUS_DOWN);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void addSecondPersonUtterance(CharSequence text) {
|
||||
addUtterance(R.layout.second_person_utterance, text);
|
||||
}
|
||||
|
||||
private void addUtterance(int res, CharSequence text) {
|
||||
TextView view = (TextView) mInflater.inflate(res, mContent, false);
|
||||
TextView view = (TextView)
|
||||
mInflater.inflate(R.layout.second_person_utterance, mContent, false);
|
||||
view.setText(text);
|
||||
mContent.addView(view);
|
||||
post(new Runnable() {
|
||||
@@ -51,12 +61,33 @@ public class ConversationView extends ScrollView {
|
||||
});
|
||||
}
|
||||
|
||||
public void updateLastUtterance(CharSequence text) {
|
||||
public void updateLastUtterance(CharSequence text, Object lexicon) {
|
||||
int count = mContent.getChildCount();
|
||||
if (count > 0) {
|
||||
TextView view = (TextView) mContent.getChildAt(count - 1);
|
||||
view.setText(text);
|
||||
View view = mContent.getChildAt(count - 1);
|
||||
TextView textview = (TextView) view.findViewById(R.id.text);
|
||||
textview.setText(text);
|
||||
|
||||
if (lexicon != null && mListener != null) {
|
||||
ImageView showWordButton = (ImageView) view.findViewById(R.id.show_word);
|
||||
showWordButton.setVisibility(VISIBLE);
|
||||
|
||||
final Object lexicon2 = lexicon;
|
||||
showWordButton.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
mListener.onWordSelected(lexicon2);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setOnWordSelectedListener(OnWordSelectedListener listener) {
|
||||
mListener = listener;
|
||||
}
|
||||
|
||||
public interface OnWordSelectedListener {
|
||||
public void onWordSelected(Object lexicon);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.grammaticalframework.ui.android;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
public class GFTranslator extends Application {
|
||||
private Translator mTranslator;
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
mTranslator = new Translator(this);
|
||||
}
|
||||
|
||||
public Translator getTranslator() {
|
||||
return mTranslator;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
package org.grammaticalframework.ui.android;
|
||||
|
||||
public class Language {
|
||||
private final String mLangCode;
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Language implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final String mLangCode;
|
||||
private final String mLangName;
|
||||
private final String mConcrete;
|
||||
|
||||
@@ -27,5 +31,10 @@ public class Language {
|
||||
public String toString() {
|
||||
return getLangName();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
Language other = (Language) o;
|
||||
return mLangCode.equals(other.mLangCode);
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,10 @@ public class LanguageSelector extends Spinner {
|
||||
setSelection(((LanguagesAdapter) getAdapter()).getPosition(selected));
|
||||
}
|
||||
|
||||
public Language getSelectedLanguage() {
|
||||
return (Language) getSelectedItem();
|
||||
}
|
||||
|
||||
public void setOnLanguageSelectedListener(final OnLanguageSelectedListener listener) {
|
||||
setOnItemSelectedListener(new OnItemSelectedListener() {
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package org.grammaticalframework.ui.android;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import android.app.ListActivity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.ArrayAdapter;
|
||||
|
||||
import org.grammaticalframework.pgf.*;
|
||||
import org.grammaticalframework.ui.android.LanguageSelector.OnLanguageSelectedListener;
|
||||
|
||||
public class LexicalEntryActivity extends ListActivity {
|
||||
|
||||
private Translator mTranslator;
|
||||
private LanguageSelector mShowLanguageView;
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_lexical_entry);
|
||||
|
||||
mTranslator = ((GFTranslator) getApplicationContext()).getTranslator();
|
||||
|
||||
mShowLanguageView = (LanguageSelector) findViewById(R.id.show_language);
|
||||
mShowLanguageView.setLanguages(mTranslator.getAvailableSourceLanguages());
|
||||
mShowLanguageView.setSelectedLanguage(mTranslator.getTargetLanguage());
|
||||
mShowLanguageView.setOnLanguageSelectedListener(new OnLanguageSelectedListener() {
|
||||
@Override
|
||||
public void onLanguageSelected(Language language) {
|
||||
mTranslator.setTargetLanguage(language);
|
||||
updateTranslations();
|
||||
}
|
||||
});
|
||||
|
||||
updateTranslations();
|
||||
}
|
||||
|
||||
private void updateTranslations() {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<MorphoAnalysis> list = (List<MorphoAnalysis>)
|
||||
getIntent().getExtras().getSerializable("analyses");
|
||||
|
||||
List<String> data = new ArrayList<String>();
|
||||
for (MorphoAnalysis a : list) {
|
||||
Expr e = Expr.readExpr(a.getLemma());
|
||||
String phrase = mTranslator.linearize(e);
|
||||
|
||||
if (!data.contains(phrase)) {
|
||||
data.add(phrase);
|
||||
}
|
||||
}
|
||||
|
||||
ArrayAdapter adapter = new ArrayAdapter(this,
|
||||
android.R.layout.simple_list_item_1,
|
||||
data);
|
||||
setListAdapter(adapter);
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,11 @@
|
||||
|
||||
package org.grammaticalframework.ui.android;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.speech.SpeechRecognizer;
|
||||
@@ -12,6 +16,8 @@ import android.widget.ImageView;
|
||||
|
||||
import org.grammaticalframework.ui.android.ASR.State;
|
||||
import org.grammaticalframework.ui.android.LanguageSelector.OnLanguageSelectedListener;
|
||||
import org.grammaticalframework.ui.android.ConversationView.OnWordSelectedListener;
|
||||
import org.grammaticalframework.pgf.MorphoAnalysis;
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
|
||||
@@ -34,9 +40,7 @@ public class MainActivity extends Activity {
|
||||
|
||||
private TTS mTts;
|
||||
|
||||
// mTranslator is static to ensure that the grammar
|
||||
// is loaded only once even if the activity has been recreated.
|
||||
private static Translator mTranslator;
|
||||
private Translator mTranslator;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -48,7 +52,7 @@ public class MainActivity extends Activity {
|
||||
mSourceLanguageView = (LanguageSelector) findViewById(R.id.source_language);
|
||||
mTargetLanguageView = (LanguageSelector) findViewById(R.id.target_language);
|
||||
mSwitchLanguagesButton = (ImageView) findViewById(R.id.switch_languages);
|
||||
|
||||
|
||||
mStartStopButton.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
@@ -61,15 +65,22 @@ public class MainActivity extends Activity {
|
||||
});
|
||||
|
||||
mStartStopButton.setEnabled(SpeechRecognizer.isRecognitionAvailable(this));
|
||||
|
||||
mConversationView.setOnWordSelectedListener(new OnWordSelectedListener() {
|
||||
@Override
|
||||
public void onWordSelected(Object lexicon) {
|
||||
Intent myIntent = new Intent(MainActivity.this, LexicalEntryActivity.class);
|
||||
myIntent.putExtra("analyses", (Serializable) lexicon);
|
||||
MainActivity.this.startActivity(myIntent);
|
||||
}
|
||||
});
|
||||
|
||||
mAsr = new ASR(this);
|
||||
mAsr.setListener(new SpeechInputListener());
|
||||
|
||||
mTts = new TTS(this);
|
||||
|
||||
if (mTranslator == null) {
|
||||
mTranslator = new Translator(this);
|
||||
}
|
||||
mTranslator = ((GFTranslator) getApplicationContext()).getTranslator();
|
||||
|
||||
mSourceLanguageView.setLanguages(mTranslator.getAvailableSourceLanguages());
|
||||
mSourceLanguageView.setSelectedLanguage(mTranslator.getSourceLanguage());
|
||||
@@ -148,11 +159,15 @@ public class MainActivity extends Activity {
|
||||
}
|
||||
|
||||
private void handlePartialSpeechInput(String input) {
|
||||
mConversationView.updateLastUtterance(input);
|
||||
mConversationView.updateLastUtterance(input, null);
|
||||
}
|
||||
|
||||
private void handleSpeechInput(final String input) {
|
||||
mConversationView.updateLastUtterance(input);
|
||||
List<MorphoAnalysis> list = mTranslator.lookupMorpho(input);
|
||||
if (list.size() == 0)
|
||||
list = null;
|
||||
|
||||
mConversationView.updateLastUtterance(input, list);
|
||||
new AsyncTask<Void,Void,String>() {
|
||||
@Override
|
||||
protected String doInBackground(Void... params) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.util.Log;
|
||||
|
||||
import org.grammaticalframework.pgf.Concr;
|
||||
import org.grammaticalframework.pgf.Expr;
|
||||
import org.grammaticalframework.pgf.MorphoAnalysis;
|
||||
import org.grammaticalframework.pgf.PGF;
|
||||
import org.grammaticalframework.pgf.ParseError;
|
||||
|
||||
@@ -19,14 +20,13 @@ public class Translator {
|
||||
private static final String TAG = "Translator";
|
||||
|
||||
// TODO: allow changing
|
||||
private String mGrammar = "ResourceDemo.pgf";
|
||||
private String mGrammar = "ParseEngAbs.pgf";
|
||||
|
||||
// TODO: build dynamically?
|
||||
private Language[] mLanguages = {
|
||||
new Language("en-US", "English", "ResourceDemoEng"),
|
||||
new Language("de-DE", "German", "ResourceDemoGer"),
|
||||
new Language("es-ES", "Spanish", "ResourceDemoSpa"),
|
||||
new Language("fr-FR", "French", "ResourceDemoFre"),
|
||||
new Language("en-US", "English", "ParseEng"),
|
||||
new Language("bg-BG", "Bulgarian", "ParseBul"),
|
||||
new Language("cmn-Hans-CN", "Chinese", "ParseChi")
|
||||
};
|
||||
|
||||
private Language mSourceLanguage;
|
||||
@@ -96,6 +96,15 @@ public class Translator {
|
||||
}
|
||||
}
|
||||
|
||||
public String linearize(Expr e) {
|
||||
Concr targetLang = getConcr(getTargetLanguage().getConcrete());
|
||||
return targetLang.linearize(e);
|
||||
}
|
||||
|
||||
public List<MorphoAnalysis> lookupMorpho(String sentence) {
|
||||
return getConcr(getSourceLanguage().getConcrete()).lookupMorpho(sentence);
|
||||
}
|
||||
|
||||
private Concr getConcr(String name) {
|
||||
return getGrammar().getLanguages().get(name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user