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 ch.uzh.ifi.attempto.acewiki.core.Ontology;
018    import ch.uzh.ifi.attempto.acewiki.core.OntologyElement;
019    import ch.uzh.ifi.attempto.acewiki.core.OntologyTextElement;
020    import ch.uzh.ifi.attempto.ape.ACEUtils;
021    import ch.uzh.ifi.attempto.base.DefaultTextOperator;
022    import ch.uzh.ifi.attempto.base.TextElement;
023    
024    /**
025     * This is the text operator used for the ACE/OWL language engine for AceWiki.
026     * 
027     * @author Tobias Kuhn
028     */
029    public class ACETextOperator extends DefaultTextOperator {
030            
031            private Ontology ontology;
032            
033            /**
034             * Creates a new text operator.
035             * 
036             * @param ontology The ontology object.
037             */
038            public ACETextOperator(Ontology ontology) {
039                    this.ontology = ontology;
040            }
041            
042            public TextElement createTextElement(String text) {
043                    OntologyTextElement ote;
044                    if (ACEGrammar.grammar.containsTerminalSymbol(text)) {
045                            return new TextElement(text);
046                    }
047                    if (ACEGrammar.grammar.containsTerminalSymbol(text.toLowerCase())) {
048                            return new TextElement(text.toLowerCase());
049                    }
050                    String t = text.replaceAll("[ _]+", "_");
051                    ote = createOntologyTextElement(t);
052                    if (ote != null) return ote;
053                    if (t.toLowerCase().startsWith("the_")) {
054                            ote = createOntologyTextElement("the " + t.substring(4));
055                            if (ote != null) {
056                                    return ote;
057                            }
058                            ote = createOntologyTextElement(t.substring(4));
059                            if (ote != null) {
060                                    ote.setPreText("the ");
061                                    return ote;
062                            }
063                    }
064                    if (t.toLowerCase().endsWith("_by")) {
065                            ote = createOntologyTextElement(t.replaceFirst("_by$", " by"));
066                            if (ote != null) return ote;
067                    }
068                    if (t.toLowerCase().endsWith("_of")) {
069                            ote = createOntologyTextElement(t.replaceFirst("_of$", " of"));
070                            if (ote != null) return ote;
071                    }
072                    return new TextElement(text);
073            }
074            
075            private OntologyTextElement createOntologyTextElement(String text) {
076                    OntologyElement oe = ontology.getElement(text);
077                    if (oe != null) {
078                            int wn = -1;
079                            String[] words = oe.getWords();
080                            for (int i = 0 ; i < words.length ; i++) {
081                                    if (text.equals(words[i])) wn = i;
082                            }
083                            if (wn > -1) {
084                                    return new OntologyTextElement(oe, wn);
085                            }
086                    }
087                    return null;
088            }
089            
090            public String getTextInContext(TextElement textElement, String preceding, String following) {
091                    String text = textElement.getOriginalText();
092                    String t;
093                    if (textElement instanceof OntologyTextElement) {
094                            OntologyElement oe = ((OntologyTextElement) textElement).getOntologyElement();
095                            boolean capitalize = false;
096                            if (preceding == null || preceding.matches("[.?!]")) {
097                                    ProperNameIndividual i = null;
098                                    if (oe instanceof ProperNameIndividual) {
099                                            i = (ProperNameIndividual) oe;
100                                    }
101                                    if (oe == null) {
102                                            capitalize = true;
103                                    } else if (i != null && i.hasDefiniteArticle()) {
104                                            capitalize = true;
105                                    } else {
106                                            capitalize = false;
107                                    }
108                            }
109                            if (capitalize && text.length() > 0) {
110                                    String f = text.substring(0, 1);
111                                    t = f.toUpperCase() + text.substring(1);
112                            } else {
113                                    t = text;
114                            }
115                    } else {
116                            boolean capitalize = false;
117                            if (preceding == null || preceding.matches("[.?!]")) {
118                                    capitalize = true;
119                            }
120                            if (capitalize && text.length() > 0) {
121                                    String f = text.substring(0, 1);
122                                    t = f.toUpperCase() + text.substring(1);
123                            } else {
124                                    t = text;
125                            }
126                            
127                            if (following != null && t.matches("(A|a)n?")) {
128                                    if (ACEUtils.useIndefiniteArticleAn(following)) {
129                                            t = t.substring(0, 1) + "n";
130                                    } else {
131                                            t = t.substring(0, 1);
132                                    }
133                            }
134                    }
135                    return t;
136            }
137    
138    }