View Javadoc
1   /*
2    * Copyright 2012-2025 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.dict.kuromoji;
17  
18  import java.util.Objects;
19  
20  import org.codelibs.fess.dict.DictionaryItem;
21  
22  /**
23   * An item in a Kuromoji dictionary.
24   */
25  public class KuromojiItem extends DictionaryItem {
26      private final String token;
27  
28      private final String segmentation;
29  
30      private final String reading;
31  
32      private final String pos;
33  
34      private String newToken;
35  
36      private String newSegmentation;
37  
38      private String newReading;
39  
40      private String newPos;
41  
42      /**
43       * Constructs a new Kuromoji item.
44       *
45       * @param id The ID of the item.
46       * @param token The token.
47       * @param segmentation The segmentation.
48       * @param reading The reading.
49       * @param pos The part of speech.
50       */
51      public KuromojiItem(final long id, final String token, final String segmentation, final String reading, final String pos) {
52          this.id = id;
53          this.token = token;
54          this.segmentation = segmentation;
55          this.reading = reading;
56          this.pos = pos;
57  
58          if (id == 0) {
59              // create
60              newToken = token;
61              newSegmentation = segmentation;
62              newReading = reading;
63              newPos = pos;
64          }
65      }
66  
67      /**
68       * Returns the new token.
69       *
70       * @return The new token.
71       */
72      public String getNewToken() {
73          return newToken;
74      }
75  
76      /**
77       * Sets the new token.
78       *
79       * @param newToken The new token.
80       */
81      public void setNewToken(final String newToken) {
82          this.newToken = newToken;
83      }
84  
85      /**
86       * Returns the new segmentation.
87       *
88       * @return The new segmentation.
89       */
90      public String getNewSegmentation() {
91          return newSegmentation;
92      }
93  
94      /**
95       * Sets the new segmentation.
96       *
97       * @param newSegmentation The new segmentation.
98       */
99      public void setNewSegmentation(final String newSegmentation) {
100         this.newSegmentation = newSegmentation;
101     }
102 
103     /**
104      * Returns the new reading.
105      *
106      * @return The new reading.
107      */
108     public String getNewReading() {
109         return newReading;
110     }
111 
112     /**
113      * Sets the new reading.
114      *
115      * @param newReading The new reading.
116      */
117     public void setNewReading(final String newReading) {
118         this.newReading = newReading;
119     }
120 
121     /**
122      * Returns the new part of speech.
123      *
124      * @return The new part of speech.
125      */
126     public String getNewPos() {
127         return newPos;
128     }
129 
130     /**
131      * Sets the new part of speech.
132      *
133      * @param newPos The new part of speech.
134      */
135     public void setNewPos(final String newPos) {
136         this.newPos = newPos;
137     }
138 
139     /**
140      * Returns the token.
141      *
142      * @return The token.
143      */
144     public String getToken() {
145         return token;
146     }
147 
148     /**
149      * Returns the segmentation.
150      *
151      * @return The segmentation.
152      */
153     public String getSegmentation() {
154         return segmentation;
155     }
156 
157     /**
158      * Returns the reading.
159      *
160      * @return The reading.
161      */
162     public String getReading() {
163         return reading;
164     }
165 
166     /**
167      * Returns the part of speech.
168      *
169      * @return The part of speech.
170      */
171     public String getPos() {
172         return pos;
173     }
174 
175     /**
176      * Returns true if the item has been updated.
177      *
178      * @return True if the item has been updated.
179      */
180     public boolean isUpdated() {
181         return newToken != null;
182     }
183 
184     /**
185      * Returns true if the item has been deleted.
186      *
187      * @return True if the item has been deleted.
188      */
189     public boolean isDeleted() {
190         return isUpdated() && newToken.length() == 0;
191     }
192 
193     @Override
194     public int hashCode() {
195         return Objects.hash(pos, reading, segmentation, token);
196     }
197 
198     @Override
199     public boolean equals(final Object obj) {
200         if (this == obj) {
201             return true;
202         }
203         if (obj == null || getClass() != obj.getClass()) {
204             return false;
205         }
206         final KuromojiItem other = (KuromojiItem) obj;
207         if (!Objects.equals(pos, other.pos) || !Objects.equals(reading, other.reading) || !Objects.equals(segmentation, other.segmentation)
208                 || !Objects.equals(token, other.token)) {
209             return false;
210         }
211         return true;
212     }
213 
214     @Override
215     public String toString() {
216         return "KuromojiItem [token=" + token + ", segmentation=" + segmentation + ", reading=" + reading + ", pos=" + pos + ", newToken="
217                 + newToken + ", newSegmentation=" + newSegmentation + ", newReading=" + newReading + ", newPos=" + newPos + "]";
218     }
219 
220     /**
221      * Returns the item as a line string.
222      *
223      * @return The item as a line string.
224      */
225     public String toLineString() {
226         final StringBuilder buf = new StringBuilder(100);
227         if (isUpdated()) {
228             buf.append(quoteEscape(newToken));
229             buf.append(',');
230             buf.append(quoteEscape(newSegmentation));
231             buf.append(',');
232             buf.append(quoteEscape(newReading));
233             buf.append(',');
234             buf.append(quoteEscape(newPos));
235         } else {
236             buf.append(quoteEscape(token));
237             buf.append(',');
238             buf.append(quoteEscape(segmentation));
239             buf.append(',');
240             buf.append(quoteEscape(reading));
241             buf.append(',');
242             buf.append(quoteEscape(pos));
243         }
244         return buf.toString();
245     }
246 
247     private static String quoteEscape(final String original) {
248         String result = original;
249 
250         if (result.indexOf('\"') >= 0) {
251             result = result.replace("\"", "\"\"");
252         }
253         if (result.indexOf(',') >= 0) {
254             return "\"" + result + "\"";
255         }
256         return result;
257     }
258 }