mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-05-19 16:12:52 -06:00
added tree visualizations in TranslateApp
This commit is contained in:
@@ -58,7 +58,9 @@ public class FridgeApp implements EntryPoint {
|
||||
public void onResult (PGF.Translations translations) {
|
||||
outputPanel.removeStyleDependentName("working");
|
||||
for (PGF.Translation t : translations.iterable()) {
|
||||
outputPanel.add(createTranslation(t.getTo(), t.getText()));
|
||||
for (PGF.Linearization l : t.getLinearizations().iterable()) {
|
||||
outputPanel.add(createTranslation(l.getTo(), l.getText()));
|
||||
}
|
||||
}
|
||||
}
|
||||
public void onError (Throwable e) {
|
||||
|
||||
@@ -25,7 +25,7 @@ public class JSONRequestBuilder {
|
||||
}
|
||||
|
||||
public static <T extends JavaScriptObject> JSONRequest sendRequest (String base, List<Arg> vars, final JSONCallback<T> callback) {
|
||||
String url = base + "?" + buildQueryString(vars);
|
||||
String url = getQueryURL(base,vars);
|
||||
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, url);
|
||||
builder.setTimeoutMillis(30000);
|
||||
builder.setHeader("Accept","text/plain, text/html;q=0.5, */*;q=0.1");
|
||||
@@ -57,8 +57,10 @@ public class JSONRequestBuilder {
|
||||
return eval('(' + json + ')');
|
||||
}-*/;
|
||||
|
||||
private static String buildQueryString(List<Arg> args) {
|
||||
public static String getQueryURL(String base, List<Arg> args) {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append(base);
|
||||
sb.append("?");
|
||||
if (args != null) {
|
||||
for (Arg arg : args) {
|
||||
if (arg.value != null) {
|
||||
|
||||
@@ -60,8 +60,19 @@ public class PGF {
|
||||
protected Translation() { }
|
||||
|
||||
public final native String getFrom() /*-{ return this.from; }-*/;
|
||||
public final native String getTree() /*-{ return this.tree; }-*/;
|
||||
public final native Linearizations getLinearizations() /*-{ return this.linearizations; }-*/;
|
||||
}
|
||||
|
||||
public static class Linearizations extends IterableJsArray<Linearization> {
|
||||
protected Linearizations() { }
|
||||
}
|
||||
|
||||
public static class Linearization extends JavaScriptObject {
|
||||
protected Linearization() { }
|
||||
|
||||
public final native String getTo() /*-{ return this.to; }-*/;
|
||||
public final native String getText() /*-{ return this.text; }-*/;
|
||||
public final native String getText() /*-{ return this.text; }-*/;
|
||||
}
|
||||
|
||||
/* Completion */
|
||||
@@ -118,6 +129,28 @@ public class PGF {
|
||||
public final native String getTree() /*-{ return this.tree; }-*/;
|
||||
}
|
||||
|
||||
public String graphvizAbstractTree(String pgfURL, String abstractTree) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("command", "abstrtree"));
|
||||
args.add(new Arg("tree", abstractTree));
|
||||
return JSONRequestBuilder.getQueryURL(pgfURL,args);
|
||||
}
|
||||
|
||||
public String graphvizParseTree(String pgfURL, String abstractTree, String lang) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("command", "parsetree"));
|
||||
args.add(new Arg("tree", abstractTree));
|
||||
args.add(new Arg("from", lang));
|
||||
return JSONRequestBuilder.getQueryURL(pgfURL,args);
|
||||
}
|
||||
|
||||
public String graphvizAlignment(String pgfURL, String abstractTree) {
|
||||
List<Arg> args = new ArrayList<Arg>();
|
||||
args.add(new Arg("command", "alignment"));
|
||||
args.add(new Arg("tree", abstractTree));
|
||||
return JSONRequestBuilder.getQueryURL(pgfURL,args);
|
||||
}
|
||||
|
||||
/* Common */
|
||||
|
||||
public <T extends JavaScriptObject> JSONRequest sendGrammarRequest(String pgfURL, String resource, List<Arg> args, final JSONCallback<T> callback) {
|
||||
|
||||
@@ -12,10 +12,12 @@ public class PGFWrapper {
|
||||
|
||||
private String baseURL;
|
||||
|
||||
private PGF pgf;
|
||||
private String grammarURL;
|
||||
|
||||
private String pgfName = null;
|
||||
|
||||
private PGF pgf;
|
||||
|
||||
private String inputLanguage = null;
|
||||
|
||||
private String outputLanguage = null;
|
||||
@@ -39,6 +41,10 @@ public class PGFWrapper {
|
||||
private List<SettingsListener> listeners = new LinkedList<SettingsListener>();
|
||||
|
||||
|
||||
public PGFWrapper() {
|
||||
this.baseURL = null;
|
||||
this.pgf = new PGF();
|
||||
}
|
||||
|
||||
public PGFWrapper(String baseURL) {
|
||||
this.baseURL = baseURL;
|
||||
@@ -93,7 +99,7 @@ public class PGFWrapper {
|
||||
|
||||
protected void updateSelectedGrammar () {
|
||||
clearCachedInfo();
|
||||
pgf.grammar(baseURL+"/"+pgfName, new PGF.GrammarCallback() {
|
||||
pgf.grammar(grammarURL, new PGF.GrammarCallback() {
|
||||
public void onResult(PGF.Grammar grammar) {
|
||||
userLanguage = grammar.getUserLanguage();
|
||||
languages = new LinkedHashMap<String,PGF.Language>();
|
||||
@@ -119,15 +125,27 @@ public class PGFWrapper {
|
||||
//
|
||||
|
||||
public JSONRequest translate (String input, final PGF.TranslateCallback callback) {
|
||||
return pgf.translate(baseURL+"/"+pgfName, input, inputLanguage, cat, outputLanguage, callback);
|
||||
return pgf.translate(grammarURL, input, inputLanguage, cat, outputLanguage, callback);
|
||||
}
|
||||
|
||||
public JSONRequest complete (String input, int limit, final PGF.CompleteCallback callback) {
|
||||
return pgf.complete(baseURL+"/"+pgfName, input, inputLanguage, cat, limit, callback);
|
||||
return pgf.complete(grammarURL, input, inputLanguage, cat, limit, callback);
|
||||
}
|
||||
|
||||
public JSONRequest parse (String input, final PGF.ParseCallback callback) {
|
||||
return pgf.parse(baseURL+"/"+pgfName, input, inputLanguage, cat, callback);
|
||||
return pgf.parse(grammarURL, input, inputLanguage, cat, callback);
|
||||
}
|
||||
|
||||
public String graphvizAbstractTree(String abstractTree) {
|
||||
return pgf.graphvizAbstractTree(grammarURL,abstractTree);
|
||||
}
|
||||
|
||||
public String graphvizParseTree(String abstractTree, String lang) {
|
||||
return pgf.graphvizParseTree(grammarURL,abstractTree,lang);
|
||||
}
|
||||
|
||||
public String graphvizAlignment(String abstractTree) {
|
||||
return pgf.graphvizAlignment(grammarURL,abstractTree);
|
||||
}
|
||||
|
||||
//
|
||||
@@ -140,6 +158,20 @@ public class PGFWrapper {
|
||||
|
||||
public void setPGFName(String pgfName) {
|
||||
this.pgfName = pgfName;
|
||||
this.grammarURL = baseURL + "/" + pgfName;
|
||||
this.inputLanguage = null;
|
||||
this.outputLanguage = null;
|
||||
this.cat = null;
|
||||
updateSelectedGrammar();
|
||||
}
|
||||
|
||||
public String getGrammarURL() {
|
||||
return grammarURL;
|
||||
}
|
||||
|
||||
public void setGrammarURL(String grammarURL) {
|
||||
this.pgfName = null;
|
||||
this.grammarURL = grammarURL;
|
||||
this.inputLanguage = null;
|
||||
this.outputLanguage = null;
|
||||
this.cat = null;
|
||||
|
||||
@@ -6,10 +6,7 @@ import com.google.gwt.core.client.EntryPoint;
|
||||
import com.google.gwt.core.client.GWT;
|
||||
import com.google.gwt.user.client.History;
|
||||
import com.google.gwt.user.client.HistoryListener;
|
||||
import com.google.gwt.user.client.ui.Label;
|
||||
import com.google.gwt.user.client.ui.RootPanel;
|
||||
import com.google.gwt.user.client.ui.VerticalPanel;
|
||||
import com.google.gwt.user.client.ui.Widget;
|
||||
import com.google.gwt.user.client.ui.*;
|
||||
|
||||
|
||||
public class TranslateApp implements EntryPoint {
|
||||
@@ -47,7 +44,24 @@ public class TranslateApp implements EntryPoint {
|
||||
outputPanel.clear();
|
||||
outputPanel.removeStyleDependentName("working");
|
||||
for (PGF.Translation t : translations.iterable()) {
|
||||
outputPanel.add(createTranslation(t.getTo(), t.getText()));
|
||||
HorizontalPanel hPanel = new HorizontalPanel();
|
||||
hPanel.addStyleName("my-translation-frame");
|
||||
VerticalPanel linsPanel = new VerticalPanel();
|
||||
linsPanel.addStyleName("my-translation-bar");
|
||||
hPanel.add(linsPanel);
|
||||
HorizontalPanel btnPanel = new HorizontalPanel();
|
||||
btnPanel.addStyleName("my-translation-btns");
|
||||
btnPanel.setSpacing(4);
|
||||
btnPanel.add(createAbsTreeButton(t.getTree()));
|
||||
btnPanel.add(createAlignButton(t.getTree()));
|
||||
hPanel.add(btnPanel);
|
||||
hPanel.setCellHorizontalAlignment(btnPanel,
|
||||
HasHorizontalAlignment.ALIGN_RIGHT);
|
||||
outputPanel.add(hPanel);
|
||||
|
||||
for (PGF.Linearization l : t.getLinearizations().iterable()) {
|
||||
linsPanel.add(createTranslation(l.getTo(), t.getTree(), l.getText()));
|
||||
}
|
||||
}
|
||||
}
|
||||
public void onError (Throwable e) {
|
||||
@@ -56,13 +70,113 @@ public class TranslateApp implements EntryPoint {
|
||||
});
|
||||
}
|
||||
|
||||
protected Widget createTranslation(String language, String text) {
|
||||
protected Widget createAbsTreeButton(final String abstractTree) {
|
||||
Image treeBtn = new Image("se.chalmers.cs.gf.gwt.TranslateApp/tree-btn.png");
|
||||
treeBtn.addClickListener(
|
||||
new ClickListener() {
|
||||
public void onClick(Widget sender) {
|
||||
// Create a dialog box and set the caption text
|
||||
final DialogBox dialogBox = new DialogBox();
|
||||
dialogBox.setText("Abstract Syntax Tree");
|
||||
|
||||
// Create a table to layout the content
|
||||
HorizontalPanel dialogContents = new HorizontalPanel();
|
||||
dialogContents.setSpacing(4);
|
||||
dialogBox.setWidget(dialogContents);
|
||||
|
||||
// Add an image to the dialog
|
||||
|
||||
Frame image = new Frame(pgf.graphvizAbstractTree(abstractTree));
|
||||
image.addStyleName("my-treeimage");
|
||||
dialogContents.add(image);
|
||||
|
||||
// Add a close button at the bottom of the dialog
|
||||
Button closeButton = new Button("Close",
|
||||
new ClickListener() {
|
||||
public void onClick(Widget sender) {
|
||||
dialogBox.hide();
|
||||
}
|
||||
});
|
||||
dialogContents.add(closeButton);
|
||||
|
||||
dialogBox.center();
|
||||
dialogBox.show();
|
||||
}
|
||||
});
|
||||
return treeBtn;
|
||||
}
|
||||
|
||||
protected Widget createAlignButton(final String abstractTree) {
|
||||
Image alignBtn = new Image("se.chalmers.cs.gf.gwt.TranslateApp/align-btn.png");
|
||||
alignBtn.addClickListener(
|
||||
new ClickListener() {
|
||||
public void onClick(Widget sender) {
|
||||
// Create a dialog box and set the caption text
|
||||
final DialogBox dialogBox = new DialogBox();
|
||||
dialogBox.setText("Word Alignment");
|
||||
|
||||
// Create a table to layout the content
|
||||
HorizontalPanel dialogContents = new HorizontalPanel();
|
||||
dialogContents.setSpacing(4);
|
||||
dialogBox.setWidget(dialogContents);
|
||||
|
||||
// Add an image to the dialog
|
||||
Frame image = new Frame(pgf.graphvizAlignment(abstractTree));
|
||||
image.addStyleName("my-alignmentimage");
|
||||
dialogContents.add(image);
|
||||
|
||||
// Add a close button at the bottom of the dialog
|
||||
Button closeButton = new Button("Close",
|
||||
new ClickListener() {
|
||||
public void onClick(Widget sender) {
|
||||
dialogBox.hide();
|
||||
}
|
||||
});
|
||||
dialogContents.add(closeButton);
|
||||
|
||||
dialogBox.center();
|
||||
dialogBox.show();
|
||||
}
|
||||
});
|
||||
return alignBtn;
|
||||
}
|
||||
|
||||
protected Widget createTranslation(final String language, final String abstractTree, String text) {
|
||||
Label l = new Label(text);
|
||||
l.addStyleName("my-translation");
|
||||
String lang = pgf.getLanguageCode(language);
|
||||
if (lang != null) {
|
||||
l.getElement().setLang(lang);
|
||||
}
|
||||
l.addClickListener(new ClickListener() {
|
||||
public void onClick(Widget sender) {
|
||||
// Create a dialog box and set the caption text
|
||||
final DialogBox dialogBox = new DialogBox();
|
||||
dialogBox.setText("Parse Tree");
|
||||
|
||||
// Create a table to layout the content
|
||||
HorizontalPanel dialogContents = new HorizontalPanel();
|
||||
dialogContents.setSpacing(4);
|
||||
dialogBox.setWidget(dialogContents);
|
||||
|
||||
// Add an image to the dialog
|
||||
Frame image = new Frame(pgf.graphvizParseTree(abstractTree, language));
|
||||
image.addStyleName("my-treeimage");
|
||||
dialogContents.add(image);
|
||||
|
||||
// Add a close button at the bottom of the dialog
|
||||
Button closeButton = new Button("Close",
|
||||
new ClickListener() {
|
||||
public void onClick(Widget sender) {
|
||||
dialogBox.hide();
|
||||
}
|
||||
});
|
||||
dialogContents.add(closeButton);
|
||||
|
||||
dialogBox.center();
|
||||
dialogBox.show();
|
||||
}
|
||||
});
|
||||
return l;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,13 +23,40 @@
|
||||
.my-translations {
|
||||
margin-top: 1em;
|
||||
}
|
||||
|
||||
|
||||
.my-translation-frame {
|
||||
width: 100%;
|
||||
margin: 0.5em;
|
||||
background: #D0E4F6;
|
||||
}
|
||||
|
||||
.my-translation-bar {
|
||||
padding-left: 25px;
|
||||
padding-right: 25px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.my-translation {
|
||||
margin: 0.2em;
|
||||
padding-left: 25px;
|
||||
font-size: 150%;
|
||||
background-repeat: no-repeat;
|
||||
background-position: 0% 50%;
|
||||
cursor:pointer;
|
||||
}
|
||||
|
||||
.my-translation-btns {
|
||||
background: #DDDDDD;
|
||||
cursor:pointer;
|
||||
}
|
||||
|
||||
.my-treeimage {
|
||||
width: 650px;
|
||||
height: 520px;
|
||||
}
|
||||
|
||||
.my-alignmentimage {
|
||||
width: 450px;
|
||||
height: 300px;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
BIN
src/server/gwt/src/se/chalmers/cs/gf/gwt/public/align-btn.png
Normal file
BIN
src/server/gwt/src/se/chalmers/cs/gf/gwt/public/align-btn.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 138 B |
BIN
src/server/gwt/src/se/chalmers/cs/gf/gwt/public/tree-btn.png
Normal file
BIN
src/server/gwt/src/se/chalmers/cs/gf/gwt/public/tree-btn.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 149 B |
Reference in New Issue
Block a user