@Test public void test_findLongestWord_2() throws Exception{ String[] expected = { "ソフトウェアコンテナ", "オープンソースソフトウェア", "Linuxカーネル", "Linuxコンテナ技術", "ファイルシステム", "ファイル", "ファイル", "Linuxカーネル" }; Trie t = trieWithWords("ソフトウェア", "ソフトウェアコンテナ", "オープンソース", "オープンソースソフトウェア", "Linux", "Linuxカーネル", "Linuxコンテナ", "Linuxコンテナ技術", "ファイル", "ファイルシステム"); List<String> actual = new ArrayList<>(); for(String s : longSentences){ int begin = 0; int found = -1; StringBuilder b = new StringBuilder(); while((found = t.findLongestWord(s, begin, s.length(), b)) != -1){ actual.add(b.toString()); begin = found + b.length(); b = new StringBuilder(); } } Assert.assertArrayEquals(expected, actual.toArray(new String[]{})); }
@Override public int findLongestWord(CharSequence chars, int start, int end, StringBuilder word) { for(int i = start; i < end; i++){ Iterator<String> it = commonPrefixSearch(chars.subSequence(i, end).toString()).iterator(); String last = null; while(it.hasNext()){ last = it.next(); } if(last != null){ if(word != null) word.append(last); return i; } } return -1; }
AbstractTrie implements Trie { @Override public int findLongestWord(CharSequence chars, int start, int end, StringBuilder word) { for(int i = start; i < end; i++){ Iterator<String> it = commonPrefixSearch(chars.subSequence(i, end).toString()).iterator(); String last = null; while(it.hasNext()){ last = it.next(); } if(last != null){ if(word != null) word.append(last); return i; } } return -1; } }
AbstractTrie implements Trie { @Override public int findLongestWord(CharSequence chars, int start, int end, StringBuilder word) { for(int i = start; i < end; i++){ Iterator<String> it = commonPrefixSearch(chars.subSequence(i, end).toString()).iterator(); String last = null; while(it.hasNext()){ last = it.next(); } if(last != null){ if(word != null) word.append(last); return i; } } return -1; }  }
AbstractTrie implements Trie { @Override public int findLongestWord(CharSequence chars, int start, int end, StringBuilder word) { for(int i = start; i < end; i++){ Iterator<String> it = commonPrefixSearch(chars.subSequence(i, end).toString()).iterator(); String last = null; while(it.hasNext()){ last = it.next(); } if(last != null){ if(word != null) word.append(last); return i; } } return -1; }  @Override int findWord(CharSequence chars, int start, int end, StringBuilder word); @Override int findShortestWord(CharSequence chars, int start, int end, StringBuilder word); @Override int findLongestWord(CharSequence chars, int start, int end, StringBuilder word); @Override void dump(Writer writer); @Override void trimToSize(); @Override void freeze(); @Override void insert(String word); }
AbstractTrie implements Trie { @Override public int findLongestWord(CharSequence chars, int start, int end, StringBuilder word) { for(int i = start; i < end; i++){ Iterator<String> it = commonPrefixSearch(chars.subSequence(i, end).toString()).iterator(); String last = null; while(it.hasNext()){ last = it.next(); } if(last != null){ if(word != null) word.append(last); return i; } } return -1; }  @Override int findWord(CharSequence chars, int start, int end, StringBuilder word); @Override int findShortestWord(CharSequence chars, int start, int end, StringBuilder word); @Override int findLongestWord(CharSequence chars, int start, int end, StringBuilder word); @Override void dump(Writer writer); @Override void trimToSize(); @Override void freeze(); @Override void insert(String word);  }