{"id":3538,"date":"2018-12-12T15:09:27","date_gmt":"2018-12-12T07:09:27","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3538"},"modified":"2018-12-12T15:09:27","modified_gmt":"2018-12-12T07:09:27","slug":"lucene-%e5%ae%9e%e6%88%98%e6%95%99%e7%a8%8b%e7%ac%ac%e5%85%ab%e7%ab%a0%e8%af%b4%e8%af%b4-tokenstream-%e5%92%8c-tokenfilter","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3538","title":{"rendered":"Lucene \u5b9e\u6218\u6559\u7a0b\u7b2c\u516b\u7ae0\u8bf4\u8bf4 TokenStream \u548c TokenFilter"},"content":{"rendered":"<p>\n\t\u5f53\u4f60\u6df1\u5165\u9aa8\u9ad3\u7684\u53bb\u4e86\u89e3\u4e00\u9879\u6280\u672f\uff0c\u4f60\u624d\u80fd\u83b7\u5f97\u5feb\u611f\uff01\u4e0a\u4e00\u7ae0\uff0c\u6211\u4eec\u8bf4\u8fc7 Analyzer \u5206\u8bcd\u5668\u7684\u4e3b\u8981\u662f\u7528\u6765\u6784\u5efa TokenStreams\uff0c\u90a3\u4e48\u6211\u4eec\u4eca\u5929\u5c31\u4e00\u8d77\u901a\u8fc7\u672c\u6587\u6765\u770b\u770b Analyzer \u7684\u5185\u90e8\u6784\u9020\uff0cTokenStream \u548c TokenFilter\u3002\n<\/p>\n<h2>\n\tTokenStream<br \/>\n<\/h2>\n<p>\n\tTokenStream \u662f\u5206\u6790\u5904\u7406\u7ec4\u4ef6\u4e2d\u7684\u4e00\u79cd\u4e2d\u95f4\u6570\u636e\u683c\u5f0f\uff0c\u5b83\u4ece\u4e00\u4e2a reader \u4e2d\u83b7\u53d6\u6587\u672c\uff0c\u5e76\u4ee5 TokenStream \u4f5c\u4e3a\u8f93\u51fa\u7ed3\u679c\u3002\u5728\u6240\u6709\u7684\u8fc7\u6ee4\u5668\u4e2d\uff0cTokenStream \u540c\u65f6\u5145\u5f53\u7740\u8f93\u5165\u548c\u8f93\u51fa\u683c\u5f0f\u3002Tokenizer \u548c TokenFilter \u7ee7\u627f\u81ea TokenStream\uff0cTokenizer \u662f\u4e00\u4e2a TokenStream\uff0c\u5176\u8f93\u5165\u6e90\u662f\u4e00\u4e2a Reader\uff1bTokenFilter \u4e5f\u662f\u4e00\u4e2a TokenStream\uff0c\u5176\u8f93\u5165\u6e90\u662f\u53e6\u4e00\u4e2a TokenStream\u3002\u800c TokenStream \u7b80\u5355\u70b9\u8bf4\u5c31\u662f\u751f\u6210\u5668\u7684\u8f93\u51fa\u7ed3\u679c\u3002TokenStream \u662f\u4e00\u4e2a\u5206\u8bcd\u540e\u7684 Token \u7ed3\u679c\u7ec4\u6210\u7684\u6d41\uff0c\u901a\u8fc7\u6d41\u80fd\u591f\u4e0d\u65ad\u7684\u5f97\u5230\u4e0b\u4e00\u4e2a Token\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n@Test\r\npublic void testTokenStream() throws IOException {\r\n    Analyzer analyzer = new WhitespaceAnalyzer();\r\n    String inputText = &quot;This is a test text for token!&quot;;\r\n    TokenStream tokenStream = analyzer.tokenStream(&quot;text&quot;, new StringReader(inputText));\r\n    \/\/\u4fdd\u5b58token\u5b57\u7b26\u4e32\r\n    CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);\r\n    \/\/\u5728\u8c03\u7528incrementToken()\u5f00\u59cb\u6d88\u8d39token\u4e4b\u524d\u9700\u8981\u91cd\u7f6estream\u5230\u4e00\u4e2a\u5e72\u51c0\u7684\u72b6\u6001\r\n    tokenStream.reset();\r\n    while (tokenStream.incrementToken()) {\r\n        \/\/\u6253\u5370\u5206\u8bcd\u7ed3\u679c\r\n        System.out.print(&quot;[\" + charTermAttribute + \"]&quot;);\r\n    }\r\n}<\/pre>\n<p>\n\tTokenStream \u7684\u4f7f\u7528\u603b\u7ed3\u8d77\u6765\u53ef\u4ee5\u5206\u4e3a 3 \u6b65\u3002\u83b7\u5f97 TokenStream\uff1b\u91cd\u7f6e tokenStream.reset();\u83b7\u5f97 token\uff0ctokenStream.incrementToken()\u3002\n<\/p>\n<h2>\n\tTokenAttribute<br \/>\n<\/h2>\n<p>\n\t\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a CharTermAttribute\u3002\u5b83\u662f\u5728\u8c03\u7528 tokenStream() \u65b9\u6cd5\u4e4b\u540e\u83b7\u5f97\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 tokenStream.addAttribute \u65b9\u6cd5\u6dfb\u52a0\u591a\u4e2a Attribute\u3002\u4ece\u800c\u53ef\u4ee5\u4e86\u89e3\u5230\u5206\u8bcd\u4e4b\u540e\u8be6\u7ec6\u7684\u8bcd\u5143\u4fe1\u606f\uff0c\u6bd4\u5982 CharTermAttribute \u7528\u4e8e\u4fdd\u5b58\u8bcd\u5143\u7684\u5185\u5bb9\uff0cTypeAttribute \u7528\u4e8e\u4fdd\u5b58\u8bcd\u5143\u7684\u7c7b\u578b\u3002\n<\/p>\n<p>\n\t\u5728 Lucene \u4e2d\u63d0\u4f9b\u4e86\u51e0\u79cd\u7c7b\u578b\u7684 Attribute\uff0c\u6bcf\u79cd\u7c7b\u578b\u7684 Attribute \u63d0\u4f9b\u4e00\u4e2a\u4e0d\u540c\u7684\u65b9\u9762\u6216\u8005 token \u7684\u5143\u6570\u636e\u3002\u5e38\u7528\u7684 Attribute \u5982\u4e0b\uff1a\n<\/p>\n<table>\n<thead>\n<tr>\n<th>\n\t\t\t\t\u540d\u79f0\n\t\t\t<\/th>\n<th>\n\t\t\t\t\u4f5c\u7528\n\t\t\t<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n\t\t\t\tCharTermAttribute\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u8868\u793atoken\u672c\u8eab\u7684\u5185\u5bb9\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tPositionIncrementAttribute\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u8868\u793a\u5f53\u524dtoken\u76f8\u5bf9\u4e8e\u524d\u4e00\u4e2atoken\u7684\u76f8\u5bf9\u4f4d\u7f6e\uff0c\u4e5f\u5c31\u662f\u76f8\u9694\u7684\u8bcd\u8bed\u6570\u91cf\uff08\u4f8b\u5982&ldquo;text for attribute&rdquo;\uff0ctext\u548cattribute\u4e4b\u95f4\u7684getPositionIncrement\u4e3a2\uff09\uff0c\u5982\u679c\u4e24\u8005\u4e4b\u95f4\u6ca1\u6709\u505c\u7528\u8bcd\uff0c\u90a3\u4e48\u8be5\u503c\u88ab\u7f6e\u4e3a\u9ed8\u8ba4\u503c1\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tOffsetAttribute\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u8868\u793atoken\u7684\u9996\u5b57\u6bcd\u548c\u5c3e\u5b57\u6bcd\u5728\u539f\u6587\u672c\u4e2d\u7684\u4f4d\u7f6e\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tTypeAttribute\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u8868\u793atoken\u7684\u8bcd\u6c47\u7c7b\u578b\u4fe1\u606f\uff0c\u9ed8\u8ba4\u503c\u4e3aword\uff0c\u5176\u5b83\u503c\u6709&lt;ALPHANUM&gt; &lt;APOSTROPHE&gt; &lt;ACRONYM&gt; &lt;COMPANY&gt; &lt;EMAIL&gt; &lt;HOST&gt; &lt;NUM&gt; &lt;CJ&gt; &lt;ACRONYM_DEP&gt;\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tFlagsAttribute\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u4e0eTypeAttribute\u7c7b\u4f3c\uff0c\u5047\u8bbe\u4f60\u9700\u8981\u7ed9token\u6dfb\u52a0\u989d\u5916\u7684\u4fe1\u606f\uff0c\u800c\u4e14\u5e0c\u671b\u8be5\u4fe1\u606f\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u94fe\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u901a\u8fc7flags\u53bb\u4f20\u9012\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tPayloadAttribute\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u5728\u6bcf\u4e2a\u7d22\u5f15\u4f4d\u7f6e\u90fd\u5b58\u50a8\u4e86payload\uff08\u5173\u952e\u4fe1\u606f\uff09\uff0c\u5f53\u4f7f\u7528\u57fa\u4e8ePayload\u7684\u67e5\u8be2\u65f6\uff0c\u8be5\u4fe1\u606f\u5728\u8bc4\u5206\u4e2d\u975e\u5e38\u6709\u7528\n\t\t\t<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\n\tAttribute \u7528\u6cd5\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\n@Test\r\npublic void testAttribute() throws IOException {\r\n    Analyzer analyzer = new StandardAnalyzer();\r\n    String input = &quot;This is a test text for attribute! Just add-some word.&quot;;\r\n    TokenStream tokenStream = analyzer.tokenStream(&quot;text&quot;, new StringReader(input));\r\n    CharTermAttribute charTermAttribute = tokenStream.addAttribute(CharTermAttribute.class);\r\n    PositionIncrementAttribute positionIncrementAttribute = \r\n        tokenStream.addAttribute(PositionIncrementAttribute.class);\r\n    OffsetAttribute offsetAttribute = tokenStream.addAttribute(OffsetAttribute.class);\r\n    TypeAttribute typeAttribute = tokenStream.addAttribute(TypeAttribute.class);\r\n    PayloadAttribute payloadAttribute = tokenStream.addAttribute(PayloadAttribute.class);\r\n    payloadAttribute.setPayload(new BytesRef(&quot;Just&quot;));\r\n    tokenStream.reset();\r\n    while (tokenStream.incrementToken()) {\r\n        System.out.print(&quot;[&quot; + charTermAttribute + &quot; increment:&quot; + \r\n            positionIncrementAttribute.getPositionIncrement() +\r\n                &quot; start:&quot; + offsetAttribute\r\n                .startOffset() + &quot; end:&quot; +\r\n                offsetAttribute\r\n                        .endOffset() + &quot; type:&quot; + typeAttribute.type() + \r\n                        &quot; payload:&quot; + payloadAttribute.getPayload() +\r\n                &quot;]\\n&quot;);\r\n    }\r\n    tokenStream.end();\r\n    tokenStream.close();\r\n}<\/pre>\n<p>\n\t\u5728\u8c03\u7528 incrementToken() \u7ed3\u675f\u8fed\u4ee3\u4e4b\u540e\uff0c\u8c03\u7528 end() \u548c close() \u65b9\u6cd5\uff0c\u5176\u4e2d end() \u53ef\u4ee5\u5524\u9192\u5f53\u524d TokenStream \u7684\u5904\u7406\u5668\u53bb\u505a\u4e00\u4e9b\u6536\u5c3e\u5de5\u4f5c\uff0cclose() \u53ef\u4ee5\u5173\u95ed TokenStream \u548c Analyzer \u53bb\u91ca\u653e\u5728\u5206\u6790\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u7684\u8d44\u6e90\u3002\n<\/p>\n<h2>\n\tTokenFilter<br \/>\n<\/h2>\n<p>\n\tTokenFilter \u4e3b\u8981\u7528\u4e8e TokenStream \u7684\u8fc7\u6ee4\u64cd\u4f5c\uff0c\u7528\u6765\u5904\u7406 Tokenizer \u6216\u8005\u4e0a\u4e00\u4e2a TokenFilter \u5904\u7406\u540e\u7684\u7ed3\u679c\uff0c\u5982\u679c\u662f\u5bf9\u73b0\u6709\u5206\u8bcd\u5668\u8fdb\u884c\u6269\u5c55\u6216\u4fee\u6539\uff0c\u63a8\u8350\u4f7f\u7528\u81ea\u5b9a\u4e49 TokenFilter \u65b9\u5f0f\u3002\u81ea\u5b9a\u4e49TokenFilter \u9700\u8981\u5b9e\u73b0 incrementToken() \u62bd\u8c61\u51fd\u6570\uff0c\u5e76\u4e14\u8be5\u65b9\u6cd5\u9700\u8981\u58f0\u660e\u4e3a final \u7684\uff0c\u5728\u6b64\u51fd\u6570\u4e2d\u5bf9\u8fc7\u6ee4 Term \u7684 CharTermAttribute \u548c PositionIncrementAttribute \u7b49\u5c5e\u6027\u8fdb\u884c\u64cd\u4f5c\uff0c\u5c31\u80fd\u5b9e\u73b0\u8fc7\u6ee4\u529f\u80fd\u3002\n<\/p>\n<p>\n\t\u4e00\u4e2a\u7b80\u5355\u7684\u8bcd\u6269\u5c55\u8fc7\u6ee4\u5668\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nimport org.apache.lucene.analysis.Analyzer;\r\nimport org.apache.lucene.analysis.TokenFilter;\r\nimport org.apache.lucene.analysis.TokenStream;\r\nimport org.apache.lucene.analysis.core.WhitespaceAnalyzer;\r\nimport org.apache.lucene.analysis.tokenattributes.CharTermAttribute;\r\nimport org.junit.Test;\r\nimport java.io.IOException;\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\npublic class TestTokenFilter {\r\n    @Test\r\n    public void test() throws IOException {\r\n        String text = &quot;Hi, Dr Wang, Mr Liu asks if you stay with Mrs Liu yesterday!&quot;;\r\n        Analyzer analyzer = new WhitespaceAnalyzer();\r\n        CourtesyTitleFilter filter = new CourtesyTitleFilter(analyzer.tokenStream(&quot;text&quot;, text));\r\n        CharTermAttribute charTermAttribute = filter.addAttribute(CharTermAttribute.class);\r\n        filter.reset();\r\n        while (filter.incrementToken()) {\r\n            System.out.print(charTermAttribute + &quot; &quot;);\r\n        }\r\n    }\r\n}\r\n\/**\r\n * \u81ea\u5b9a\u4e49\u8bcd\u6269\u5c55\u8fc7\u6ee4\u5668\r\n *\/\r\nclass CourtesyTitleFilter extends TokenFilter {\r\n    Map&lt;String, String&gt; courtesyTitleMap = new HashMap&lt;&gt;();\r\n    private CharTermAttribute termAttribute;\r\n    \/**\r\n     * Construct a token stream filtering the given input.\r\n     *\r\n     * @param input\r\n     *\/\r\n    protected CourtesyTitleFilter(TokenStream input) {\r\n        super(input);\r\n        termAttribute = addAttribute(CharTermAttribute.class);\r\n        courtesyTitleMap.put(&quot;Dr&quot;, &quot;doctor&quot;);\r\n        courtesyTitleMap.put(&quot;Mr&quot;, &quot;mister&quot;);\r\n        courtesyTitleMap.put(&quot;Mrs&quot;, &quot;miss&quot;);\r\n    }\r\n    @Override\r\n    public final boolean incrementToken() throws IOException {\r\n        if (!input.incrementToken()) {\r\n            return false;\r\n        }\r\n        String small = termAttribute.toString();\r\n        if (courtesyTitleMap.containsKey(small)) {\r\n            termAttribute.setEmpty().append(courtesyTitleMap.get(small));\r\n        }\r\n        return true;\r\n    }\r\n}<\/pre>\n<p>\n\t\u901a\u8fc7 TokenStream\u3001Tokenizer\u3001TokenFilter\u3001Analyzer \u4f60\u53ef\u4ee5\u770b\u5230\u4e00\u5207\u7b80\u5355\u800c\u53c8\u7d27\u5bc6\u7684\u7ed3\u5408\u5728\u4e00\u8d77\u3002\u5176\u5b9e\u770b\u61c2\u4e86\u5c31\u4e0d\u89c9\u5f97\u96be\u4e86\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3538\">Lucene \u5b9e\u6218\u6559\u7a0b\u7b2c\u516b\u7ae0\u8bf4\u8bf4 TokenStream \u548c TokenFilter<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u5f53\u4f60\u6df1\u5165\u9aa8\u9ad3\u7684\u53bb\u4e86\u89e3\u4e00\u9879\u6280\u672f\uff0c\u4f60\u624d\u80fd\u83b7\u5f97\u5feb\u611f\uff01\u4e0a\u4e00\u7ae0\uff0c\u6211\u4eec\u8bf4\u8fc7 Analyzer \u5206\u8bcd\u5668\u7684\u4e3b\u8981\u662f\u7528\u6765\u6784\u5efa To [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1806,1444,1807,1794,1793],"class_list":["post-3538","post","type-post","status-publish","format-standard","hentry","category-java","tag-chartermattribute","tag-lucene","tag-offsetattribute","tag-tokenfilter","tag-tokenstream"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3538"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3538\/revisions"}],"predecessor-version":[{"id":3539,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3538\/revisions\/3539"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}