    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.ape;
    016
    017    import java.util.ArrayList;
    018    import java.util.Collection;
    019    import java.util.List;
    020
    021    /**
    022     * This class represents a lexicon which consists of a set of lexicon entries.
    023     *
    024     * @author Tobias Kuhn
    025     */
    026    public class Lexicon {
    027
    028            private ArrayList<LexiconEntry> entries = new ArrayList<LexiconEntry>();
    029
    030
    031            /**
    032             * Creates an empty lexicon.
    033             */
    034            public Lexicon() {
    035            }
    036
    037            /**
    038             * Adds a lexicon entry to this lexicon.
    039             *
    040             * @param entry The lexicon entry to be added.
    041             */
    042            public void addEntry(LexiconEntry entry) {
    043                    if (!entries.contains(entry)) {
    044                            entries.add(entry);
    045                    }
    046            }
    047
    048            /**
    049             * Adds a collection of lexicon entries to this lexicon.
    050             *
    051             * @param entries The lexicon entries to be added.
    052             */
    053            public void addEntries(Collection<LexiconEntry> entries) {
    054                    for (LexiconEntry entry : entries) {
    055                            addEntry(entry);
    056                    }
    057            }
    058
    059            /**
    060             * Removes the lexicon entry from this lexicon.
    061             *
    062             * @param entry The lexicon entry to be removed.
    063             */
    064            public void removeEntry(LexiconEntry entry) {
    065                    entries.remove(entry);
    066            }
    067
    068            /**
    069             * Removes all entries from this lexicon.
    070             */
    071            public void removeAllEntries() {
    072                    entries.clear();
    073            }
    074
    075            /**
    076             * Returns a list of all lexicon entries that are contained in this lexicon.
    077             *
    078             * @return A list of all lexicon entries.
    079             */
    080            public List<LexiconEntry> getEntries() {
    081                    return new ArrayList<LexiconEntry>(entries);
    082            }
    083
    084            /**
    085             * Returns the lexicon as a serialized Prolog list.
    086             *
    087             * @return The lexicon as a serialized Prolog list.
    088             */
    089            public String toList() {
    090                    StringBuffer sb = new StringBuffer();
    091                    for (LexiconEntry entry : entries) {
    092                            sb.append(entry.toString());
    093                            sb.append(", ");
    094                    }
    095                    if (sb.length() > 0) {
    096                            sb.substring(0, sb.length()-2);
    097                    }
    098                    return "[" + sb.toString() + "]";
    099            }
    100
    101            /**
    102             * Returns this lexicon as a contents of an APE lexicon file, i.e.
    103             * each entry is followed by a dot ('.') and a space (' ').
    104             */
    105            @Override
    106            public String toString() {
    107                    StringBuffer sb = new StringBuffer();
    108                    for (LexiconEntry entry : entries) {
    109                            sb.append(entry.toString());
    110                            sb.append(". ");
    111                    }
    112                    return sb.toString();
    113            }
    114
    115    }