001    // This file is part of the Attempto Java Packages.
002    // Copyright 2008, 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.preditor.text;
016    
017    import ch.uzh.ifi.attempto.chartparser.Terminal;
018    
019    /**
020     * This is a general purpose implementation of a text element.
021     * 
022     * @author Tobias Kuhn
023     */
024    public class BasicTextElement implements TextElement {
025            
026            private final String originalText;
027            private String text, category;
028            private boolean capitalize;
029            
030            /**
031             * Creates a new basic text element.
032             * 
033             * @param text The text.
034             * @param category The category.
035             * @param capitalize true if the word should be capitalized in sentence-initial position.
036             */
037            public BasicTextElement(String text, String category, boolean capitalize) {
038                    this.text = text;
039                    this.originalText = text;
040                    this.category = category;
041                    this.capitalize = capitalize;
042            }
043            
044            /**
045             * Creates a new basic text element. It is capitalized in sentence-initial position.
046             * 
047             * @param text The text.
048             * @param category The category.
049             */
050            public BasicTextElement(String text, String category) {
051                    this(text, category, true);
052            }
053            
054            /**
055             * Creates a new basic text element containing a number. The text element is capitalized in
056             * sentence-initial position.
057             * 
058             * @param number The number.
059             * @param category The category.
060             */
061            public BasicTextElement(int number, String category) {
062                    this(number + "", category, true);
063            }
064            
065            /**
066             * Creates a new basic text element where the text is the name of the category.
067             * 
068             * @param category The category which is the same as the text.
069             * @param capitalize true if the word should be capitalized in sentence-initial position.
070             */
071            public BasicTextElement(String category, boolean capitalize) {
072                    this(category, category, capitalize);
073            }
074            
075            /**
076             * Creates a new basic text element where the text is the name of the category.
077             * The text element is capitalized in sentence-initial position.
078             * 
079             * @param category The category which is the same as the text.
080             */
081            public BasicTextElement(String category) {
082                    this(category, category, true);
083            }
084            
085            /**
086             * Creates a new basic text element containing a number which is also the name of the category.
087             * The text element is capitalized in sentence-initial position.
088             * 
089             * @param number The number.
090             */
091            public BasicTextElement(int number) {
092                    this(number + "", number + "", true);
093            }
094            
095            /**
096             * Creates a new basic text element on the basis of a terminal category object. The text is the
097             * same as the category name.
098             * 
099             * @param terminal The terminal category object.
100             * @param capitalize true if the word should be capitalized in sentence-initial position.
101             */
102            public BasicTextElement(Terminal terminal, boolean capitalize) {
103                    this(terminal.getName(), terminal.getName(), capitalize);
104            }
105            
106            /**
107             * Creates a new basic text element on the basis of a terminal category object. The text is the
108             * same as the category name. The text element is capitalized in sentence-initial position.
109             * 
110             * @param terminal The terminal category object.
111             */
112            public BasicTextElement(Terminal terminal) {
113                    this(terminal.getName(), terminal.getName(), true);
114            }
115            
116            public String getCategory() {
117                    return category;
118            }
119            
120            public String getText() {
121                    return text;
122            }
123            
124            public void checkNeighborTextElements(TextElement precedingTextElement, TextElement nextTextElement) {
125                    if ((precedingTextElement == null || precedingTextElement.getText().matches("(\\.|\\?|\\!)")) && capitalize) {
126                            String f = originalText.substring(0, 1);
127                            text = f.toUpperCase() + originalText.substring(1);
128                    } else {
129                            text = originalText;
130                    }
131                    
132                    if (nextTextElement != null && text.matches("(A|a)n?")) {
133                            String nt = nextTextElement.getText().toLowerCase();
134                            boolean an = false;
135                            if (nt.matches("[aeiou].*")) an = true;
136                            if (nt.matches("[fhlmnrsx]")) an = true;
137                            if (nt.matches("[fhlmnrsx]-.*")) an = true;
138                            if (nt.equals("u")) an = false;
139                            if (nt.matches("u-.*")) an = false;
140                            if (nt.matches("u[rtn]i.*")) an = false;
141                            if (nt.matches("use.*")) an = false;
142                            if (nt.matches("uk.*")) an = false;
143                            
144                            if (an) {
145                                    text = text.substring(0, 1) + "n";
146                            } else {
147                                    text = text.substring(0, 1);
148                            }
149                    }
150            }
151            
152            public boolean equals(Object obj) {
153                    if (obj instanceof BasicTextElement) {
154                            BasicTextElement other = (BasicTextElement) obj;
155                            return (this.text.equals(other.text) && this.category.equals(other.category));
156                    }
157                    return false;
158            }
159    
160    }