mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
GWT client now has the same func
This commit is contained in:
@@ -8,8 +8,19 @@ public class CompletionOracle extends SuggestOracle {
|
||||
|
||||
private GF gf;
|
||||
|
||||
public CompletionOracle (String gfBaseURL) {
|
||||
gf = new GF(gfBaseURL);
|
||||
private List<String> inputLangs;
|
||||
|
||||
public CompletionOracle (GF gf) {
|
||||
this.gf = gf;
|
||||
inputLangs = new ArrayList<String>();
|
||||
}
|
||||
|
||||
public void setInputLangs(List<String> inputLangs) {
|
||||
this.inputLangs = inputLangs;
|
||||
}
|
||||
|
||||
public List<String> getInputLangs() {
|
||||
return inputLangs;
|
||||
}
|
||||
|
||||
public static class CompletionSuggestion implements SuggestOracle.Suggestion {
|
||||
@@ -28,15 +39,13 @@ public class CompletionOracle extends SuggestOracle {
|
||||
}
|
||||
|
||||
public void requestSuggestions(final SuggestOracle.Request request, final SuggestOracle.Callback callback) {
|
||||
gf.complete(request.getQuery(), request.getLimit(), new GF.CompleteCallback() {
|
||||
gf.complete(request.getQuery(), getInputLangs(), null, request.getLimit(),
|
||||
new GF.CompleteCallback() {
|
||||
public void onCompleteDone(GF.Completions completions) {
|
||||
Collection<CompletionSuggestion> suggestions = new ArrayList<CompletionSuggestion>();
|
||||
for (String lang : completions.getCompletionLanguages()) {
|
||||
int c = completions.countCompletions(lang);
|
||||
for (int i = 0; i < c; i++) {
|
||||
String text = request.getQuery() + " " + completions.getCompletion(lang,i);
|
||||
suggestions.add(new CompletionSuggestion(text));
|
||||
}
|
||||
for (int i = 0; i < completions.length(); i++) {
|
||||
String text = request.getQuery() + " " + completions.get(i).getText();
|
||||
suggestions.add(new CompletionSuggestion(text));
|
||||
}
|
||||
callback.onSuggestionsReady(request, new SuggestOracle.Response(suggestions));
|
||||
}
|
||||
|
||||
@@ -2,14 +2,16 @@ package se.chalmers.cs.gf.gwt_translate.client;
|
||||
|
||||
import com.google.gwt.http.client.*;
|
||||
import com.google.gwt.core.client.GWT;
|
||||
import com.google.gwt.core.client.JsArray;
|
||||
import com.google.gwt.core.client.JavaScriptObject;
|
||||
|
||||
import com.google.gwt.json.client.JSONValue;
|
||||
import com.google.gwt.json.client.JSONObject;
|
||||
import com.google.gwt.json.client.JSONParser;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class GF {
|
||||
|
||||
@@ -19,38 +21,101 @@ public class GF {
|
||||
this.baseURL = baseURL;
|
||||
}
|
||||
|
||||
/* Translation */
|
||||
/* Languages */
|
||||
|
||||
|
||||
/* Completion */
|
||||
|
||||
public void complete (String text, int count, final CompleteCallback callback) {
|
||||
Map<String,String> args = new HashMap<String,String>();
|
||||
args.put("input",text);
|
||||
// args.put("from",);
|
||||
// args.put("cat",);
|
||||
// args.put("count",);
|
||||
sendRequest("complete", args, new JSONRequestCallback() {
|
||||
public void languages (final LanguagesCallback callback) {
|
||||
sendRequest("languages", null, new JSONRequestCallback() {
|
||||
public void onJSONReceived(JSONValue json) {
|
||||
callback.onCompleteDone(new Completions(json));
|
||||
callback.onLanguagesDone((Languages)json.isArray().getJavaScriptObject().cast());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static final class Completions {
|
||||
private final JSONObject completions;
|
||||
public Completions (JSONValue json) {
|
||||
this.completions = json.isObject();
|
||||
public static class Languages extends JsArray<Language> {
|
||||
protected Languages() { }
|
||||
}
|
||||
|
||||
public static class Language extends JavaScriptObject {
|
||||
protected Language() { }
|
||||
|
||||
public final native String getName() /*-{ return this.name; }-*/;
|
||||
public final native String getLanguageCode() /*-{ return this.languageCode; }-*/;
|
||||
public final native boolean canParse() /*-{ return this.canParse; }-*/;
|
||||
}
|
||||
|
||||
|
||||
public interface LanguagesCallback {
|
||||
public void onLanguagesDone (Languages languages);
|
||||
}
|
||||
|
||||
/* Translation */
|
||||
|
||||
public void translate (String input, List<String> fromLangs, String cat, List<String> toLangs,
|
||||
final TranslateCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("input", input));
|
||||
if (fromLangs != null) {
|
||||
for (String from : fromLangs) {
|
||||
args.add(new Arg("from", from));
|
||||
}
|
||||
}
|
||||
public final Set<String> getCompletionLanguages() { return completions.keySet(); }
|
||||
public final int countCompletions(String lang) {
|
||||
JSONValue cs = completions.get(lang);
|
||||
return cs == null ? 0 : cs.isArray().size();
|
||||
args.add(new Arg("cat", cat));
|
||||
if (toLangs != null) {
|
||||
for (String to : toLangs) {
|
||||
args.add(new Arg("to", to));
|
||||
}
|
||||
}
|
||||
public final String getCompletion(String lang, int i) {
|
||||
JSONValue cs = completions.get(lang);
|
||||
return cs == null ? null : cs.isArray().get(i).isString().stringValue();
|
||||
sendRequest("translate", args, new JSONRequestCallback() {
|
||||
public void onJSONReceived(JSONValue json) {
|
||||
callback.onTranslateDone((Translations)json.isArray().getJavaScriptObject().cast());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static class Translations extends JsArray<Translation> {
|
||||
protected Translations() { }
|
||||
}
|
||||
|
||||
public static class Translation extends JavaScriptObject {
|
||||
protected Translation() { }
|
||||
|
||||
public final native String getFrom() /*-{ return this.from; }-*/;
|
||||
public final native String getTo() /*-{ return this.to; }-*/;
|
||||
public final native String getText() /*-{ return this.text; }-*/;
|
||||
}
|
||||
|
||||
public interface TranslateCallback {
|
||||
public void onTranslateDone (Translations translations);
|
||||
}
|
||||
|
||||
/* Completion */
|
||||
|
||||
public void complete (String input, List<String> fromLangs, String cat, int limit, final CompleteCallback callback) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("input", input));
|
||||
if (fromLangs != null) {
|
||||
for (String from : fromLangs) {
|
||||
args.add(new Arg("from", from));
|
||||
}
|
||||
}
|
||||
args.add(new Arg("cat", cat));
|
||||
args.add(new Arg("limit", limit));
|
||||
sendRequest("complete", args, new JSONRequestCallback() {
|
||||
public void onJSONReceived(JSONValue json) {
|
||||
callback.onCompleteDone((Completions)json.isArray().getJavaScriptObject().cast());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static class Completions extends JsArray<Translation> {
|
||||
protected Completions() { }
|
||||
}
|
||||
|
||||
public static class Completion extends JavaScriptObject {
|
||||
protected Completion() { }
|
||||
|
||||
public final native String getFrom() /*-{ return this.from; }-*/;
|
||||
public final native String getText() /*-{ return this.text; }-*/;
|
||||
}
|
||||
|
||||
public interface CompleteCallback {
|
||||
@@ -64,7 +129,7 @@ public class GF {
|
||||
public void onJSONReceived(JSONValue json);
|
||||
}
|
||||
|
||||
private void sendRequest (String resource, Map<String,String> vars, final JSONRequestCallback callback) {
|
||||
private void sendRequest (String resource, List<Arg> vars, final JSONRequestCallback callback) {
|
||||
String url = baseURL + "/" + resource + "?" + buildQueryString(vars);
|
||||
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
|
||||
|
||||
@@ -78,7 +143,7 @@ public class GF {
|
||||
if (200 == response.getStatusCode()) {
|
||||
callback.onJSONReceived(JSONParser.parse(response.getText()));
|
||||
} else {
|
||||
GWT.log("Response not OK: " + response.getStatusCode(), null);
|
||||
GWT.log("Response not OK: " + response.getStatusCode() + ". " + response.getText(), null);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -87,16 +152,32 @@ public class GF {
|
||||
}
|
||||
}
|
||||
|
||||
private static String buildQueryString(Map<String,String> vars) {
|
||||
private static class Arg {
|
||||
public final String name;
|
||||
public final String value;
|
||||
public Arg (String name, String value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
public Arg (String name, int value) {
|
||||
this(name, Integer.toString(value));
|
||||
}
|
||||
}
|
||||
|
||||
private static String buildQueryString(List<Arg> args) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
for (Map.Entry<String,String> e : vars.entrySet()) {
|
||||
if (sb.length() > 0) {
|
||||
sb.append("&");
|
||||
if (args != null) {
|
||||
for (Arg arg : args) {
|
||||
if (arg.value != null) {
|
||||
if (sb.length() > 0) {
|
||||
sb.append("&");
|
||||
}
|
||||
sb.append(URL.encodeComponent(arg.name));
|
||||
sb.append("=");
|
||||
sb.append(URL.encodeComponent(arg.value));
|
||||
}
|
||||
}
|
||||
sb.append(URL.encodeComponent(e.getKey()));
|
||||
sb.append("=");
|
||||
sb.append(URL.encodeComponent(e.getValue()));
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,10 +2,14 @@ package se.chalmers.cs.gf.gwt_translate.client;
|
||||
|
||||
import com.google.gwt.core.client.EntryPoint;
|
||||
import com.google.gwt.user.client.ui.Button;
|
||||
import com.google.gwt.user.client.ui.ChangeListener;
|
||||
import com.google.gwt.user.client.ui.ClickListener;
|
||||
import com.google.gwt.user.client.ui.DialogBox;
|
||||
import com.google.gwt.user.client.ui.Grid;
|
||||
import com.google.gwt.user.client.ui.Image;
|
||||
import com.google.gwt.user.client.ui.HorizontalPanel;
|
||||
import com.google.gwt.user.client.ui.Label;
|
||||
import com.google.gwt.user.client.ui.ListBox;
|
||||
import com.google.gwt.user.client.ui.RootPanel;
|
||||
import com.google.gwt.user.client.ui.SuggestBox;
|
||||
import com.google.gwt.user.client.ui.VerticalPanel;
|
||||
@@ -14,22 +18,56 @@ import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
|
||||
|
||||
import com.google.gwt.user.client.Window;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class Translate implements EntryPoint {
|
||||
|
||||
private static final String gfBaseURL = "http://localhost/~bringert/gf-server/gf.fcgi";
|
||||
|
||||
private GF gf;
|
||||
|
||||
private CompletionOracle oracle;
|
||||
private SuggestBox suggest;
|
||||
private List<String> fromLangs;
|
||||
private List<String> toLangs;
|
||||
private Grid outputTable;
|
||||
|
||||
private void translate() {
|
||||
outputTable.resizeRows(3);
|
||||
outputTable.setText(0, 1, suggest.getText());
|
||||
outputTable.setText(1, 1, suggest.getText());
|
||||
outputTable.setText(2, 1, suggest.getText());
|
||||
gf.translate(suggest.getText(), fromLangs, null, toLangs, new GF.TranslateCallback() {
|
||||
public void onTranslateDone (GF.Translations translations) {
|
||||
outputTable.resizeRows(translations.length());
|
||||
for (int i = 0; i < translations.length(); i++) {
|
||||
GF.Translation t = translations.get(i);
|
||||
outputTable.setText(i, 0, t.getFrom());
|
||||
outputTable.setText(i, 1, t.getTo());
|
||||
outputTable.setText(i, 2, t.getText());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private List<String> listBoxSelection(ListBox box) {
|
||||
int c = box.getItemCount();
|
||||
List<String> l = new ArrayList<String>();
|
||||
for (int i = 0; i < c; i++) {
|
||||
if (box.isItemSelected(i)) {
|
||||
l.add(box.getValue(i));
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
|
||||
public void onModuleLoad() {
|
||||
|
||||
suggest = new SuggestBox(new CompletionOracle("http://localhost/~bringert/gf-server/gf.fcgi"));
|
||||
gf = new GF(gfBaseURL);
|
||||
|
||||
oracle = new CompletionOracle(gf);
|
||||
|
||||
suggest = new SuggestBox(oracle);
|
||||
suggest.setWidth("50em");
|
||||
suggest.setText("this cheese is warm");
|
||||
suggest.addKeyboardListener(new KeyboardListenerAdapter() {
|
||||
public void onKeyUp (Widget sender, char keyCode, int modifiers) {
|
||||
if (keyCode == KEY_ENTER) {
|
||||
@@ -38,6 +76,25 @@ public class Translate implements EntryPoint {
|
||||
}
|
||||
});
|
||||
|
||||
final ListBox fromLangBox = new ListBox();
|
||||
fromLangBox.addItem("Any language", "");
|
||||
fromLangBox.addChangeListener(new ChangeListener() {
|
||||
public void onChange(Widget sender) {
|
||||
fromLangs = listBoxSelection(fromLangBox);
|
||||
oracle.setInputLangs(fromLangs);
|
||||
translate();
|
||||
}
|
||||
});
|
||||
|
||||
final ListBox toLangBox = new ListBox();
|
||||
toLangBox.addItem("All languages", "");
|
||||
toLangBox.addChangeListener(new ChangeListener() {
|
||||
public void onChange(Widget sender) {
|
||||
toLangs = listBoxSelection(toLangBox);
|
||||
translate();
|
||||
}
|
||||
});
|
||||
|
||||
Button translateButton = new Button("Translate");
|
||||
translateButton.addClickListener(new ClickListener() {
|
||||
public void onClick(Widget sender) {
|
||||
@@ -45,16 +102,35 @@ public class Translate implements EntryPoint {
|
||||
}
|
||||
});
|
||||
|
||||
outputTable = new Grid(0, 2);
|
||||
HorizontalPanel hPanel = new HorizontalPanel();
|
||||
hPanel.add(new Label("From:"));
|
||||
hPanel.add(fromLangBox);
|
||||
hPanel.add(new Label("To:"));
|
||||
hPanel.add(toLangBox);
|
||||
hPanel.add(translateButton);
|
||||
|
||||
outputTable = new Grid(0, 3);
|
||||
|
||||
VerticalPanel vPanel = new VerticalPanel();
|
||||
vPanel.setWidth("100%");
|
||||
vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
|
||||
vPanel.add(suggest);
|
||||
vPanel.add(translateButton);
|
||||
vPanel.add(hPanel);
|
||||
vPanel.add(outputTable);
|
||||
|
||||
RootPanel.get().add(vPanel);
|
||||
|
||||
gf.languages(new GF.LanguagesCallback() {
|
||||
public void onLanguagesDone(GF.Languages languages) {
|
||||
for (int i = 0; i < languages.length(); i++) {
|
||||
GF.Language l = languages.get(i);
|
||||
if (l.canParse()) {
|
||||
fromLangBox.addItem(l.getName());
|
||||
}
|
||||
toLangBox.addItem(l.getName());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user