{"id":3536,"date":"2018-12-12T14:16:06","date_gmt":"2018-12-12T06:16:06","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3536"},"modified":"2018-12-12T14:16:06","modified_gmt":"2018-12-12T06:16:06","slug":"lucene-%e5%ae%9e%e6%88%98%e6%95%99%e7%a8%8b%e7%ac%ac%e4%b8%83%e7%ab%a0%e4%b8%83%e5%a4%a7%e5%88%86%e8%af%8d%e5%99%a8-analyzer-%e4%bb%8b%e7%bb%8d%e5%92%8c%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3536","title":{"rendered":"Lucene \u5b9e\u6218\u6559\u7a0b\u7b2c\u4e03\u7ae0\u4e03\u5927\u5206\u8bcd\u5668 Analyzer \u4ecb\u7ecd\u548c\u4f7f\u7528"},"content":{"rendered":"<p>\n\t\u8fd9\u4e00\u7ae0\u4e5f\u4e0d\u7b97\u662f\u4ec0\u4e48\u65b0\u7684\u5185\u5bb9\u4e86\uff0c\u524d\u9762\u6211\u5df2\u7ecf\u5199\u8fc7\u4e24\u7bc7\u5173\u4e8e Analyzer \u7684\u5185\u5bb9\u4e86\u3002\u5b83\u4eec\u5206\u522b\u662f\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3515\" title=\"\u6df1\u5165\u7406\u89e3 Lucene \u7684 Analyzer - \u4e1a\u4f59\u8349\">\u6df1\u5165\u7406\u89e3 Lucene \u7684 Analyzer<\/a>\u300b\u3001\u300a<a href=\"https:\/\/www.xttblog.com\/?p=3513\" title=\"\u8be6\u89e3 org.apache.lucene.analysis.Analyzer \u4f7f\u7528\u6559\u7a0b - \u4e1a\u4f59\u8349\">\u8be6\u89e3 org.apache.lucene.analysis.Analyzer \u4f7f\u7528\u6559\u7a0b<\/a>\u300b\u3002\u8fd9\u7bc7\u6587\u7ae0\uff0c\u6211\u4eec\u5728\u5bf9\u4e03\u4e2a\u5e38\u7528\u7684 Analyzer \u505a\u4e2a\u4ecb\u7ecd\u5427\uff01\n<\/p>\n<h2>\n\t\u4e3a\u4ec0\u4e48\u8981\u6709 Analyzer \u5462\uff1f<br \/>\n<\/h2>\n<p>\n\t\u5bf9\u4e8e Lucene \u800c\u8a00\uff0c\u4e0d\u7ba1\u662f\u7d22\u5f15\u8fd8\u662f\u68c0\u7d22\uff0c\u90fd\u662f\u9488\u5bf9\u7eaf\u6587\u672c\u800c\u8a00\uff0c\u5bf9\u4e8e\u7eaf\u6587\u672c\u7684\u6765\u6e90\u53ef\u4ee5\u662f PDF\uff0cWord\uff0cExcel\uff0cPPT\uff0cHTML\u7b49\uff0cLucene \u5bf9\u6b64\u5e76\u4e0d\u5173\u5fc3\uff0c\u53ea\u8981\u4fdd\u8bc1\u4f20\u9012\u7ed9 Lucene \u7684\u662f\u7eaf\u6587\u672c\u5373\u53ef\u3002\n<\/p>\n<p>\n\t\u4ee5\u6211\u4eec\u4f7f\u7528\u8c37\u6b4c\u548c\u767e\u5ea6\u4e3a\u4f8b\uff0c\u6211\u4eec\u5728\u641c\u7d22\u6846\u91cc\u8f93\u5165\u5185\u5bb9\u540e\uff0c\u641c\u7d22\u5f15\u64ce\u4f1a\u6839\u636e\u6211\u4eec\u8f93\u5165\u7684\u5185\u5bb9\u626f\u5206\u51fa\u5173\u952e\u5b57\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5bf9 Lucene \u6765\u8bf4\uff0c\u5c31\u662f Analyzer \u5206\u8bcd\u5668\u7684\u5e94\u7528\u573a\u666f\u91cc\u3002Analyzer \u4e3b\u8981\u662f\u7528\u6765\u6784\u5efa TokenStreams\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\u8fd9\u4e2a\u524d\u9762\u4e5f\u6709\u4ecb\u7ecd\uff0c\u540e\u9762\u8fd8\u4f1a\u518d\u6765\u5199\uff0c\u672c\u6587\u5148\u7565\u8fc7\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5e38\u7528\u7684 7 \u4e2a Analyzer \u5427\u3002\n<\/p>\n<table>\n<thead>\n<tr>\n<th>\n\t\t\t\t\u5206\u6790\u5668\n\t\t\t<\/th>\n<th>\n\t\t\t\t\u8bf4\u660e\n\t\t\t<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\n\t\t\t\tWhitespaceAnalyzer\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u6839\u636e\u7a7a\u683c\u62c6\u5206\u8bed\u6c47\u5355\u5143\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tSimpleAnalyzer\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u6839\u636e\u975e\u5b57\u6bcd\u62c6\u5206\u6587\u672c\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u5c0f\u5199\u5f62\u5f0f\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tStopAnalyzer\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u6839\u636e\u975e\u5b57\u6bcd\u62c6\u5206\u6587\u672c\uff0c\u7136\u540e\u5c0f\u5199\u5316\uff0c\u518d\u79fb\u9664\u505c\u7528\u8bcd\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tKeywordAnalyzer\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u5c06\u6574\u4e2a\u6587\u672c\u4f5c\u4e3a\u4e00\u4e2a\u5355\u4e00\u8bed\u6c47\u5355\u5143\u5904\u7406\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tStandardAnalyzer\n\t\t\t<\/td>\n<td>\n\t\t\t\t\u6839\u636eUnicode\u6587\u672c\u5206\u5272\u7b97\u6cd5\uff0c\u5177\u4f53\u7b97\u6cd5\u53c2\u8003<a href=\"http:\/\/unicode.org\/reports\/tr29\/\" rel=\"external\" target=\"_blank\">Unicode Standard Annex #29<\/a>\uff0c\u7136\u540e\u5c06\u6587\u672c\u8f6c\u5316\u4e3a\u5c0f\u5199\uff0c\u5e76\u79fb\u9664\u82f1\u6587\u505c\u7528\u8bcd\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tSmartChineseAnalyzer\n\t\t\t<\/td>\n<td>\n\t\t\t\tSmartChineseAnalyzer \u662f\u4e00\u4e2a\u667a\u80fd\u4e2d\u6587\u5206\u8bcd\u6a21\u5757\uff0c\u80fd\u591f\u5229\u7528\u6982\u7387\u5bf9\u6c49\u8bed\u53e5\u5b50\u8fdb\u884c\u6700\u4f18\u5207\u5206\uff0c\u5e76\u5185\u5d4c\u82f1\u6587 tokenizer\uff0c\u80fd\u6709\u6548\u5904\u7406\u4e2d\u82f1\u6587\u6df7\u5408\u7684\u6587\u672c\u5185\u5bb9\u3002\u5b83\u7684\u539f\u7406\u57fa\u4e8e\u81ea\u7136\u8bed\u8a00\u5904\u7406\u9886\u57df\u7684\u9690\u9a6c\u5c14\u79d1\u592b\u6a21\u578b\uff08HMM\uff09\uff0c\u5229\u7528\u5927\u91cf\u8bed\u6599\u5e93\u7684\u8bad\u7ec3\u6765\u7edf\u8ba1\u6c49\u8bed\u8bcd\u6c47\u7684\u8bcd\u9891\u548c\u8df3\u8f6c\u6982\u7387\uff0c\u4ece\u800c\u6839\u636e\u8fd9\u4e9b\u7edf\u8ba1\u7ed3\u679c\u5bf9\u6574\u4e2a\u6c49\u8bed\u53e5\u5b50\u8ba1\u7b97\u6700\u4f3c\u7136\uff08likelihood\uff09\u7684\u5207\u5206\u3002\u56e0\u4e3a\u667a\u80fd\u5206\u8bcd\u9700\u8981\u8bcd\u5178\u6765\u4fdd\u5b58\u8bcd\u6c47\u7684\u7edf\u8ba1\u503c\uff0cSmartChineseAnalyzer \u7684\u8fd0\u884c\u9700\u8981\u6307\u5b9a\u8bcd\u5178\u4f4d\u7f6e\uff0c\u5982\u4f55\u6307\u5b9a\u8bcd\u5178\u4f4d\u7f6e\u8bf7\u53c2\u8003org.apache.lucene.analysis.cn.smart.AnalyzerProfile\u3002SmartChineseAnalyzer \u7684\u7b97\u6cd5\u548c\u8bed\u6599\u5e93\u8bcd\u5178\u6765\u81ea\u4e8e<a href=\"http:\/\/ictclas.nlpir.org\/downloads\" rel=\"external\" target=\"_blank\">ICTCLAS<\/a>\n\t\t\t<\/td>\n<\/tr>\n<tr>\n<td>\n\t\t\t\tCJKAnalyzer\n\t\t\t<\/td>\n<td>\n\t\t\t\tCJK\u8868\u793a\u4e2d\u65e5\u97e9\uff0c\u76ee\u7684\u662f\u8981\u628a\u5206\u522b\u6765\u81ea\u4e2d\u6587\u3001\u65e5\u6587\u3001\u97e9\u6587\u3001\u8d8a\u6587\u4e2d\uff0c\u672c\u8d28\u3001\u610f\u4e49\u76f8\u540c\u3001\u5f62\u72b6\u4e00\u6837\u6216\u7a0d\u5f02\u7684\u8868\u610f\u6587\u5b57\uff08\u4e3b\u8981\u4e3a\u6c49\u5b57\uff0c\u4f46\u4e5f\u6709\u4eff\u6c49\u5b57\u5982\u65e5\u672c\u56fd\u5b57\u3001\u97e9\u56fd\u72ec\u6709\u6c49\u5b57\u3001\u8d8a\u5357\u7684\u5583\u5b57\uff09\u5728ISO 10646 \u53ca Unicode \u6807\u51c6\u5185\u8d4b\u4e88\u76f8\u540c\u7f16\u7801\u3002\u5bf9\u4e8e\u4e2d\u6587\u662f\u4ea4\u53c9\u53cc\u5b57\u5206\u5272\uff0c\u4e8c\u5143\u5206\u8bcd\u6cd5\n\t\t\t<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\n\tAnalyzer \u4e3b\u8981\u662f\u7528\u6765\u6784\u5efa TokenStreams\u3002\u4e0b\u9762\u6211\u4eec\u6765\u770b\u770b WhitespaceAnalyzer\uff0cSimpleAnalyzer\uff0cStopAnalyzer\uff0cStandardAnalyzer\uff0cCJKAnalyzer\uff0cSmartChineseAnalyzer \u8fd9 6 \u5927\u5e38\u7528\u5206\u8bcd\u5668\u7684\u7b80\u5355\u7528\u6cd5\u3002\u5b83\u4eec\u5206\u522b\u5177\u6709\u5bf9\u82f1\u6587\u3001\u4e2d\u6587\u3001\u7279\u6b8a\u7b26\u53f7\u53ca\u90ae\u7bb1\u7b49\u7684\u5207\u5206\u6548\u679c\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\npublic class AnalyzerDemo {\r\n    private static final String[] examples = {\r\n        &quot;The quick brown 1234 fox jumped over the lazy dog!&quot;, \r\n        &quot;XY&amp;Z 15.6 Corporation - xttblog@xttblog.com&quot;,\r\n        &quot;\u5317\u4eac\u5e02\u5317\u4eac\u5927\u5b66&quot;\r\n    };\r\n    private static final Analyzer[] ANALYZERS = new Analyzer[]{\r\n        new WhitespaceAnalyzer(), \r\n        new SimpleAnalyzer(), \r\n        new StopAnalyzer(), \r\n        new StandardAnalyzer(), \r\n        new CJKAnalyzer(), \r\n        new SmartChineseAnalyzer()\r\n    };\r\n    @Test\r\n    public void testAnalyzer() throws IOException {\r\n        for (int i = 0; i &lt; ANALYZERS.length; i++) {\r\n            String simpleName = ANALYZERS[i].getClass().getSimpleName();\r\n            for (int j = 0; j &lt; examples.length; j++) {\r\n                TokenStream contents = ANALYZERS[i].tokenStream(\"contents\", examples[j]);\r\n                \/\/TokenStream contents = ANALYZERS[i].tokenStream(\"contents\", new StringReader(examples[j]));\r\n                OffsetAttribute offsetAttribute = contents.addAttribute(OffsetAttribute.class);\r\n                TypeAttribute typeAttribute = contents.addAttribute(TypeAttribute.class);\r\n                contents.reset();\r\n                System.out.println(simpleName + &quot; analyzing : &quot; + examples[j]);\r\n                while (contents.incrementToken()) {\r\n                    String s1 = offsetAttribute.toString();\r\n                    int i1 = offsetAttribute.startOffset();\/\/\u8d77\u59cb\u504f\u79fb\u91cf\r\n                    int i2 = offsetAttribute.endOffset();\/\/\u7ed3\u675f\u504f\u79fb\u91cf\r\n                    System.out.print(s1 + &quot;[\" + i1 + \",\" + i2 + \":\" + typeAttribute.type() + \"]&quot; + &quot; &quot;);\r\n                }\r\n                contents.end();\r\n                contents.close();\r\n                System.out.println();\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>\n\t\u8fd0\u884c\u6548\u679c\u6211\u5c31\u4e0d\u8d34\u4e86\uff0c\u5927\u5bb6\u81ea\u5df1\u53bb\u770b\uff01<\/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=3536\">Lucene \u5b9e\u6218\u6559\u7a0b\u7b2c\u4e03\u7ae0\u4e03\u5927\u5206\u8bcd\u5668 Analyzer \u4ecb\u7ecd\u548c\u4f7f\u7528<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e00\u7ae0\u4e5f\u4e0d\u7b97\u662f\u4ec0\u4e48\u65b0\u7684\u5185\u5bb9\u4e86\uff0c\u524d\u9762\u6211\u5df2\u7ecf\u5199\u8fc7\u4e24\u7bc7\u5173\u4e8e Analyzer \u7684\u5185\u5bb9\u4e86\u3002\u5b83\u4eec\u5206\u522b\u662f\u300a\u6df1\u5165\u7406\u89e3 Luc [&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":[1454,1444,1794,1449,1793],"class_list":["post-3536","post","type-post","status-publish","format-standard","hentry","category-java","tag-analyzer","tag-lucene","tag-tokenfilter","tag-tokenizer","tag-tokenstream"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3536","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=3536"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3536\/revisions"}],"predecessor-version":[{"id":3537,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3536\/revisions\/3537"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}