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 nouns.
030     * 
031     * @author Tobias Kuhn
032     */
033    public class NounChanger implements LexiconChanger {
034            
035            public String getDescription() {
036                    return "Every noun represents a certain type of things. " +
037                            "For example, the noun \"city\" stands for all things that are cities.";
038            }
039            
040            public List<LexiconDetail> getDetails(OntologyElement el) {
041                    NounConcept concept = (NounConcept) el;
042                    List<LexiconDetail> l = new ArrayList<LexiconDetail>();
043                    l.add(new LexiconDetail(
044                                    "singular",
045                                    "examples: woman, city, process",
046                                    concept.getWord(0)
047                            ));
048                    l.add(new LexiconDetail(
049                                    "plural",
050                                    "examples: women, cities, processes",
051                                    concept.getWord(1)
052                            ));
053                    return l;
054            }
055            
056            public void save(OntologyElement el, int wordNumber, List<Object> newValues, Ontology ontology)
057                            throws InvalidWordException {
058                    NounConcept concept = (NounConcept) el;
059                    
060                    String singular = ACEOWLLexicon.normalize((String) newValues.get(0));
061                    String plural = ACEOWLLexicon.normalize((String) newValues.get(1));
062                    String singularP = LanguageUtils.getPrettyPrinted(singular);
063                    String pluralP = LanguageUtils.getPrettyPrinted(plural);
064                    
065                    if (singular.equals(plural)) {
066                            throw new InvalidWordException("Singular and plural form have to be distinct.");
067                    }
068                    if (singular.equals("")) {
069                            throw new InvalidWordException("No singular form defined: Please specify the " +
070                                    "singular form.");
071                    }
072                    if (!ACEOWLLexicon.isValidWordOrEmpty(singular)) {
073                            throw new InvalidWordException("Invalid character: Only a-z, A-Z, 0-9, -, and " +
074                                    "spaces are allowed, and the first character must be one of a-z A-Z.");
075                    }
076                    if (FunctionWords.isFunctionWord(singular)) {
077                            throw new InvalidWordException("'" + singularP + "' is a predefined word and cannot " +
078                                    "be used here.");
079                    }
080                    OntologyElement oe = ontology.getElement(singular);
081                    if (oe != null && oe != concept) {
082                            throw new InvalidWordException("The word '" + singularP + "' is already used. " +
083                                    "Please use a different one.");
084                    }
085                    if (plural.equals("")) {
086                            throw new InvalidWordException("No plural form defined: Please specify the plural " +
087                                    "form.");
088                    }
089                    if (!ACEOWLLexicon.isValidWordOrEmpty(plural)) {
090                            throw new InvalidWordException("Invalid character: Only a-z, A-Z, 0-9, -, and " +
091                                    "spaces are allowed, and the first character must be one of a-z A-Z.");
092                    }
093                    if (FunctionWords.isFunctionWord(plural)) {
094                            throw new InvalidWordException("'" + pluralP + "' is a predefined word and cannot " +
095                                    "be used here.");
096                    }
097                    oe = ontology.getElement(plural);
098                    if (oe != null && oe != concept) {
099                            throw new InvalidWordException("The word '" + pluralP + "' is already used. Please " +
100                                    "use a different one.");
101                    }
102                    ontology.change(concept, singular + ";" + plural);
103            }
104    
105    }