001 package gate.creole.annic.apache.lucene.index;
002
003 /**
004 A Term represents a word from text. This is the unit of search. It is
005 composed of two elements, the text of the word, as a string, and the name of
006 the field that the text occured in, an interned string.
007
008 Note that terms may represent more than words from text fields, but also
009 things like dates, email addresses, urls, etc. */
010
011 public final class Term implements Comparable, java.io.Serializable {
012 String field;
013 String text;
014 //Niraj
015 String type;
016 // End
017
018
019 /** Constructs a Term with the given field and text. */
020 public Term(String fld, String txt) {
021 this(fld, txt, true);
022 }
023
024 // Niraj
025 public Term(String fld, String txt, String type/*, int pos*/) {
026 this(fld, txt, type, /*pos,*/ true);
027 }
028
029
030 Term(String fld, String txt, String type, /*int pos,*/ boolean intern) {
031 field = intern ? fld.intern() : fld;
032 text = txt;
033 this.type = type;
034 }
035
036 // End
037
038 Term(String fld, String txt, boolean intern) {
039 field = intern ? fld.intern() : fld; // field names are interned
040 text = txt; // unless already known to be
041 }
042
043
044 /** Returns the field of this term, an interned string. The field indicates
045 the part of a document which this term came from. */
046 public final String field() { return field; }
047
048 //Niraj
049 public final String type() { return type; }
050 /* public final int position() { return position; }*/
051 //End
052
053 /** Returns the text of this term. In the case of words, this is simply the
054 text of the word. In the case of dates and other types, this is an
055 encoding of the object as a string. */
056 public final String text() { return text; }
057
058 /** Compares two terms, returning true iff they have the same
059 field and text. */
060 public final boolean equals(Object o) {
061 if (o == null)
062 return false;
063 Term other = (Term)o;
064 if (type != null) {
065 // Niraj
066 //return field == other.field && text.equals(other.text);
067 boolean ret = (field.equals(other.field)) &&
068 (text.equals(other.text)/* || text.equals("*") || other.text.equals("*")*/) &&
069 (type.equals(other.type)/* || type.equals("*") || other.type.equals("*")*/);/* &&
070 position == other.position;*/
071 return ret;
072 // End
073 }
074 else {
075 boolean ret = (field.equals(other.field)) &&
076 (text.equals(other.text)/* || text.equals("*") || other.text.equals("*")*/);
077 return ret;
078 }
079 }
080
081 /** Combines the hashCode() of the field and the text. */
082 public final int hashCode() {
083 if(type != null) {
084 //Niraj
085 //return field.hashCode() + text.hashCode();
086 return field.hashCode() + text.hashCode() + type.hashCode();
087 //End
088 } else {
089 return field.hashCode() + text.hashCode();
090 }
091
092 }
093
094 public int compareTo(Object other) {
095 return compareTo((Term)other);
096 }
097
098 /** Compares two terms, returning an integer which is less than zero iff this
099 term belongs after the argument, equal zero iff this term is equal to the
100 argument, and greater than zero iff this term belongs after the argument.
101
102 The ordering of terms is first by field, then by text.*/
103 public final int compareTo(Term other) {
104 if (field == other.field) { // fields are interned
105 int rank = text.compareTo(other.text);
106 if (rank == 0) {
107 rank = type.compareTo(other.type);
108 /*if(rank == 0) {
109 if(position > other.position)
110 return 1;
111 else if(position == other.position)
112 return 0;
113 else
114 return -1;
115 }*/
116 return rank;
117 }
118 else {
119 return rank;
120 }
121 } else {
122 return field.compareTo(other.field);
123 }
124 }
125
126
127 public final int indexCompareTo(Term other) {
128 if (field.equals(other.field)) { // fields are interned
129 int rank = text.compareTo(other.text);
130 // we need to check for the star wild card characters
131 if (rank == 0 && type != null && other.type != null) {
132 rank = type.compareTo(other.type);
133 if(rank == 0 /*|| type.equals("*") || other.type.equals("*")*/) {
134 return 0;
135 //System.out.println("position "+position+" : "+other.position);
136 //return new Integer(position).compareTo(new Integer(other.position));
137 }
138 return rank;
139 }
140 else {
141 return rank;
142 }
143 } else {
144 int rank = field.compareTo(other.field);
145 return rank;
146 }
147 }
148
149 private boolean isWildcharMatches(String text, String other) {
150 /*if(text.endsWith("*")) {
151 String text1 = text.substring(0, text.length()-1);
152 if(other.startsWith(text1))
153 return true;
154 else
155 return false;
156 }
157 return false;*/
158 java.util.regex.Pattern p = java.util.regex.Pattern.compile(text);
159 java.util.regex.Matcher m = p.matcher(other);
160 return m.matches();
161 }
162
163 /** Resets the field and text of a Term. */
164 final void set(String fld, String txt) {
165 field = fld;
166 text = txt;
167 }
168
169 // Niraj
170 final void set(String fld, String text, String type/*, int pos*/) {
171 field = fld;
172 this.text = text;
173 this.type = type;
174 /*position = pos;*/
175 }
176 // End
177
178 public final String toString() {
179 if(type == null)
180 return field + ":" + text;
181 //Niraj
182 return field + ":" + text + ":" + type;/* + ":" + position;*/
183 //End
184 }
185
186 private void readObject(java.io.ObjectInputStream in)
187 throws java.io.IOException, ClassNotFoundException
188 {
189 in.defaultReadObject();
190 field = field.intern();
191 }
192 }
|