1
1
/*
2
- * Copyright (c) 2021 Works Applications Co., Ltd.
2
+ * Copyright (c) 2021-2024 Works Applications Co., Ltd.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -42,13 +42,13 @@ public class LatticeNodeImpl implements LatticeNode {
42
42
LatticeNodeImpl bestPreviousNode ;
43
43
44
44
// either Lexicon or StringsCache object
45
- Object lexicon ;
45
+ Object lexiconOrStrings ;
46
46
47
47
// Empty wordInfo for special words.
48
48
static final WordInfo UNDEFINED_WORDINFO = new WordInfo ((short ) 0 , (short ) -1 );
49
49
50
50
LatticeNodeImpl (Lexicon lexicon , long params , int wordId ) {
51
- this .lexicon = lexicon ;
51
+ this .lexiconOrStrings = lexicon ;
52
52
this .leftId = WordParameters .leftId (params );
53
53
this .rightId = WordParameters .rightId (params );
54
54
this .cost = WordParameters .cost (params );
@@ -82,7 +82,7 @@ static LatticeNodeImpl makeOov(int begin, int end, short posId, String surface,
82
82
LatticeNodeImpl node = new LatticeNodeImpl ();
83
83
node .wordId = WordId .makeOov (posId );
84
84
node .wordInfo = new WordInfo ((short ) (end - begin ), posId );
85
- node .lexicon = new StringsCache (surface , readingForm , normalizedForm , dictionaryForm );
85
+ node .lexiconOrStrings = new StringsCache (surface , readingForm , normalizedForm , dictionaryForm );
86
86
node .begin = begin ;
87
87
node .end = end ;
88
88
return node ;
@@ -110,10 +110,10 @@ public void setParameter(long params) {
110
110
}
111
111
112
112
private Lexicon lexicon () {
113
- if (lexicon instanceof Lexicon ) {
114
- return (Lexicon ) lexicon ;
115
- } else if (lexicon instanceof StringsCache ) {
116
- return ((StringsCache ) lexicon ). lexicon ;
113
+ if (lexiconOrStrings instanceof Lexicon ) {
114
+ return (Lexicon ) lexiconOrStrings ;
115
+ } else if (lexiconOrStrings instanceof StringsCache ) {
116
+ return ((StringsCache ) lexiconOrStrings ). getLexicon () ;
117
117
} else {
118
118
throw new IllegalStateException ("lexicon was null probably" );
119
119
}
@@ -191,26 +191,6 @@ public boolean isConnectedToBOS() {
191
191
return bestPreviousNode != null ;
192
192
}
193
193
194
- @ Override
195
- public String getSurface () {
196
- return strings ().getSurface (this );
197
- }
198
-
199
- @ Override
200
- public String getReading () {
201
- return strings ().getReading (this );
202
- }
203
-
204
- @ Override
205
- public String getNormalizedForm () {
206
- return strings ().getNormalizedForm (this );
207
- }
208
-
209
- @ Override
210
- public String getDictionaryForm () {
211
- return strings ().getDictionaryForm (this );
212
- }
213
-
214
194
@ Override
215
195
public String toString () {
216
196
String surface = getSurface ();
@@ -220,11 +200,12 @@ public String toString() {
220
200
cost );
221
201
}
222
202
223
- private StringsCache strings () {
224
- Object l = lexicon ;
203
+ @ Override
204
+ public StringsCache getStrings () {
205
+ Object l = lexiconOrStrings ;
225
206
if (l instanceof Lexicon ) {
226
- StringsCache c = new StringsCache ((Lexicon ) l );
227
- lexicon = c ;
207
+ StringsCache c = new StringsCache ((Lexicon ) l , wordId );
208
+ lexiconOrStrings = c ;
228
209
return c ;
229
210
} else if (l instanceof StringsCache ) {
230
211
return (StringsCache ) l ;
@@ -274,82 +255,6 @@ private void appendSplitsTo(List<LatticeNodeImpl> result, int[] splitsId) {
274
255
}
275
256
}
276
257
277
- /**
278
- * Cache to reduce the access to the lexicon. Also used to mock the lexicon for
279
- * OOV nodes.
280
- */
281
- private static final class StringsCache {
282
- private final Lexicon lexicon ;
283
- private String surface ;
284
- private String reading ;
285
- private String normalizedForm ;
286
- private String dictionaryForm ;
287
-
288
- public StringsCache (Lexicon lexicon ) {
289
- this .lexicon = lexicon ;
290
- }
291
-
292
- public StringsCache (String surface , String readingForm , String normalizedForm , String dictionaryForm ) {
293
- this .lexicon = null ;
294
- this .surface = surface ;
295
- this .reading = readingForm ;
296
- this .normalizedForm = normalizedForm ;
297
- this .dictionaryForm = dictionaryForm ;
298
- }
299
-
300
- public String getSurface (LatticeNodeImpl node ) {
301
- // benign data race pattern
302
- // https://shipilev.net/blog/2016/close-encounters-of-jmm-kind/#wishful-benign-is-resilient
303
- String s = surface ;
304
- if (s == null ) {
305
- WordInfo wi = node .getWordInfo ();
306
- int headwordPtr = wi .getHeadword ();
307
- int dic = WordId .dic (node .getWordId ());
308
- s = lexicon .string (dic , headwordPtr );
309
- surface = s ;
310
- }
311
- return s ;
312
- }
313
-
314
- public String getReading (LatticeNodeImpl node ) {
315
- String s = reading ;
316
- if (s == null ) {
317
- WordInfo wi = node .getWordInfo ();
318
- int readingPtr = wi .getReadingForm ();
319
- int dic = WordId .dic (node .getWordId ());
320
- s = lexicon .string (dic , readingPtr );
321
- reading = s ;
322
- }
323
- return s ;
324
- }
325
-
326
- public String getNormalizedForm (LatticeNodeImpl node ) {
327
- String s = normalizedForm ;
328
- if (s == null ) {
329
- WordInfo wi = node .getWordInfo ();
330
- int wordref = wi .getNormalizedForm ();
331
- int dic = WordId .refDic (wordref , WordId .dic (node .wordId ));
332
- int headwordPtr = lexicon .wordInfos (dic ).headwordPtr (WordId .word (wordref ));
333
- s = lexicon .string (dic , headwordPtr );
334
- normalizedForm = s ;
335
- }
336
- return s ;
337
- }
338
-
339
- public String getDictionaryForm (LatticeNodeImpl node ) {
340
- String s = dictionaryForm ;
341
- if (s == null ) {
342
- WordInfo wi = node .getWordInfo ();
343
- int wordref = wi .getDictionaryForm ();
344
- int dic = WordId .refDic (wordref , WordId .dic (node .wordId ));
345
- int headwordPtr = lexicon .wordInfos (dic ).headwordPtr (WordId .word (wordref ));
346
- s = lexicon .string (dic , headwordPtr );
347
- dictionaryForm = s ;
348
- }
349
- return s ;
350
- }
351
- }
352
-
353
258
/** Alias for {@link OOVFactory} constructor. */
354
259
public static OOVFactory oovFactory (short leftId , short rightId , short cost , short posId ) {
355
260
return new OOVFactory (leftId , rightId , cost , posId );
0 commit comments