{"id":2748,"date":"2013-05-12T12:57:51","date_gmt":"2013-05-12T04:57:51","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=2748"},"modified":"2013-09-26T09:12:54","modified_gmt":"2013-09-26T01:12:54","slug":"%e7%94%a8pyenchant%e5%81%9a%e6%8b%bc%e5%86%99%e6%a3%80%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/2748.html","title":{"rendered":"\u7528PyEnchant\u505a\u62fc\u5199\u68c0\u67e5"},"content":{"rendered":"<p><span style=\"font-size: small;\">\u5b89\u88c5 PyEnchant\uff1a<\/span><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo pip install pyenchant\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u521d\u6b65\u4f7f\u7528\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n&gt;&gt;&gt; import enchant\r\n&gt;&gt;&gt; d = enchant.Dict(&quot;en_US&quot;)\r\n&gt;&gt;&gt; d.check(&quot;Hello&quot;)\r\nTrue\r\n&gt;&gt;&gt; d.check(&quot;Helo&quot;)\r\nFalse\r\n\r\n&gt;&gt;&gt; d = enchant.Dict()\r\n&gt;&gt;&gt; d.tag\r\n'en_AU'\r\n&gt;&gt;&gt; print d.tag\r\nen_AU\r\n\r\n&gt;&gt;&gt; enchant.dict_exists(&quot;fake&quot;)\r\nFalse\r\n&gt;&gt;&gt; enchant.dict_exists(&quot;en_US&quot;)\r\nTrue\r\n&gt;&gt;&gt; d = enchant.request_dict(&quot;en_US&quot;)\r\n&gt;&gt;&gt; d\r\n&lt;enchant.Dict object at 0x2aaaabdffa50&gt;cl\r\n&gt;&gt;&gt; enchant.list_languages()\r\n&#x5B;'en', 'en_CA', 'en_GB', 'en_US', 'eo', 'fr', 'fr_CH', 'fr_FR']\r\n\r\n# \u62fc\u5199\u68c0\u67e5\uff0c\u7ed9\u51fa\u6240\u6709\u53ef\u80fd\u7684\u60c5\u51b5\r\n&gt;&gt;&gt; d.suggest(&quot;Helo&quot;)\r\n&#x5B;'He lo', 'He-lo', 'Hello', 'Helot', 'Help', 'Halo', 'Hell', 'Held', 'Helm', 'Hero', &quot;He'll&quot;]\r\n\r\n# \u81ea\u5b9a\u4e49\u5b57\u5178\uff0c\u5b57\u5178\u5185\u5bb9\u76f4\u63a5\u5c31\u662f\u4e00\u884c\u4e00\u4e2a\u5355\u8bcd\r\npwl = enchant.request_pwl_dict(&quot;mywords.txt&quot;)\r\n# \u5c06\u81ea\u5b9a\u4e49\u5b57\u5178\u548c\u7cfb\u7edf\u5185\u7f6e\u5b57\u5178\u7ed3\u5408\u8d77\u6765\r\n&gt;&gt;&gt; d2 = enchant.DictWithPWL(&quot;en_US&quot;,&quot;mywords.txt&quot;)\r\n&gt;&gt;&gt; d2.check(&quot;Hello&quot;)\r\nTrue\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u6211\u5199\u7684\u4e00\u6bb5\u652f\u6301\u62fc\u97f3\u7684\u7ea0\u9519\u529f\u80fd\uff1a<\/span><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom collections import defaultdict\r\nfrom xpinyin import Pinyin\r\nimport enchant\r\n\r\nclass SpellCheck:\r\n\r\n    instance = None   # \u5355\u5b9e\u4f8b\r\n    pwl = None        # \u4e2d\u6587 enchant\u5bf9\u8c61\r\n    pwl_pinyin = None # \u62fc\u97f3 enchant\u5bf9\u8c61\r\n    pinyin_map = None # \u62fc\u97f3\u5bf9\u6c49\u5b57\u7684\u5b57\u5178\r\n    all_words = &#x5B;]    # \u6240\u6709\u4e2d\u6587\u8bcd\r\n    all_pinyins = &#x5B;]  # \u6240\u6709\u62fc\u97f3\r\n\r\n    @classmethod\r\n    def sugguest(self, word):\r\n        ''''\r\n        \u62fc\u5199\u7ea0\u9519\r\n        :param word: \u9700\u8981\u7ea0\u6b63\u6211\u8bcd\uff0c\u4e2d\u6587\u6216\u662f\u62fc\u97f3\r\n        :return: \u8fd4\u56de\u6240\u6709\u53ef\u80fd\u6b63\u786e\u7684\u8bcd\uff0c\u5217\u8868\r\n        '''\r\n        spell_check = SpellCheck.get_instance()\r\n\r\n        in_words = &#x5B;w for w in spell_check.all_words if word in w]  # \u5b8c\u5168\u5339\u914d\u7684\r\n        zh_sugguests = sorted(in_words, key=lambda x: len(x))\r\n        zh_sugguests2 = spell_check.pwl.suggest(word)  # \u62fc\u5199\u7ea0\u9519\u7684\r\n        zh_sugguests.extend(zh_sugguests2)\r\n        result = filter(lambda x: x not in word, zh_sugguests)\r\n\r\n        p = Pinyin()\r\n        py = p.get_pinyin(word)\r\n        in_words = &#x5B;w for w in spell_check.all_pinyins if py in w] # \u5b8c\u5168\u5339\u914d\u7684-\u62fc\u97f3\r\n        py_sugguests = sorted(in_words, key=lambda x: len(x))\r\n        py_sugguests2 = spell_check.pwl_pinyin.suggest(py)  # \u62fc\u5199\u7ea0\u9519\u7684-\u62fc\u97f3\r\n        py_sugguests.extend(py_sugguests2)\r\n        for sugguest in py_sugguests:\r\n            result.extend(spell_check.pinyin_map&#x5B;sugguest])\r\n\r\n        result = list(set(result))\r\n        return result\r\n<\/pre>\n<p><span style=\"font-size: small;\">\u5b98\u65b9\u6559\u7a0b\uff1ahttp:\/\/pythonhosted.org\/pyenchant\/tutorial.html<\/span><\/p>\n<p><span style=\"font-size: small;\">\u5176\u5b83\u7c7b\u4f3c\u7684\u5e93\u6709\uff0cHunspell\uff0cASpell\uff0cISpell\u7b49\u3002\u5176\u4e2d Hunspell \u4e3a Chrome\u3001Firefox\u3001OpenOffice \u7b49\u505a\u4e3a\u62fc\u5199\u68c0\u67e5\u5668\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88c5 PyEnchant\uff1a sudo pip install pyenchant \u521d\u6b65\u4f7f\u7528\uff1a &gt;&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-2748","post","type-post","status-publish","format-standard","hentry","category-diary"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=2748"}],"version-history":[{"count":3,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2748\/revisions"}],"predecessor-version":[{"id":2750,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/2748\/revisions\/2750"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=2748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=2748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=2748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}