/* * Parse.java * Copyright (C) 2004-2005, Bjorn Bringert (bringert@cs.chalmers.se) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package se.chalmers.cs.gf.oaa; import se.gu.ling.trindikit.oaa.common.*; import com.sri.oaa2.icl.*; import se.chalmers.cs.gf.GFException; import se.chalmers.cs.gf.abssyn.*; import se.chalmers.cs.gf.translate.*; import se.chalmers.cs.gf.util.Pair; import static se.chalmers.cs.gf.util.Pair.pair; import java.util.*; /** * Implements the parsing solvable. */ public class Parse extends OAASolver { private static String solvableString = "parse(Grammar,Lang,Text,Tree)"; private Translators translators; public Parse(Translators translators) { super(solvableString); this.translators = translators; } public boolean solve(IclTerm goal, IclList params, IclList answers) { String grammar; IclTerm langTerm; String text; IclTerm result; try { grammar = ((IclStr)goal.getTerm(0)).toUnquotedString(); langTerm = goal.getTerm(1); text = ((IclStr)goal.getTerm(2)).toUnquotedString(); result = goal.getTerm(3); } catch (ClassCastException e){ return false; } Translator translator = translators.getTranslator(grammar); if (translator == null) { System.err.println("Unknown grammar " + grammar); return false; } Collection> ts = parse(translator, langTerm, text); System.err.println("Parser returned " + ts.size() + " results."); boolean success = false; for (Pair p : ts) { String lang = p.fst; Tree t = p.snd; IclTerm parseResult = TreeToIclTerm.treeToIclTerm(t); System.err.println("Trying to unify " + result + " and " + parseResult); IclTerm r = Unifier.getInstance().unify(result, parseResult); if (r != null) { IclTerm ans = new IclStruct("parse", new IclStr(grammar), new IclStr(lang), new IclStr(text), r); System.err.println("Adding answer " + ans); answers.add(ans); success = true; } } System.err.println("Returning " + success); return success; } public static Collection> parse(Translator translator, IclTerm langTerm, String text) { try { if (langTerm.isStr()) { String origLang = ((IclStr)langTerm).toUnquotedString(); String lang = origLang; String cat = null; int i; if ((i = lang.indexOf('.')) != -1) { cat = lang.substring(i+1); lang = lang.substring(0,i); System.err.println("Language = " + lang + ", category = " + cat); } List> ts = new LinkedList>(); Collection ps = cat == null ? translator.parse(lang, text) : translator.parse(lang, text, cat); for (Tree t : ps) ts.add(pair(origLang,t)); return ts; } else if (langTerm.isVar()) { System.err.println("Parsing '" + text + "' as any language"); return translator.parseWithAll(text); } else { System.err.println("Bad language " + langTerm); return new LinkedList>(); } } catch (GFException ex) { ex.printStackTrace(); return new LinkedList>(); } } }