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.InvalidWordException; 021 import ch.uzh.ifi.attempto.acewiki.core.LanguageUtils; 022 import ch.uzh.ifi.attempto.acewiki.core.LexiconChanger; 023 import ch.uzh.ifi.attempto.acewiki.core.LexiconDetail; 024 import ch.uzh.ifi.attempto.acewiki.core.Ontology; 025 import ch.uzh.ifi.attempto.acewiki.core.OntologyElement; 026 import ch.uzh.ifi.attempto.ape.FunctionWords; 027 028 /** 029 * This class is used to modify or create proper names. 030 * 031 * @author Tobias Kuhn 032 */ 033 public class ProperNameChanger implements LexiconChanger { 034 035 public String getDescription() { 036 return "Every proper name represents a certain individual. " + 037 "The country \"Switzerland\" and the person \"Bob Dylan\" are typical " + 038 "examples. Proper names can have an abbreviation that has the same meaning " + 039 "as the longer proper name."; 040 } 041 042 public List<LexiconDetail> getDetails(OntologyElement el) { 043 ProperNameIndividual ind = (ProperNameIndividual) el; 044 List<LexiconDetail> l = new ArrayList<LexiconDetail>(); 045 l.add(new LexiconDetail( 046 "proper name", 047 "examples: Switzerland, Bob Dylan, Nile, United Nations", 048 ind.getWord(1) 049 )); 050 l.add(new LexiconDetail( 051 "... used with \"the\"", 052 "examples: the Nile, the United Nations", 053 ind.hasDefiniteArticle(0) 054 )); 055 l.add(new LexiconDetail( 056 "abbreviation", 057 "example: HTML, UN", 058 ind.getAbbreviation(), 059 false 060 )); 061 l.add(new LexiconDetail( 062 "... used with \"the\"", 063 "example: the UN", 064 ind.hasDefiniteArticle(2) 065 )); 066 return l; 067 } 068 069 public void save(OntologyElement el, int wordNumber, List<Object> newValues, Ontology ontology) 070 throws InvalidWordException { 071 ProperNameIndividual ind = (ProperNameIndividual) el; 072 073 String name = ACEOWLLexicon.normalize((String) newValues.get(0)); 074 String abbrev = ACEOWLLexicon.normalize((String) newValues.get(2)); 075 boolean nameDefArt = (Boolean) newValues.get(1); 076 boolean abbrevDefArt = (Boolean) newValues.get(3); 077 078 if (name.toLowerCase().startsWith("the_")) { 079 name = name.substring(4); 080 nameDefArt = true; 081 } 082 if (abbrev.toLowerCase().startsWith("the_")) { 083 abbrev = abbrev.substring(4); 084 abbrevDefArt = true; 085 } 086 String nameP = LanguageUtils.getPrettyPrinted(name); 087 String abbrevP = LanguageUtils.getPrettyPrinted(abbrev); 088 089 if (name.equals("")) { 090 throw new InvalidWordException("No proper name defined: Please specify a name."); 091 } 092 if (!ACEOWLLexicon.isValidWordOrEmpty(name) || !ACEOWLLexicon.isValidWordOrEmpty(abbrev)) { 093 throw new InvalidWordException("Invalid character: Only a-z, A-Z, 0-9, -, and " + 094 "spaces are allowed, and the first character must be one of a-z A-Z."); 095 } 096 if (FunctionWords.isFunctionWord(name)) { 097 throw new InvalidWordException("'" + nameP + "' is a predefined word and cannot be " + 098 "used here."); 099 } 100 if (FunctionWords.isFunctionWord(abbrev)) { 101 throw new InvalidWordException("'" + abbrevP + "' is a predefined word and cannot " + 102 "be used here."); 103 } 104 if (abbrev.length() >= name.length()) { 105 throw new InvalidWordException("The abbreviation has to be shorter than the full " + 106 "proper name."); 107 } 108 OntologyElement oe = ontology.getElement(name); 109 if (oe != null && oe != ind) { 110 throw new InvalidWordException("The word '" + nameP + "' is already used. Please " + 111 "use a different one."); 112 } 113 oe = ontology.getElement(abbrev); 114 if (oe != null && oe != ind) { 115 throw new InvalidWordException("The word '" + abbrevP + "' is already used. Please " + 116 "use a different one."); 117 } 118 String word = name; 119 if (nameDefArt) { 120 word = "the " + name; 121 } 122 String abbrevWord = abbrev; 123 if (!abbrev.equals("") && abbrevDefArt) { 124 abbrevWord = "the " + abbrev; 125 } 126 ontology.change(ind, word + ";" + name + ";" + abbrevWord + ";" + abbrev); 127 } 128 129 }