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.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; 028 private Terminal category; 029 private boolean capitalize; 030 031 /** 032 * Creates a new basic text element. 033 * 034 * @param text The text. 035 * @param category The category. 036 * @param capitalize true if the word should be capitalized in sentence-initial position. 037 */ 038 public BasicTextElement(String text, Terminal category, boolean capitalize) { 039 this.text = text; 040 this.originalText = text; 041 this.category = category; 042 this.capitalize = capitalize; 043 } 044 045 /** 046 * Creates a new basic text element. 047 * 048 * @param text The text. 049 * @param categoryName The name of the category. 050 * @param capitalize true if the word should be capitalized in sentence-initial position. 051 */ 052 public BasicTextElement(String text, String categoryName, boolean capitalize) { 053 this(text, new Terminal(categoryName), capitalize); 054 } 055 056 /** 057 * Creates a new basic text element. It is capitalized in sentence-initial position. 058 * 059 * @param text The text. 060 * @param category The category. 061 */ 062 public BasicTextElement(String text, Terminal category) { 063 this(text, category, true); 064 } 065 066 /** 067 * Creates a new basic text element. It is capitalized in sentence-initial position. 068 * 069 * @param text The text. 070 * @param categoryName The name of the category. 071 */ 072 public BasicTextElement(String text, String categoryName) { 073 this(text, new Terminal(categoryName), true); 074 } 075 076 /** 077 * Creates a new basic text element containing a number. The text element is capitalized in 078 * sentence-initial position. 079 * 080 * @param number The number. 081 * @param category The category. 082 */ 083 public BasicTextElement(int number, Terminal category) { 084 this(number + "", category, true); 085 } 086 087 /** 088 * Creates a new basic text element containing a number. The text element is capitalized in 089 * sentence-initial position. 090 * 091 * @param number The number. 092 * @param categoryName The name of the category. 093 */ 094 public BasicTextElement(int number, String categoryName) { 095 this(number + "", new Terminal(categoryName), true); 096 } 097 098 /** 099 * Creates a new basic text element where the text is the name of the category. 100 * 101 * @param category The category. 102 * @param capitalize true if the word should be capitalized in sentence-initial position. 103 */ 104 public BasicTextElement(Terminal category, boolean capitalize) { 105 this(category.getName(), category, capitalize); 106 } 107 108 /** 109 * Creates a new basic text element where the text is the name of the category. 110 * 111 * @param categoryName The name of the category. 112 * @param capitalize true if the word should be capitalized in sentence-initial position. 113 */ 114 public BasicTextElement(String categoryName, boolean capitalize) { 115 this(categoryName, new Terminal(categoryName), capitalize); 116 } 117 118 /** 119 * Creates a new basic text element where the text is the name of the category. 120 * The text element is capitalized in sentence-initial position. 121 * 122 * @param category The category. 123 */ 124 public BasicTextElement(Terminal category) { 125 this(category.getName(), category, true); 126 } 127 128 /** 129 * Creates a new basic text element where the text is the name of the category. 130 * The text element is capitalized in sentence-initial position. 131 * 132 * @param categoryName The name of the category. 133 */ 134 public BasicTextElement(String categoryName) { 135 this(categoryName, new Terminal(categoryName), true); 136 } 137 138 /** 139 * Creates a new basic text element containing a number which is also the name of the category. 140 * The text element is capitalized in sentence-initial position. 141 * 142 * @param number The number. 143 */ 144 public BasicTextElement(int number) { 145 this(number + "", new Terminal(number + ""), true); 146 } 147 148 public Terminal getCategory() { 149 return category; 150 } 151 152 public String getText() { 153 return text; 154 } 155 156 public void checkNeighborTextElements(TextElement precedingTextElement, TextElement nextTextElement) { 157 if ((precedingTextElement == null || precedingTextElement.getText().matches("(\\.|\\?|\\!)")) && capitalize) { 158 String f = originalText.substring(0, 1); 159 text = f.toUpperCase() + originalText.substring(1); 160 } else { 161 text = originalText; 162 } 163 164 if (nextTextElement != null && text.matches("(A|a)n?")) { 165 String nt = nextTextElement.getText().toLowerCase(); 166 boolean an = false; 167 if (nt.matches("[aeiou].*")) an = true; 168 if (nt.matches("[fhlmnrsx]")) an = true; 169 if (nt.matches("[fhlmnrsx]-.*")) an = true; 170 if (nt.equals("u")) an = false; 171 if (nt.matches("u-.*")) an = false; 172 if (nt.matches("u[rtn]i.*")) an = false; 173 if (nt.matches("use.*")) an = false; 174 if (nt.matches("uk.*")) an = false; 175 176 if (an) { 177 text = text.substring(0, 1) + "n"; 178 } else { 179 text = text.substring(0, 1); 180 } 181 } 182 } 183 184 public boolean equals(Object obj) { 185 if (obj instanceof BasicTextElement) { 186 BasicTextElement other = (BasicTextElement) obj; 187 return (this.text.equals(other.text) && this.category.equals(other.category)); 188 } 189 return false; 190 } 191 192 }