001 // This file is part of AceWiki. 002 // Copyright 2008-2012, AceWiki developers. 003 // 004 // AceWiki is free software: you can redistribute it and/or modify it under the terms of the GNU 005 // Lesser General Public License as published by the Free Software Foundation, either version 3 of 006 // the License, or (at your option) any later version. 007 // 008 // AceWiki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 010 // Lesser General Public License for more details. 011 // 012 // You should have received a copy of the GNU Lesser General Public License along with AceWiki. If 013 // not, see http://www.gnu.org/licenses/. 014 015 package ch.uzh.ifi.attempto.acewiki.aceowl; 016 017 import java.util.ArrayList; 018 import java.util.List; 019 020 import ch.uzh.ifi.attempto.acewiki.core.EditorController; 021 import ch.uzh.ifi.attempto.acewiki.core.MonolingualHandler; 022 import ch.uzh.ifi.attempto.acewiki.core.Ontology; 023 import ch.uzh.ifi.attempto.acewiki.core.Sentence; 024 import ch.uzh.ifi.attempto.acewiki.core.SentenceSuggestion; 025 import ch.uzh.ifi.attempto.base.PredictiveParser; 026 import ch.uzh.ifi.attempto.base.TextContainer; 027 import ch.uzh.ifi.attempto.base.TextElement; 028 import ch.uzh.ifi.attempto.base.TextOperator; 029 import ch.uzh.ifi.attempto.chartparser.ChartParser; 030 import ch.uzh.ifi.attempto.chartparser.ParseTree; 031 032 /** 033 * This is a language handler implementation for ACE. 034 * 035 * @author Tobias Kuhn 036 */ 037 public class ACEHandler extends MonolingualHandler { 038 039 private TextOperator textOperator; 040 private EditorController editContr = new EditorController(); 041 private ACEOWLLexicon lexicon = new ACEOWLLexicon(); 042 043 /** 044 * Creates a new language handler for ACE. 045 */ 046 public ACEHandler() { 047 setLexiconChanger("propername", new ProperNameChanger()); 048 setLexiconChanger("noun", new NounChanger()); 049 setLexiconChanger("nounof", new NounOfChanger()); 050 setLexiconChanger("trverb", new VerbChanger()); 051 setLexiconChanger("tradj", new TrAdjChanger()); 052 053 editContr.setDefaultMenuGroup("function word"); 054 055 // menu group color shift 056 editContr.addMenuGroup("function word", 0); 057 editContr.addMenuGroup("proper name", 60); 058 editContr.addMenuGroup("noun", 100); 059 editContr.addMenuGroup("plural noun", 120); 060 editContr.addMenuGroup("of-construct", 140); 061 editContr.addMenuGroup("transitive adjective", 180); 062 editContr.addMenuGroup("verb", 210); 063 editContr.addMenuGroup("passive verb", 210); 064 editContr.addMenuGroup("new variable", 320); 065 editContr.addMenuGroup("reference", 320); 066 067 // category menu group word type / number 068 editContr.addExtensibleCategory("propername", "proper name", "propername", 0); 069 editContr.addExtensibleCategory("noun", "noun", "noun", 0); 070 editContr.addExtensibleCategory("nounpl", "plural noun", "noun", 1); 071 editContr.addExtensibleCategory("nounof", "of-construct", "nounof", 0); 072 editContr.addExtensibleCategory("verbsg", "verb", "trverb", 0); 073 editContr.addExtensibleCategory("verbinf", "verb", "trverb", 1); 074 editContr.addExtensibleCategory("pverb", "passive verb", "trverb", 2); 075 editContr.addExtensibleCategory("tradj", "transitive adjective", "tradj", 0); 076 077 // category menu group 078 editContr.addPlainCategory("defnoun", "reference"); 079 editContr.addPlainCategory("variable", "new variable"); 080 editContr.addPlainCategory("reference", "reference"); 081 082 editContr.setAutocompleteTokens(".", "?"); 083 } 084 085 public void init(Ontology ontology) { 086 lexicon.init(ontology); 087 textOperator = new ACETextOperator(ontology); 088 } 089 090 public TextOperator getTextOperator() { 091 return textOperator; 092 } 093 094 public List<Sentence> extractSentences(TextContainer tc, PredictiveParser pp) { 095 List<Sentence> l = new ArrayList<Sentence>(); 096 ChartParser parser = (ChartParser) pp; 097 List<ParseTree> subTrees = parser.getParseTree().getSubTrees("complete_sentence"); 098 for (ParseTree pt : subTrees) { 099 TextContainer c = tc.getSubTextContainer(pt.getStartPos(), pt.getEndPos()); 100 int s = c.getTextElementsCount(); 101 if (s > 0) { 102 if (c.getTextElement(s-1).getOriginalText().equals("?")) { 103 l.add(new ACEQuestion(c)); 104 } else { 105 l.add(new ACEDeclaration(c)); 106 } 107 } 108 } 109 return l; 110 } 111 112 public EditorController getEditorController() { 113 return editContr; 114 } 115 116 public PredictiveParser getPredictiveParser() { 117 ChartParser cp = new ChartParser(ACEGrammar.grammar, "text"); 118 cp.setDynamicLexicon(lexicon); 119 return cp; 120 } 121 122 public SentenceSuggestion getSuggestion(Sentence sentence) { 123 ACESentence s = (ACESentence) sentence; 124 List<TextElement> t = s.getTextElements(); 125 String t0 = t.get(0).getText(); 126 String t1 = t.get(1).getText(); 127 String l = t.get(t.size()-1).getText(); 128 if (t0.matches("(A|a)n?") && !t1.matches(".* of") && l.equals(".")) { 129 return new AToEverySuggestion(s); 130 } 131 return null; 132 } 133 134 }