PhraseTree { public String[] getLongestMatch(String[] matchText) { int i = 0; PhraseTreeElement match = root.getChild(matchText[i]); boolean isEndElement = false; while (match != null && i < matchText.length - 1) { isEndElement = match.isEndElement(); i++; String childToken = matchText[i]; PhraseTreeElement child = match.getChild(childToken); match = child; } String[] matchedString = null; if (isEndElement) { matchedString = Arrays.copyOfRange(matchText, 0, i); } return matchedString; } PhraseTree(); void addPhrase(String[] phraseParts); boolean contains(String[] phraseParts); String[] getLongestMatch(String[] matchText);  }
@Test public void matchTest() throws Exception { String[] sentence = "the red dog whines".split(" "); String[] longestMatch = phrases.getLongestMatch(sentence); assertArrayEquals(longestMatch, "the red dog".split(" ")); sentence = "the".split(" "); assertNull(phrases.getLongestMatch(sentence)); sentence = "red dog".split(" "); assertNull(phrases.getLongestMatch(sentence)); sentence = "the new".split(" "); assertNull(phrases.getLongestMatch(sentence)); }