// load grammar
URL grammarURL = new File(grammarfile).toURI().toURL();
System.out.println("Loading grammar from URL: " + grammarURL);
Grammar grammar = new Grammar(grammarURL);
Tokenizer tokenizer = grammar.lexicon.tokenizer;
System.out.println();
// set up parser
Parser parser = new Parser(grammar);
// instantiate scorer
try {
System.out.println("Instantiating parsing sign scorer from class: " + parseScorerClass);
SignScorer parseScorer = (SignScorer) Class.forName(parseScorerClass).newInstance();
parser.setSignScorer(parseScorer);
System.out.println();
} catch (Exception exc) {
throw (RuntimeException) new RuntimeException().initCause(exc);
}
// instantiate supertagger
try {
Supertagger supertagger;
if (supertaggerClass != null) {
System.out.println("Instantiating supertagger from class: " + supertaggerClass);
supertagger = (Supertagger) Class.forName(supertaggerClass).newInstance();
}
else {
System.out.println("Instantiating supertagger from config file: " + stconfig);
supertagger = WordAndPOSDictionaryLabellingStrategy.supertaggerFactory(stconfig);
}
parser.setSupertagger(supertagger);
System.out.println();
} catch (Exception exc) {
throw (RuntimeException) new RuntimeException().initCause(exc);
}
// loop through input
BufferedReader in = new BufferedReader(new FileReader(inputfile));
String line;
Map<String,String> predInfoMap = new HashMap<String,String>();
System.out.println("Parsing " + inputfile);
System.out.println();
int count = 1;
while ((line = in.readLine()) != null) {
String id = "s" + count;
try {
// parse it
System.out.println(line);
parser.parse(line);
int numParses = Math.min(nbestListSize, parser.getResult().size());
for (int i=0; i < numParses; i++) {
Sign thisParse = parser.getResult().get(i);
// convert lf
Category cat = thisParse.getCategory();
LF convertedLF = null;
String predInfo = null;
if (cat.getLF() != null) {
// convert LF
LF flatLF = cat.getLF();
cat = cat.copy();
Nominal index = cat.getIndexNominal();
convertedLF = HyloHelper.compactAndConvertNominals(flatLF, index, thisParse);
// get pred info
predInfoMap.clear();
Testbed.extractPredInfo(flatLF, predInfoMap);
predInfo = Testbed.getPredInfo(predInfoMap);
}
// add test item, sign
Element item = RegressionInfo.makeTestItem(grammar, line, 1, convertedLF);
String actualID = (nbestListSize == 1) ? id : id + "-" + (i+1);
item.setAttribute("info", actualID);
outRoot.addContent(item);
signMap.put(actualID, thisParse);
// Add parsed words as a separate LF element
Element fullWordsElt = new Element("full-words");
fullWordsElt.addContent(tokenizer.format(thisParse.getWords()));
item.addContent(fullWordsElt);
if (predInfo != null) {
Element predInfoElt = new Element("pred-info");
predInfoElt.setAttribute("data", predInfo);
item.addContent(predInfoElt);