1   /*
2    *  MutableWordImpl.java
3    *
4    *  Copyright (c) 1998-2003, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Kalina Bontcheva, 21/February/2003
12   *
13   *  $Id: MutableWordImpl.java,v 1.4 2003/02/26 16:24:53 kalina Exp $
14   */
15  
16  package gate.lexicon;
17  
18  import java.util.*;
19  import java.io.Serializable;
20  import gate.util.Out;
21  
22  public class MutableWordImpl implements MutableWord, Serializable {
23    private String lemma = "";
24    private List senseList = new ArrayList();
25    static final long serialVersionUID = -2448794632943663270L;
26  
27  
28    public MutableWordImpl(String newLemma) {
29      this.lemma = newLemma;
30    }
31  
32    public LexKBWordSense addSense(MutableLexKBSynset wordSynset) {
33      return addSense(senseList.size(), wordSynset);
34    }
35  
36    public LexKBWordSense addSense(int index, MutableLexKBSynset wordSynset) {
37      MutableLexKBSynset newSynset = wordSynset;
38      if (newSynset == null)
39        throw new RuntimeException("A valid synset must be provided!");
40      MutableLexKBWordSense newSense = new MutableLexKBWordSenseImpl(
41          this, newSynset, index, newSynset.getWordSenses().size());
42      senseList.add(index, newSense);
43      newSynset.addWordSense(newSense);
44      return newSense;
45    }
46  
47    public List getWordSenses() {
48      return senseList;
49    }
50  
51    public String getLemma() {
52      return lemma;
53    }
54  
55    public int getSenseCount() {
56      return senseList.size();
57    }
58  
59    public void removeSenses() {
60      //doing back to front, so no need to shift the numbers of the other senses,
61      //as they are all getting removed anyway
62      for (int i= senseList.size()-1; i >=0; i--) {
63        LexKBWordSense theSense = (LexKBWordSense) senseList.get(i);
64        removeSense(theSense);
65      }
66    }
67  
68    public void removeSense(LexKBWordSense theSense) {
69      if (! (theSense instanceof MutableLexKBWordSense)) {
70        Out.prln("Could not remove sense: " + theSense
71                 + "because it is not mutable");
72        return;
73      }
74      LexKBSynset theSynset = ((MutableLexKBWordSense)theSense).getSynset();
75      if (! (theSynset instanceof MutableLexKBSynset)) {
76        Out.prln("Could not remove sense: " + theSense
77                 + "because it is not mutable");
78        return;
79      }
80      ((MutableLexKBSynset)theSynset).removeSense(theSense);
81  
82      for (int i=theSense.getSenseNumber() + 1; i < senseList.size(); i++) {
83        LexKBWordSense nextSense = (LexKBWordSense) senseList.get(i);
84        if (! (nextSense instanceof MutableLexKBWordSense))
85          continue;
86        //decrease the sense number by 1
87        ((MutableLexKBWordSense)nextSense).setSenseNumber(
88            nextSense.getSenseNumber()-1);
89      }
90      senseList.remove(theSense);
91    }
92  }
93