{"id":1158847,"date":"2025-01-13T18:45:36","date_gmt":"2025-01-13T10:45:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1158847.html"},"modified":"2025-01-13T18:45:39","modified_gmt":"2025-01-13T10:45:39","slug":"python%e5%a6%82%e4%bd%95%e5%81%9a%e5%88%86%e8%af%8d%e5%ae%9e%e9%aa%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1158847.html","title":{"rendered":"python\u5982\u4f55\u505a\u5206\u8bcd\u5b9e\u9a8c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25200712\/de45675b-2448-4950-b508-dcec06268b0b.webp\" alt=\"python\u5982\u4f55\u505a\u5206\u8bcd\u5b9e\u9a8c\" \/><\/p>\n<p><p> <strong>Python\u8fdb\u884c\u5206\u8bcd\u5b9e\u9a8c\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5de5\u5177\u5305\u3001\u5e94\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u501f\u52a9<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6a21\u578b\u3001\u5229\u7528\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u7b49\u3002<\/strong>\u5176\u4e2d\uff0c\u5e94\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5de5\u5177\u5305\u662f\u6700\u5e38\u89c1\u4e14\u65b9\u4fbf\u7684\u65b9\u5f0f\u4e4b\u4e00\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u5de5\u5177\u5305\u901a\u5e38\u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5206\u8bcd\u65b9\u6cd5\u548c\u9884\u8bad\u7ec3\u6a21\u578b\uff0c\u80fd\u591f\u6ee1\u8db3\u5927\u591a\u6570\u5206\u8bcd\u9700\u6c42\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5de5\u5177\u5305\u8fdb\u884c\u5206\u8bcd\u5b9e\u9a8c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5de5\u5177\u5305<\/h3>\n<\/p>\n<p><p>Python\u6709\u591a\u4e2a\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5de5\u5177\u5305\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662fNLTK\u3001spaCy\u548cjieba\u3002NLTK\u662f\u4e00\u4e2a\u529f\u80fd\u975e\u5e38\u5168\u9762\u7684\u5de5\u5177\u5305\uff0c\u9002\u5408\u5b66\u672f\u7814\u7a76\uff1bspaCy\u5219\u4ee5\u5176\u9ad8\u6548\u548c\u5f3a\u5927\u7684\u6027\u80fd\u8457\u79f0\uff1bjieba\u5219\u662f\u4e2d\u6587\u5206\u8bcd\u7684\u5229\u5668\u3002<\/p>\n<\/p>\n<p><h4>1.1 NLTK\u5206\u8bcd<\/h4>\n<\/p>\n<p><p>NLTK\uff08Natural Language Toolkit\uff09\u662f\u4e00\u4e2a\u5f3a\u5927\u7684Python\u5e93\uff0c\u7528\u4e8e\u5904\u7406\u548c\u5206\u6790\u4eba\u7c7b\u8bed\u8a00\u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u529f\u80fd\uff0c\u5305\u62ec\u5206\u8bcd\u3001\u8bcd\u5e72\u63d0\u53d6\u3001\u8bcd\u6027\u6807\u6ce8\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>nltk.download(&#39;punkt&#39;)<\/p>\n<p>from nltk.tokenize import word_tokenize<\/p>\n<p>text = &quot;This is a simple sentence for tokenization.&quot;<\/p>\n<p>tokens = word_tokenize(text)<\/p>\n<p>print(tokens)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u4e0b\u8f7d\u4e86NLTK\u7684punkt\u6a21\u578b\uff0c\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8e\u5206\u8bcd\u7684\u9884\u8bad\u7ec3\u6a21\u578b\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>word_tokenize<\/code>\u51fd\u6570\u5bf9\u6587\u672c\u8fdb\u884c\u5206\u8bcd\u3002<\/p>\n<\/p>\n<p><h4>1.2 spaCy\u5206\u8bcd<\/h4>\n<\/p>\n<p><p>spaCy\u662f\u4e00\u4e2a\u7528\u4e8e\u9ad8\u7ea7\u81ea\u7136\u8bed\u8a00\u5904\u7406\u7684\u5e93\uff0c\u5177\u6709\u5feb\u901f\u3001\u51c6\u786e\u548c\u6613\u7528\u7684\u7279\u70b9\u3002\u5b83\u652f\u6301\u591a\u79cd\u8bed\u8a00\u7684\u5206\u8bcd\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u9884\u8bad\u7ec3\u7684\u8bed\u8a00\u6a21\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import spacy<\/p>\n<p>nlp = spacy.load(&quot;en_core_web_sm&quot;)<\/p>\n<p>text = &quot;This is a simple sentence for tokenization.&quot;<\/p>\n<p>doc = nlp(text)<\/p>\n<p>tokens = [token.text for token in doc]<\/p>\n<p>print(tokens)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u52a0\u8f7d\u4e86\u4e00\u4e2a\u5c0f\u578b\u7684\u82f1\u8bed\u8bed\u8a00\u6a21\u578b<code>en_core_web_sm<\/code>\uff0c\u7136\u540e\u5c06\u6587\u672c\u4f20\u5165<code>nlp<\/code>\u5bf9\u8c61\u4e2d\u8fdb\u884c\u5904\u7406\uff0c\u6700\u7ec8\u901a\u8fc7\u904d\u5386<code>doc<\/code>\u5bf9\u8c61\u4e2d\u7684<code>token<\/code>\u751f\u6210\u5206\u8bcd\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h4>1.3 jieba\u5206\u8bcd<\/h4>\n<\/p>\n<p><p>jieba\u662f\u4e00\u4e2a\u4e2d\u6587\u5206\u8bcd\u5de5\u5177\uff0c\u5177\u6709\u7b80\u5355\u6613\u7528\u3001\u51c6\u786e\u7387\u9ad8\u7684\u7279\u70b9\uff0c\u652f\u6301\u4e09\u79cd\u5206\u8bcd\u6a21\u5f0f\uff1a\u7cbe\u786e\u6a21\u5f0f\u3001\u5168\u6a21\u5f0f\u548c\u641c\u7d22\u5f15\u64ce\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import jieba<\/p>\n<p>text = &quot;\u6211\u7231\u81ea\u7136\u8bed\u8a00\u5904\u7406\u3002&quot;<\/p>\n<p>tokens = jieba.lcut(text)<\/p>\n<p>print(tokens)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86jieba\u7684<code>lcut<\/code>\u51fd\u6570\u5bf9\u4e2d\u6587\u6587\u672c\u8fdb\u884c\u5206\u8bcd\uff0c\u8fd4\u56de\u4e00\u4e2a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5e94\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\uff08Regular Expression\uff09\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u6587\u672c\u5904\u7406\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u4e8e\u5404\u79cd\u6587\u672c\u5904\u7406\u4efb\u52a1\uff0c\u5305\u62ec\u5206\u8bcd\u3002\u867d\u7136\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0d\u5982\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5de5\u5177\u5305\u90a3\u4e48\u5f3a\u5927\uff0c\u4f46\u5728\u67d0\u4e9b\u7b80\u5355\u7684\u5206\u8bcd\u4efb\u52a1\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u4e5f\u80fd\u8d77\u5230\u5f88\u597d\u7684\u4f5c\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;This is a simple sentence for tokenization.&quot;<\/p>\n<p>tokens = re.findall(r&#39;\\b\\w+\\b&#39;, text)<\/p>\n<p>print(tokens)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f<code>\\b\\w+\\b<\/code>\u6765\u5339\u914d\u5355\u8bcd\u8fb9\u754c\u548c\u4e00\u4e2a\u6216\u591a\u4e2a\u5b57\u7b26\uff0c\u4ece\u800c\u5b9e\u73b0\u5bf9\u6587\u672c\u7684\u5206\u8bcd\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u501f\u52a9\u673a\u5668\u5b66\u4e60\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4f20\u7edf\u7684\u5206\u8bcd\u65b9\u6cd5\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u501f\u52a9\u673a\u5668\u5b66\u4e60\u6a21\u578b\u8fdb\u884c\u5206\u8bcd\u3002\u5e38\u89c1\u7684\u673a\u5668\u5b66\u4e60\u6a21\u578b\u5305\u62ec\u6761\u4ef6\u968f\u673a\u573a\uff08CRF\uff09\u3001\u652f\u6301\u5411\u91cf\u673a\uff08SVM\uff09\u7b49\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528CRF\u6a21\u578b\u8fdb\u884c\u5206\u8bcd\u7684\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><h4>3.1 CRF\u5206\u8bcd<\/h4>\n<\/p>\n<p><p>CRF\uff08Conditional Random Field\uff09\u662f\u4e00\u79cd\u5e38\u7528\u7684\u5e8f\u5217\u6807\u6ce8\u6a21\u578b\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u4e2d\uff0c\u5305\u62ec\u5206\u8bcd\u3001\u547d\u540d\u5b9e\u4f53\u8bc6\u522b\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5<code>sklearn-crfsuite<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install sklearn-crfsuite<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49\u7279\u5f81\u51fd\u6570\uff0c\u5e76\u8bad\u7ec3CRF\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sklearn_crfsuite<\/p>\n<p>from sklearn_crfsuite import metrics<\/p>\n<h2><strong>\u5b9a\u4e49\u7279\u5f81\u51fd\u6570<\/strong><\/h2>\n<p>def word2features(sent, i):<\/p>\n<p>    word = sent[i][0]<\/p>\n<p>    features = {<\/p>\n<p>        &#39;bias&#39;: 1.0,<\/p>\n<p>        &#39;word.lower()&#39;: word.lower(),<\/p>\n<p>        &#39;word[-3:]&#39;: word[-3:],<\/p>\n<p>        &#39;word[-2:]&#39;: word[-2:],<\/p>\n<p>        &#39;word.isupper()&#39;: word.isupper(),<\/p>\n<p>        &#39;word.istitle()&#39;: word.istitle(),<\/p>\n<p>        &#39;word.isdigit()&#39;: word.isdigit(),<\/p>\n<p>    }<\/p>\n<p>    if i &gt; 0:<\/p>\n<p>        word1 = sent[i-1][0]<\/p>\n<p>        features.update({<\/p>\n<p>            &#39;-1:word.lower()&#39;: word1.lower(),<\/p>\n<p>            &#39;-1:word.istitle()&#39;: word1.istitle(),<\/p>\n<p>            &#39;-1:word.isupper()&#39;: word1.isupper(),<\/p>\n<p>        })<\/p>\n<p>    else:<\/p>\n<p>        features[&#39;BOS&#39;] = True  # \u53e5\u5b50\u5f00\u59cb<\/p>\n<p>    if i &lt; len(sent)-1:<\/p>\n<p>        word1 = sent[i+1][0]<\/p>\n<p>        features.update({<\/p>\n<p>            &#39;+1:word.lower()&#39;: word1.lower(),<\/p>\n<p>            &#39;+1:word.istitle()&#39;: word1.istitle(),<\/p>\n<p>            &#39;+1:word.isupper()&#39;: word1.isupper(),<\/p>\n<p>        })<\/p>\n<p>    else:<\/p>\n<p>        features[&#39;EOS&#39;] = True  # \u53e5\u5b50\u7ed3\u675f<\/p>\n<p>    return features<\/p>\n<h2><strong>\u5c06\u53e5\u5b50\u8f6c\u6362\u4e3a\u7279\u5f81\u96c6<\/strong><\/h2>\n<p>def sent2features(sent):<\/p>\n<p>    return [word2features(sent, i) for i in range(len(sent))]<\/p>\n<h2><strong>\u5c06\u53e5\u5b50\u8f6c\u6362\u4e3a\u6807\u7b7e\u96c6<\/strong><\/h2>\n<p>def sent2labels(sent):<\/p>\n<p>    return [label for token, label in sent]<\/p>\n<h2><strong>\u8bad\u7ec3\u6570\u636e<\/strong><\/h2>\n<p>tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_sents = [<\/p>\n<p>    [(&#39;This&#39;, &#39;B&#39;), (&#39;is&#39;, &#39;I&#39;), (&#39;a&#39;, &#39;I&#39;), (&#39;simple&#39;, &#39;I&#39;), (&#39;sentence&#39;, &#39;I&#39;), (&#39;for&#39;, &#39;I&#39;), (&#39;tokenization&#39;, &#39;I&#39;)],<\/p>\n<p>    # \u6dfb\u52a0\u66f4\u591a\u8bad\u7ec3\u6570\u636e<\/p>\n<p>]<\/p>\n<p>X_train = [sent2features(s) for s in train_sents]<\/p>\n<p>y_train = [sent2labels(s) for s in train_sents]<\/p>\n<h2><strong>\u521d\u59cb\u5316\u548c\u8bad\u7ec3CRF\u6a21\u578b<\/strong><\/h2>\n<p>crf = sklearn_crfsuite.CRF(<\/p>\n<p>    algorithm=&#39;lbfgs&#39;,<\/p>\n<p>    c1=0.1,<\/p>\n<p>    c2=0.1,<\/p>\n<p>    max_iterations=100,<\/p>\n<p>    all_possible_transitions=True<\/p>\n<p>)<\/p>\n<p>crf.fit(X_train, y_train)<\/p>\n<h2><strong>\u6d4b\u8bd5\u6570\u636e<\/strong><\/h2>\n<p>test_sents = [<\/p>\n<p>    [(&#39;This&#39;, &#39;B&#39;), (&#39;is&#39;, &#39;I&#39;), (&#39;a&#39;, &#39;I&#39;), (&#39;test&#39;, &#39;I&#39;)],<\/p>\n<p>    # \u6dfb\u52a0\u66f4\u591a\u6d4b\u8bd5\u6570\u636e<\/p>\n<p>]<\/p>\n<p>X_test = [sent2features(s) for s in test_sents]<\/p>\n<p>y_test = [sent2labels(s) for s in test_sents]<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>y_pred = crf.predict(X_test)<\/p>\n<h2><strong>\u8bc4\u4f30<\/strong><\/h2>\n<p>print(metrics.flat_classification_report(y_test, y_pred))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u7279\u5f81\u51fd\u6570<code>word2features<\/code>\uff0c\u5b83\u7528\u4e8e\u5c06\u6bcf\u4e2a\u5355\u8bcd\u8f6c\u6362\u4e3a\u7279\u5f81\u96c6\u3002\u7136\u540e\uff0c\u6211\u4eec\u5c06\u53e5\u5b50\u8f6c\u6362\u4e3a\u7279\u5f81\u96c6\u548c\u6807\u7b7e\u96c6\uff0c\u5e76\u8bad\u7ec3CRF\u6a21\u578b\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u5bf9\u6d4b\u8bd5\u6570\u636e\u8fdb\u884c\u9884\u6d4b\uff0c\u5e76\u8bc4\u4f30\u6a21\u578b\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5229\u7528\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u4e2d\u8868\u73b0\u51fa\u8272\uff0c\u5c24\u5176\u662f\u5728\u5206\u8bcd\u4efb\u52a1\u4e2d\u3002\u5e38\u7528\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u5305\u62ec\u5faa\u73af\u795e\u7ecf\u7f51\u7edc\uff08RNN\uff09\u3001\u957f\u77ed\u671f\u8bb0\u5fc6\u7f51\u7edc\uff08LSTM\uff09\u3001\u53cc\u5411LSTM\uff08BiLSTM\uff09\u548c\u6761\u4ef6\u968f\u673a\u573a\uff08CRF\uff09\u7684\u7ed3\u5408\u3002<\/p>\n<\/p>\n<p><h4>4.1 BiLSTM-CRF\u5206\u8bcd<\/h4>\n<\/p>\n<p><p>BiLSTM-CRF\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u5e8f\u5217\u6807\u6ce8\u6a21\u578b\uff0c\u5b83\u7ed3\u5408\u4e86\u53cc\u5411LSTM\u548cCRF\uff0c\u80fd\u591f\u6355\u6349\u4e0a\u4e0b\u6587\u4fe1\u606f\uff0c\u5e76\u8fdb\u884c\u5168\u5c40\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5<code>tensorflow<\/code>\u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tensorflow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5b9a\u4e49BiLSTM-CRF\u6a21\u578b\uff0c\u5e76\u8fdb\u884c\u8bad\u7ec3\u548c\u9884\u6d4b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>from tensorflow.keras.layers import Input, Embedding, LSTM, Dense, Bidirectional, TimeDistributed<\/p>\n<p>from tensorflow.keras.models import Model<\/p>\n<p>from tensorflow.keras_contrib.layers import CRF<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u8bad\u7ec3\u6570\u636e<\/strong><\/h2>\n<p>sentences = [<\/p>\n<p>    [&quot;This&quot;, &quot;is&quot;, &quot;a&quot;, &quot;simple&quot;, &quot;sentence&quot;, &quot;.&quot;],<\/p>\n<p>    [&quot;Another&quot;, &quot;sentence&quot;, &quot;for&quot;, &quot;testing&quot;, &quot;.&quot;],<\/p>\n<p>    # \u6dfb\u52a0\u66f4\u591a\u8bad\u7ec3\u6570\u636e<\/p>\n<p>]<\/p>\n<p>labels = [<\/p>\n<p>    [&quot;O&quot;, &quot;O&quot;, &quot;O&quot;, &quot;B-NP&quot;, &quot;I-NP&quot;, &quot;O&quot;],<\/p>\n<p>    [&quot;O&quot;, &quot;B-NP&quot;, &quot;O&quot;, &quot;B-VP&quot;, &quot;O&quot;],<\/p>\n<p>    # \u6dfb\u52a0\u66f4\u591a\u6807\u7b7e<\/p>\n<p>]<\/p>\n<h2><strong>\u6784\u5efa\u8bcd\u6c47\u8868\u548c\u6807\u7b7e\u8868<\/strong><\/h2>\n<p>word2idx = {w: i + 2 for i, w in enumerate(set(sum(sentences, [])))}<\/p>\n<p>word2idx[&quot;UNK&quot;] = 1<\/p>\n<p>word2idx[&quot;PAD&quot;] = 0<\/p>\n<p>tag2idx = {t: i + 1 for i, t in enumerate(set(sum(labels, [])))}<\/p>\n<p>tag2idx[&quot;PAD&quot;] = 0<\/p>\n<h2><strong>\u5c06\u53e5\u5b50\u548c\u6807\u7b7e\u8f6c\u6362\u4e3a\u7d22\u5f15<\/strong><\/h2>\n<p>X = [[word2idx[w] for w in s] for s in sentences]<\/p>\n<p>y = [[tag2idx[t] for t in l] for l in labels]<\/p>\n<h2><strong>\u7531\u4e8eLSTM\u9700\u8981\u56fa\u5b9a\u8f93\u5165\u957f\u5ea6\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u53e5\u5b50\u8fdb\u884c\u586b\u5145<\/strong><\/h2>\n<p>from tensorflow.keras.preprocessing.sequence import pad_sequences<\/p>\n<p>maxlen = max([len(s) for s in sentences])<\/p>\n<p>X = pad_sequences(X, maxlen=maxlen, padding=&#39;post&#39;)<\/p>\n<p>y = pad_sequences(y, maxlen=maxlen, padding=&#39;post&#39;)<\/p>\n<h2><strong>\u5c06\u6807\u7b7e\u8f6c\u6362\u4e3aone-hot\u7f16\u7801<\/strong><\/h2>\n<p>from tensorflow.keras.utils import to_categorical<\/p>\n<p>y = [to_categorical(i, num_classes=len(tag2idx) + 1) for i in y]<\/p>\n<h2><strong>\u5b9a\u4e49BiLSTM-CRF\u6a21\u578b<\/strong><\/h2>\n<p>input = Input(shape=(maxlen,))<\/p>\n<p>model = Embedding(input_dim=len(word2idx), output_dim=50, input_length=maxlen)(input)<\/p>\n<p>model = Bidirectional(LSTM(units=100, return_sequences=True, recurrent_dropout=0.1))(model)<\/p>\n<p>model = TimeDistributed(Dense(50, activation=&quot;relu&quot;))(model)<\/p>\n<p>crf = CRF(len(tag2idx) + 1)<\/p>\n<p>out = crf(model)<\/p>\n<p>model = Model(input, out)<\/p>\n<p>model.compile(optimizer=&quot;adam&quot;, loss=crf.loss_function, metrics=[crf.accuracy])<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(X, np.array(y), batch_size=32, epochs=5, validation_split=0.1, verbose=1)<\/p>\n<h2><strong>\u6d4b\u8bd5\u6a21\u578b<\/strong><\/h2>\n<p>test_sentence = [&quot;This&quot;, &quot;is&quot;, &quot;a&quot;, &quot;test&quot;]<\/p>\n<p>test_sentence = pad_sequences([[word2idx.get(w, 1) for w in test_sentence]], maxlen=maxlen, padding=&#39;post&#39;)<\/p>\n<p>predictions = model.predict(test_sentence)<\/p>\n<p>predictions = np.argmax(predictions, axis=-1)<\/p>\n<h2><strong>\u6253\u5370\u9884\u6d4b\u7ed3\u679c<\/strong><\/h2>\n<p>for word, pred in zip(test_sentence[0], predictions[0]):<\/p>\n<p>    if word != 0:<\/p>\n<p>        print(f&quot;{list(word2idx.keys())[list(word2idx.values()).index(word)]}: {list(tag2idx.keys())[pred]}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u8bcd\u6c47\u8868\u548c\u6807\u7b7e\u8868\uff0c\u5e76\u5c06\u53e5\u5b50\u548c\u6807\u7b7e\u8f6c\u6362\u4e3a\u7d22\u5f15\u3002\u7136\u540e\uff0c\u6211\u4eec\u5bf9\u53e5\u5b50\u8fdb\u884c\u586b\u5145\uff0c\u4ee5\u6ee1\u8db3LSTM\u7684\u8f93\u5165\u8981\u6c42\u3002\u63a5\u7740\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86BiLSTM-CRF\u6a21\u578b\uff0c\u5e76\u8fdb\u884c\u8bad\u7ec3\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u8bad\u7ec3\u597d\u7684\u6a21\u578b\u5bf9\u6d4b\u8bd5\u53e5\u5b50\u8fdb\u884c\u9884\u6d4b\uff0c\u5e76\u6253\u5370\u9884\u6d4b\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86Python\u8fdb\u884c\u5206\u8bcd\u5b9e\u9a8c\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5de5\u5177\u5305\u3001\u5e94\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u501f\u52a9\u673a\u5668\u5b66\u4e60\u6a21\u578b\u548c\u5229\u7528\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u8fdb\u884c\u5206\u8bcd\u5b9e\u9a8c\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u591f\u5bf9\u60a8\u8fdb\u884c\u5206\u8bcd\u5b9e\u9a8c\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5206\u8bcd\u5b9e\u9a8c\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728\u8fdb\u884cPython\u5206\u8bcd\u5b9e\u9a8c\u65f6\uff0c\u901a\u5e38\u9700\u8981\u9075\u5faa\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a\u9996\u5148\uff0c\u9009\u62e9\u5408\u9002\u7684\u5206\u8bcd\u5e93\uff0c\u5982jieba\u6216nltk\uff0c\u7136\u540e\u51c6\u5907\u5f85\u5206\u8bcd\u7684\u6587\u672c\u6570\u636e\u3002\u63a5\u7740\uff0c\u4f7f\u7528\u6240\u9009\u5e93\u7684\u5206\u8bcd\u529f\u80fd\u5bf9\u6587\u672c\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u53ef\u4ee5\u9009\u62e9\u8f93\u51fa\u5206\u8bcd\u7ed3\u679c\u7684\u65b9\u5f0f\uff0c\u4f8b\u5982\u6253\u5370\u5230\u63a7\u5236\u53f0\u6216\u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bf9\u5206\u8bcd\u7ed3\u679c\u8fdb\u884c\u7edf\u8ba1\u5206\u6790\uff0c\u8bc4\u4f30\u5206\u8bcd\u6548\u679c\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8fdb\u884c\u5206\u8bcd\u65f6\uff0c\u5e38\u89c1\u7684\u5e93\u6709\u54ea\u4e9b\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u6d41\u884c\u7684\u5206\u8bcd\u5e93\u53ef\u4f9b\u9009\u62e9\uff0c\u6700\u5e38\u7528\u7684\u5305\u62ecjieba\u3001nltk\u3001spaCy\u548cThulac\u3002jieba\u4e3b\u8981\u7528\u4e8e\u4e2d\u6587\u5206\u8bcd\uff0c\u652f\u6301\u591a\u79cd\u5206\u8bcd\u6a21\u5f0f\uff1bnltk\u548cspaCy\u5219\u9002\u7528\u4e8e\u82f1\u6587\u53ca\u591a\u79cd\u5176\u4ed6\u8bed\u8a00\uff0c\u63d0\u4f9b\u4e30\u5bcc\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u529f\u80fd\uff1bThulac\u540c\u6837\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u4e2d\u6587\u5206\u8bcd\u5de5\u5177\uff0c\u9002\u5408\u5927\u89c4\u6a21\u6587\u672c\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bc4\u4f30\u5206\u8bcd\u7ed3\u679c\u7684\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u8bc4\u4f30\u5206\u8bcd\u7ed3\u679c\u7684\u51c6\u786e\u6027\u53ef\u4ee5\u901a\u8fc7\u5bf9\u6bd4\u5206\u8bcd\u7ed3\u679c\u4e0e\u4eba\u5de5\u6807\u6ce8\u7684\u6807\u51c6\u7b54\u6848\u6765\u8fdb\u884c\u3002\u53ef\u4ee5\u8ba1\u7b97\u5206\u8bcd\u7684\u7cbe\u786e\u7387\u3001\u53ec\u56de\u7387\u548cF1\u5206\u6570\u7b49\u6307\u6807\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u5206\u8bcd\u540e\u7684\u8bcd\u9891\u5206\u5e03\u3001\u8bcd\u6027\u6807\u6ce8\u548c\u4e0a\u4e0b\u6587\u5173\u7cfb\u6765\u5224\u65ad\u5206\u8bcd\u7684\u5408\u7406\u6027\uff0c\u5fc5\u8981\u65f6\u5bf9\u5206\u8bcd\u7b97\u6cd5\u8fdb\u884c\u8c03\u6574\u4ee5\u63d0\u9ad8\u5206\u8bcd\u8d28\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8fdb\u884c\u5206\u8bcd\u5b9e\u9a8c\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5de5\u5177\u5305\u3001\u5e94\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u501f\u52a9\u673a\u5668\u5b66\u4e60\u6a21\u578b\u3001\u5229\u7528\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1158854,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158847"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1158847"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158847\/revisions"}],"predecessor-version":[{"id":1158855,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1158847\/revisions\/1158855"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1158854"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1158847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1158847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1158847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}