Set<String> set = new HashSet<String>(); set.add("爱"); StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_29,set); TokenStream stream = analyzer .tokenStream(null, new StringReader("我爱你,hello world")); while (true) { Token token = stream.next(); if (null == token) { break; } System.out.println(new String(token.termBuffer(),0,token.termLength()) + "/t/t 起始位置 " + token.startOffset() + " 结束位置" + token.startOffset()); }
我 起始位置 0 结束位置0
你 起始位置 2 结束位置2
hello 起始位置 4 结束位置4
world 起始位置 10 结束位置10
其中 “爱”作为分词 stopWords 被过滤掉了