@@ -102,7 +102,7 @@ public List<Token> encode(String text) throws IllegalStateException {
102102 text = normalize (text );
103103 List <Symbol > symbols = new ArrayList <>(text .length ());
104104 for (int i = 0 ; i < text .length (); ) {
105- int len = uTrie .prefixLen (text . substring ( i ) );
105+ int len = uTrie .prefixLen (text , i );
106106 if (len > 0 ) {
107107 symbols .add (
108108 new Symbol (text .substring (i , i + len ), true , symbols .size () - 1 , symbols .size () + 1 ));
@@ -212,7 +212,7 @@ public String decodeIds(List<Integer> ids) {
212212 }
213213
214214 private String normalize (String text ) {
215- return text .replaceAll ( " " , "▁" );
215+ return text .replace ( ' ' , '▁' );
216216 }
217217
218218 private void addNewCandidate (
@@ -244,12 +244,15 @@ private String maybeMerge(String a, String b) {
244244 private boolean isMergeCandidateValid (List <Symbol > symbols , MergeCandidate symbol ) {
245245 String left = symbols .get (symbol .left ).text ;
246246 String right = symbols .get (symbol .right ).text ;
247- return left != "" && right != "" && left .length () + right .length () == symbol .length ;
247+ return ! left . isEmpty () && ! right . isEmpty () && ( left .length () + right .length () == symbol .length ) ;
248248 }
249249
250250 private int symbolToID (Symbol symbol ) {
251- if (this .pieces .containsKey (symbol .text )) return this .pieces .get (symbol .text );
252- if (this .reserved .containsKey (symbol .text )) return this .reserved .get (symbol .text );
251+ if (this .pieces .containsKey (symbol .text )) {
252+ return this .pieces .get (symbol .text );
253+ } else if (this .reserved .containsKey (symbol .text )) {
254+ return this .reserved .get (symbol .text );
255+ }
253256
254257 return this .unkID ;
255258 }
@@ -358,17 +361,18 @@ public Trie() {
358361
359362 // Inserts a word into the trie.
360363 public void insert (String word ) {
361- int index , i ;
364+ int i ;
362365 char ch ;
363366 TrieNode node = root ;
364367 for (i = 0 ; i < word .length (); i ++) {
365368 ch = word .charAt (i );
366- index = ch - 'a' ;
367369 if (node .childreNode .get (ch ) == null ) {
368370 node .childreNode .put (ch , new TrieNode ());
369371 }
370372 node = node .childreNode .get (ch );
371- if (i == word .length () - 1 ) node .freq ++;
373+ if (i == word .length () - 1 ) {
374+ node .freq ++;
375+ }
372376 }
373377 }
374378
@@ -380,22 +384,27 @@ public boolean search(String word) {
380384 for (i = 0 ; i < word .length (); i ++) {
381385 ch = word .charAt (i );
382386 node = node .childreNode .get (ch );
383- if (node == null ) return false ;
384- if (i == word .length () - 1 && node .freq > 0 ) return true ;
387+ if (node == null ) {
388+ return false ;
389+ } else if (i == word .length () - 1 && node .freq > 0 ) {
390+ return true ;
391+ }
385392 }
386393 return false ;
387394 }
388395
389- public int prefixLen (String word ) {
390- int index , i ;
391- char ch ;
396+ public int prefixLen (String word , int offset ) {
392397 TrieNode node = root ;
393398 int result = 0 ;
394- for (i = 0 ; i < word .length (); i ++) {
395- ch = word .charAt (i );
399+ for (int i = offset ; i < word .length (); i ++) {
400+ char ch = word .charAt (i );
396401 node = node .childreNode .get (ch );
397- if (node == null ) break ;
398- if (node .freq > 0 ) result = i + 1 ;
402+ if (node == null ) {
403+ break ;
404+ }
405+ if (node .freq > 0 ) {
406+ result = i - offset + 1 ;
407+ }
399408 }
400409 return result ;
401410 }
@@ -410,8 +419,11 @@ public boolean startsWith(String prefix) {
410419 ch = prefix .charAt (i );
411420 index = ch - 'a' ;
412421 node = node .childreNode .get (ch );
413- if (node == null ) return false ;
414- if (i == prefix .length () - 1 ) return true ;
422+ if (node == null ) {
423+ return false ;
424+ } else if (i == prefix .length () - 1 ) {
425+ return true ;
426+ }
415427 }
416428 return false ;
417429 }
0 commit comments