001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008-2009, Attempto Group, University of Zurich (see http://attempto.ifi.uzh.ch).
003    //
004    // The Attempto Java Packages is free software: you can redistribute it and/or modify it under the
005    // terms of the GNU Lesser General Public License as published by the Free Software Foundation,
006    // either version 3 of the License, or (at your option) any later version.
007    //
008    // The Attempto Java Packages is distributed in the hope that it will be useful, but WITHOUT ANY
009    // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
010    // PURPOSE. See the GNU Lesser General Public License for more details.
011    //
012    // You should have received a copy of the GNU Lesser General Public License along with the Attempto
013    // Java Packages. If not, see http://www.gnu.org/licenses/.
014    
015    package ch.uzh.ifi.attempto.acewiki.core.ontology;
016    
017    import ch.uzh.ifi.attempto.chartparser.Terminal;
018    import ch.uzh.ifi.attempto.preditor.text.TextElement;
019    
020    /**
021     * This class represents a text element that links to an ontology element. The text of
022     * the text elements corresponds to one of the word forms of the ontology element.
023     * 
024     * @author Tobias Kuhn
025     */
026    public class OntologyTextElement extends TextElement {
027            
028            private OntologyElement ontologyElement;
029            private int wordNumber;
030            private String category;
031            
032            /**
033             * Creates a new text element for the given word form (by word form id) of the given
034             * ontology element.
035             * 
036             * @param el The ontology element.
037             * @param wordNumber The word form id.
038             * @return The newly created text element.
039             */
040            public static OntologyTextElement createTextElement(OntologyElement el, int wordNumber) {
041                    if (el instanceof NounConcept) {
042                            if (wordNumber == 0) {
043                                    return new OntologyTextElement(el, 0, "NOUN");
044                            } else if (wordNumber == 1) {
045                                    return new OntologyTextElement(el, 1, "NOUNPL");
046                            }
047                    } else if (el instanceof Individual) {
048                            return new OntologyTextElement(el, wordNumber, "PROPERNAME");
049                    } else if (el instanceof VerbRole) {
050                            if (wordNumber == 0) {
051                                    return new OntologyTextElement(el, 0, "VERB");
052                            } else if (wordNumber == 1) {
053                                    return new OntologyTextElement(el, 1, "VERBINF");
054                            } else if (wordNumber == 2) {
055                                    return new OntologyTextElement(el, 2, "PVERB");
056                            }
057                    } else if (el instanceof OfRole) {
058                            if (wordNumber == 0) return new OntologyTextElement(el, 0, "NOUNOF");
059                    } else if (el instanceof TrAdjRole) {
060                            if (wordNumber == 0) return new OntologyTextElement(el, 0, "TRADJ");
061                    }
062                    return null;
063            }
064            
065            /**
066             * Creates a new text element for the default word form of the given ontology element.
067             * 
068             * @param el The ontology element.
069             * @return The newly created text element.
070             */
071            public static OntologyTextElement createTextElement(OntologyElement el) {
072                    return createTextElement(el, 0);
073            }
074            
075            /**
076             * Creates a new ontology text element.
077             * 
078             * @param ontologyElement The ontology element.
079             * @param wordNumber The word number.
080             * @param category The category name.
081             */
082            public OntologyTextElement(OntologyElement ontologyElement, int wordNumber, String category) {
083                    if (ontologyElement.getWord(wordNumber) == null) {
084                            throw new RuntimeException(ontologyElement + " has no word number " + wordNumber);
085                    }
086                    this.ontologyElement = ontologyElement;
087                    this.wordNumber = wordNumber;
088                    this.category = category;
089            }
090    
091            /**
092             * Returns the text of this text element in its plain form where underscores are not
093             * replaces by blanks.
094             * 
095             * @return The plain text.
096             */
097            public String getUnderscoredText() {
098                    return super.getText();
099            }
100            
101            public String getText() {
102                    String t = super.getText();
103                    if (t != null) t = t.replace("_", " ");
104                    return t;
105            }
106            
107            public String getOriginalText() {
108                    return ontologyElement.getWord(wordNumber);
109            }
110    
111            /**
112             * Returns the id of the word form of the ontology element that is used for this
113             * text element.
114             * 
115             * @return The word form id.
116             */
117            public int getWordNumber() {
118                    return wordNumber;
119            }
120    
121            public Terminal[] getCategories() {
122                    Terminal cat = new Terminal(category);
123                    cat.setFeature("text", ontologyElement.getWord(wordNumber));
124                    if (category.equals("PROPERNAME")) {
125                            if (((Individual) ontologyElement).hasDefiniteArticle(wordNumber)) {
126                                    cat.setFeature("capitalize", "true");
127                            } else {
128                                    cat.setFeature("capitalize", "false");
129                            }
130                    }
131                    return new Terminal[] {cat};
132            }
133            
134            /**
135             * Returns the ontology element to which this text element is linked.
136             * 
137             * @return The ontology element.
138             */
139            public OntologyElement getOntologyElement() {
140                    return ontologyElement;
141            }
142            
143            public void include(TextElement textElement) {
144                    if (!equals(textElement)) {
145                            throw new RuntimeException("Only equal text elements can be included");
146                    }
147            }
148            
149            public boolean equals(Object obj) {
150                    if (obj instanceof OntologyTextElement) {
151                            OntologyTextElement other = ((OntologyTextElement) obj);
152                            return (ontologyElement == other.ontologyElement && wordNumber == other.wordNumber && category.equals(other.category));
153                    }
154                    return false;
155            }
156    
157    }