-
Notifications
You must be signed in to change notification settings - Fork 5
Getting started: using a PGF grammar in an android app
- a cool android application
- the pgf file of your grammar (compiled with the -optimize-pgf option)
- the JPGF jar
In your android source you should have a folder called "res". This is the folder in which everything goes if it is not java (images, layouts, strings...) In this 'res' folder, create a new folder called 'raw' in which you will put your pgf file. Be careful, resource file names are limited to [a-z0-9_]+ so you may need to rename your pgf. Finally it is worth noting that there is a limit of 2Mio for resource files. If your grammar is more than that you won't be able to open it (reading it on the phone would take forever anyway)
This operation depends on your development environment. On eclipse it is done in your project's properties ; if you are using ant or sbt, just drop the jar in the 'lob' folder.
Once the pgf is in place, you need to be able to load it in the application. In this example, I will assume that the file is "res/raw/my_grammar.pgf". You can then get its resource id using R.raw.my_grammar. Then use the following code in your Activity class to read the pgf :
import org.grammaticalframework.PGFBuilder;
import org.grammaticalframework.PGF;
import java.io.InputStream
...
// loading the PGF
InputStream is = this.getResources().openRawResource(R.raw.foods);
PGF myPGF = PGFBuilder.fromInputStream(is);
Parsing and linearization are done through the Parser and the Linearizer class respectively.
Here is an example of simple translation function :
import org.grammaticalframework.Parser;
import org.grammaticalframework.Linearizer;
import org.grammaticalframework.Trees.Abssyn.Tree;
...
public String translate(String english_text) {
Parser myParser = new Parser(myPGF, "ConcreteEng");
Linearizer myLinearizer = new Linearizer(myPGF, "ConcreteSwe");
Tree tree = Parser.parse(english_text)[0];
return myLinearizer.linearizeString(tree);
}
Random generation is done with a Generator object. The Generator generates directly a Tree object that can be linearized using a Linearizer instance.
import org.grammaticalframework.Generator;
import org.grammaticalframework.Linearizer;
import org.grammaticalframework.Trees.Abssyn.Tree;
...
private String generate() {
Tree tree = new Generator(myPGF).gen();
return new Linearizer(myPGF, "ConcreteEng").linearizeString(tree);
}