diff --git a/index.html b/index.html index fa0d31a..3b75bb3 100644 --- a/index.html +++ b/index.html @@ -113,7 +113,56 @@
Trie trie = new Trie().onlyWholeWords();
+ trie.addKeyword("sugar");
+ Collection<Emit> emits = trie.parseText("sugarcane sugarcane sugar canesugar");
+In this case, it will only find one match, whereas it would normally find four. The sugarcane/canesugar words + are discarded because they are partial matches.
+ +Some text are WrItTeN in combinations of lowercase and uppercase and therefore hard to identify. You can instruct + the Trie to lowercase the entire searchtext to ease the matching process.
+ + Trie trie = new Trie().caseInsensitive();
+ trie.addKeyword("casing");
+ Collection<Emit> emits = trie.parseText("CaSiNg");
+Normally, this match would not be found. With the caseInsensitive settings the entire search text is lowercased + before the matching begins. Therefore it will find exactly one match. Since you still have control of the original + search text and you will know exactly where the match was, you can still utilize the original casing.
+ +In many cases you may want to do useful stuff with both the non-matching and the matching text. In this case, you + might be better served by using the Trie.tokenize(). It allows you to loop over the entire text and deal with + matches as soon as you encounter them. Let's look at an example where we want to highlight words from HGttG in HTML:
+ + String speech = "The Answer to the Great Question... Of Life, " +
+ "the Universe and Everything... Is... Forty-two,' said " +
+ "Deep Thought, with infinite majesty and calm.";
+ Trie trie = new Trie().removeOverlaps().onlyWholeWords().caseInsensitive();
+ trie.addKeyword("great question");
+ trie.addKeyword("forty-two");
+ trie.addKeyword("deep thought");
+ Collection<Token> tokens = trie.tokenize(speech);
+ StringBuffer html = new StringBuffer();
+ html.append("<html><body><p>");
+ for (Token token : tokens) {
+ if (token.isMatch()) {
+ html.append("<i>");
+ }
+ html.append(token.getFragment());
+ if (token.isMatch()) {
+ html.append("</i>");
+ }
+ }
+ html.append("</p></body></html>");
+ System.out.println(html);
+Licensed under the Apache License, Version 2.0 (the "License");