{"id":1315,"date":"2023-03-25T10:12:45","date_gmt":"2023-03-25T02:12:45","guid":{"rendered":""},"modified":"2023-03-25T10:12:45","modified_gmt":"2023-03-25T02:12:45","slug":"NLTK\u5305","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1315.html","title":{"rendered":"NLTK\u5305"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

NLTK\u5305<\/h1>\n

NLTK\u5305\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u5728\u672c\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u5f00\u59cb\u4f7f\u7528\u81ea\u7136\u8bed\u8a00\u5de5\u5177\u5305(\u8f6f\u4ef6\u5305)\u3002\n <\/div>\n
\n \u524d\u63d0\u6761\u4ef6<\/strong>\n <\/div>\n
\n \u5982\u679c\u60f3\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\u6765\u6784\u5efa\u5e94\u7528\u7a0b\u5e8f\uff0c\u90a3\u4e48\u4e0a\u4e0b\u6587\u4e2d\u7684\u53d8\u5316\u5c31\u4f1a\u4f7f\u5176\u53d8\u5f97\u975e\u5e38\u56f0\u96be\u3002 \u8bed\u5883\u56e0\u7d20\u5f71\u54cd\u673a\u5668\u5982\u4f55\u7406\u89e3\u7279\u5b9a\u53e5\u5b50\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u901a\u8fc7\u4f7f\u7528\u673a\u5668\u5b66\u4e60\u65b9\u6cd5\u6765\u5f00\u53d1\u81ea\u7136\u8bed\u8a00\u5e94\u7528\u7a0b\u5e8f\uff0c\u4ee5\u4fbf\u673a\u5668\u4e5f\u80fd\u591f\u7406\u89e3\u4eba\u7c7b\u53ef\u4ee5\u7406\u89e3\u4e0a\u4e0b\u6587\u7684\u65b9\u5f0f\u3002\n <\/div>\n
\n \u8981\u6784\u5efa\u8fd9\u6837\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u6211\u4eec\u5c06\u4f7f\u7528\u540d\u4e3aNLTK(\u81ea\u7136\u8bed\u8a00\u5de5\u5177\u5305\u5305)\u7684Python\u5305\u3002\n <\/div>\n

<\/span>\u5bfc\u5165NLTK<\/h2>\n
\n \u5728\u4f7f\u7528\u4e4b\u524d\u9700\u8981\u5b89\u88c5NLTK\uff0c\u5b83\u53ef\u4ee5\u5728\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
pip install nltk
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8981\u4e3aNLTK\u6784\u5efaconda\u5305\uff0c\u8bf7\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
conda install -c anaconda nltk
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\u5b89\u88c5NLTK\u5305\u540e\uff0c\u9700\u8981\u901a\u8fc7python\u547d\u4ee4\u63d0\u793a\u7b26\u5bfc\u5165\u3002\u901a\u8fc7\u5728Python\u547d\u4ee4\u63d0\u793a\u7b26\u4e0b\u7f16\u5199\u4ee5\u4e0b\u547d\u4ee4\u6765\u5bfc\u5165\u5b83 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
>>> import <\/span>nltk
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4e0b\u8f7dNLTK\u7684\u6570\u636e<\/strong>\n <\/div>\n
\n \u73b0\u5728\u5bfc\u5165NLTK\u540e\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4e0b\u8f7d\u6240\u9700\u7684\u6570\u636e\u3002 \u5b83\u53ef\u4ee5\u5728Python\u547d\u4ee4\u63d0\u793a\u7b26\u4e0b\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u5b8c\u6210 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
>>> nltk.download<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b89\u88c5\u5176\u4ed6\u5fc5\u9700\u7684\u8f6f\u4ef6\u5305<\/strong>\n <\/div>\n
\n \u4e3a\u4e86\u4f7f\u7528NLTK\u6784\u5efa\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\uff0c\u9700\u8981\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\u5305\u3002\u5982\u4e0b -\n <\/div>\n
\n gensim<\/strong>\n <\/div>\n
\n \u5b83\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u8bed\u4e49\u5efa\u6a21\u5e93\uff0c\u5bf9\u8bb8\u591a\u5e94\u7528\u7a0b\u5e8f\u5f88\u6709\u7528\u3002\u53ef\u4ee5\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5b83 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
pip install gensim
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n pattern<\/strong>\n <\/div>\n
\n \u5b83\u7528\u4e8e\u4f7fgensim\u5305\u6b63\u5e38\u5de5\u4f5c\u3002\u53ef\u4ee5\u901a\u8fc7\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\u5b83 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
pip install pattern
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u6807\u8bb0\u5316\uff0c\u8bcd\u5e72\u5316\u548c\u8bcd\u5f62\u5316\u7684\u6982\u5ff5<\/h2>\n
\n \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4e86\u89e3\u4ec0\u4e48\u662f\u6807\u8bb0\u5316\uff0c\u8bcd\u5e72\u5316\u548c\u8bcd\u5f62\u5316\u3002\n <\/div>\n

1. \u6807\u8bb0\u5316<\/strong><\/h2>\n
\n \u5b83\u53ef\u4ee5\u88ab\u5b9a\u4e49\u4e3a\u5c06\u7ed9\u5b9a\u6587\u672c\u5373\u5b57\u7b26\u5e8f\u5217\u5206\u6210\u79f0\u4e3a\u4ee4\u724c\u7684\u8f83\u5c0f\u5355\u5143\u7684\u8fc7\u7a0b\u3002\u4ee4\u724c\u53ef\u4ee5\u662f\u5355\u8bcd\uff0c\u6570\u5b57\u6216\u6807\u70b9\u7b26\u53f7\u3002 \u5b83\u4e5f\u88ab\u79f0\u4e3a\u5206\u8bcd\u3002 \u4ee5\u4e0b\u662f\u6807\u8bb0\u5316\u7684\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b -\n <\/div>\n
\n \u8f93\u5165<\/strong> - \u8292\u679c\uff0c\u9999\u8549\uff0c\u83e0\u841d\u548c\u82f9\u679c\u90fd\u662f\u6c34\u679c\u3002\n <\/div>\n
\n \u8f93\u51fa<\/strong> -\n <\/div>\n
\n NLTK\u5305_https:\/\/bianchenghao6.com_\u3010\u4eba\u5de5\u667a\u80fd\u6559\u7a0b\u3011_\u7b2c1\u5f20\n <\/div>\n
\n \u6253\u65ad\u7ed9\u5b9a\u6587\u672c\u7684\u8fc7\u7a0b\u53ef\u4ee5\u901a\u8fc7\u67e5\u627e\u5355\u8bcd\u8fb9\u754c\u6765\u5b8c\u6210\u3002 \u5355\u8bcd\u7684\u7ed3\u5c3e\u548c\u65b0\u5355\u8bcd\u7684\u5f00\u5934\u79f0\u4e3a\u5355\u8bcd\u8fb9\u754c\u3002 \u6587\u5b57\u7684\u4e66\u5199\u4f53\u7cfb\u548c\u5370\u5237\u7ed3\u6784\u4f1a\u5f71\u54cd\u8fb9\u754c\u3002\n <\/div>\n
\n \u5728Python NLTK\u6a21\u5757\u4e2d\uff0c\u6709\u4e0e\u6807\u8bb0\u5316\u6709\u5173\u7684\u4e0d\u540c\u5305\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u5c06\u6587\u672c\u5212\u5206\u4e3a\u6807\u8bb0\u3002 \u4e00\u4e9b\u8f6f\u4ef6\u5305\u5982\u4e0b\u6240\u793a -\n <\/div>\n
\n sent_tokenize\u5305<\/strong>\n <\/div>\n
\n \u987e\u540d\u601d\u4e49\uff0c\u8fd9\u4e2a\u8f6f\u4ef6\u5305\u4f1a\u5c06\u8f93\u5165\u6587\u672c\u5206\u6210\u51e0\u4e2a\u53e5\u5b50\u3002 \u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684Python\u4ee3\u7801\u5bfc\u5165\u8fd9\u4e2a\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>nltk.tokenize import <\/span>sent_tokenize
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n word_tokenize\u5305<\/strong>\n <\/div>\n
\n \u8fd9\u4e2a\u5305\u5c06\u8f93\u5165\u6587\u672c\u5206\u6210\u5355\u8bcd\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684Python\u4ee3\u7801\u6765\u5bfc\u5165\u8fd9\u4e2a\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>nltk.tokenize import <\/span>word_tokenize
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n WordPuncttokenizer\u5305<\/strong>\n <\/div>\n
\n \u8fd9\u4e2a\u5305\u5c06\u8f93\u5165\u6587\u672c\u5206\u6210\u5355\u8bcd\u548c\u6807\u70b9\u7b26\u53f7\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684Python\u4ee3\u7801\u6765\u5bfc\u5165\u8fd9\u4e2a\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>nltk.tokenize import <\/span>WordPuncttokenizer
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/span>2. \u8bcd\u5e72<\/h3>\n
\n \u5728\u5904\u7406\u6587\u5b57\u65f6\uff0c\u7531\u4e8e\u8bed\u6cd5\u539f\u56e0\uff0c\u6211\u4eec\u9047\u5230\u4e86\u5f88\u591a\u53d8\u5316\u3002 \u8fd9\u91cc\u7684\u53d8\u5316\u7684\u6982\u5ff5\u610f\u5473\u7740\u5fc5\u987b\u5904\u7406\u50cf:
\n democracy<\/em>,
\n democratic<\/em> \u548c
\n democratization<\/em> \u7b49\u4e0d\u540c\u5f62\u5f0f\u7684\u76f8\u540c\u8bcd\u6c47\u3002\u673a\u5668\u975e\u5e38\u9700\u8981\u7406\u89e3\u8fd9\u4e9b\u4e0d\u540c\u7684\u5355\u8bcd\u5177\u6709\u76f8\u540c\u7684\u57fa\u672c\u5f62\u5f0f\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u5728\u5206\u6790\u6587\u672c\u7684\u540c\u65f6\u63d0\u53d6\u5355\u8bcd\u7684\u57fa\u672c\u5f62\u5f0f\u5c06\u4f1a\u5f88\u6709\u7528\u3002\n <\/div>\n
\n \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u963b\u6b62\u6765\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8bf4\u5e72\u6270\u662f\u901a\u8fc7\u5207\u65ad\u5355\u8bcd\u7684\u7ed3\u5c3e\u6765\u63d0\u53d6\u5355\u8bcd\u57fa\u672c\u5f62\u5f0f\u7684\u542f\u53d1\u5f0f\u8fc7\u7a0b\u3002\n <\/div>\n
\n \u5728Python NLTK\u6a21\u5757\u4e2d\uff0c\u6709\u4e00\u4e9b\u4e0estemming\u76f8\u5173\u7684\u5176\u5b83\u5305\u3002 \u8fd9\u4e9b\u5305\u53ef\u4ee5\u7528\u6765\u83b7\u53d6\u5355\u8bcd\u7684\u57fa\u672c\u5f62\u5f0f\u3002 \u8fd9\u4e9b\u8f6f\u4ef6\u5305\u4f7f\u7528\u7b97\u6cd5\u3002 \u4e00\u4e9b\u8f6f\u4ef6\u5305\u5982\u4e0b\u6240\u793a -\n <\/div>\n
\n PorterStemmer\u5305<\/strong>\n <\/div>\n
\n \u8fd9\u4e2aPython\u5305\u4f7f\u7528Porter\u7b97\u6cd5\u6765\u63d0\u53d6\u57fa\u7840\u8868\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684Python\u4ee3\u7801\u6765\u8fd9\u4e2a\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>nltk.stem.porter import <\/span>PorterStemmer
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4f8b\u5982\uff0c\u5982\u679c\u5c06writing\u8fd9\u4e2a\u8bcd\u4f5c\u4e3a\u8fd9\u4e2a\u8bcd\u5e72\u7684\u8f93\u5165\uff0c\u5b83\u4eec\u5c31\u4f1a\u5728\u8bcd\u5e72\u4e4b\u540e\u5f97\u5230write\u8fd9\u4e2a\u8bcd\u3002\n <\/div>\n
\n LancasterStemmer\u5305<\/strong>\n <\/div>\n
\n \u8fd9\u4e2aPython\u5305\u5c06\u4f7f\u7528Lancaster\u7684\u7b97\u6cd5\u6765\u63d0\u53d6\u57fa\u672c\u5f62\u5f0f\u3002 \u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684Python\u4ee3\u7801\u6765\u5bfc\u5165\u8fd9\u4e2a\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>nltk.stem.lancaster import <\/span>LancasterStemmer
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4f8b\u5982\uff0c\u5982\u679c\u5c06writing\u8fd9\u4e2a\u8bcd\u4f5c\u4e3a\u8fd9\u4e2a\u8bcd\u5e72\u7684\u8f93\u5165\uff0c\u5b83\u4eec\u5c31\u4f1a\u5728\u8bcd\u5e72\u4e4b\u540e\u5f97\u5230write\u8fd9\u4e2a\u8bcd\u3002\n <\/div>\n
\n SnowballStemmer\u5305<\/strong>\n <\/div>\n
\n \u8fd9\u4e2aPython\u5305\u5c06\u4f7f\u7528\u96ea\u7403\u7b97\u6cd5\u6765\u63d0\u53d6\u57fa\u672c\u5f62\u5f0f\u3002 \u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684Python\u4ee3\u7801\u6765\u5bfc\u5165\u8fd9\u4e2a\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>nltk.stem.snowball import <\/span>SnowballStemmer
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4f8b\u5982\uff0c\u5982\u679c\u5c06writing\u8fd9\u4e2a\u8bcd\u4f5c\u4e3a\u8fd9\u4e2a\u8bcd\u5e72\u7684\u8f93\u5165\uff0c\u5b83\u4eec\u5c31\u4f1a\u5728\u8bcd\u5e72\u4e4b\u540e\u5f97\u5230write\u8fd9\u4e2a\u8bcd\u3002\n <\/div>\n
\n \u6240\u6709\u8fd9\u4e9b\u7b97\u6cd5\u90fd\u6709\u4e0d\u540c\u7a0b\u5ea6\u7684\u4e25\u683c\u6027\u3002 \u5982\u679c\u6bd4\u8f83\u8fd9\u4e09\u4e2a\u8bcd\u5e72\u7684\u8bdd\uff0c\u90a3\u4e48\u6ce2\u7279\u8bcd\u5e72\u662f\u6700\u4e0d\u4e25\u683c\u7684\uff0c\u5170\u5361\u65af\u7279\u8bcd\u5e72\u662f\u6700\u4e25\u683c\u7684\u3002 \u96ea\u7403\u8bcd\u5e72\u5728\u901f\u5ea6\u548c\u4e25\u683c\u6027\u65b9\u9762\u90fd\u5f88\u597d\u7528\u3002\n <\/div>\n
\n \u8bcd\u5f62\u8fd8\u539f<\/strong>\n <\/div>\n
\n \u4e5f\u53ef\u4ee5\u901a\u8fc7\u8bcd\u5f62\u5316\u6765\u63d0\u53d6\u5355\u8bcd\u7684\u57fa\u672c\u5f62\u5f0f\u3002 \u5b83\u57fa\u672c\u4e0a\u901a\u8fc7\u4f7f\u7528\u8bcd\u6c47\u7684\u8bcd\u6c47\u548c\u5f62\u6001\u5206\u6790\u6765\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\uff0c\u901a\u5e38\u65e8\u5728\u4ec5\u5220\u9664\u53d8\u5143\u7ed3\u5c3e\u3002 \u4efb\u4f55\u5355\u8bcd\u7684\u8fd9\u79cd\u57fa\u672c\u5f62\u5f0f\u90fd\u79f0\u4e3a\u5f15\u7406\u3002\n <\/div>\n
\n \u8bcd\u5e72\u5316\u548c\u8bcd\u6027\u5316\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u8bcd\u6c47\u7684\u4f7f\u7528\u548c\u5f62\u6001\u5206\u6790\u3002 \u53e6\u4e00\u4e2a\u533a\u522b\u662f\uff0c\u8bcd\u5e72\u6700\u5e38\u89c1\u7684\u662f\u5d29\u6e83\u6d3e\u751f\u76f8\u5173\u7684\u8bcd\u6c47\uff0c\u800c\u8bcd\u7d20\u5316\u901a\u5e38\u53ea\u4f1a\u6298\u62e2\u5f15\u7406\u7684\u4e0d\u540c\u7684\u6298\u70b9\u5f62\u5f0f\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u63d0\u4f9b\u5355\u8bcdsaw\u4f5c\u4e3a\u8f93\u5165\u8bcd\uff0c\u90a3\u4e48\u8bcd\u5e72\u53ef\u80fd\u4f1a\u8fd4\u56de\u5355\u8bcd's'\uff0c\u4f46\u8bcd\u5f62\u5316\u4f1a\u5c1d\u8bd5\u8fd4\u56de\u5355\u8bcd\uff0c\u770b\u770b\u6216\u770b\u5230\u53d6\u51b3\u4e8e\u4f7f\u7528\u8be5\u5355\u8bcd\u662f\u52a8\u8bcd\u8fd8\u662f\u540d\u8bcd\u3002\n <\/div>\n
\n \u5728Python NLTK\u6a21\u5757\u4e2d\uff0c\u6709\u4ee5\u4e0b\u4e0e\u8bcd\u5f62\u5316\u8fc7\u7a0b\u6709\u5173\u7684\u5305\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u83b7\u53d6\u8bcd\u7684\u57fa\u672c\u5f62\u5f0f -\n <\/div>\n
\n WordNetLemmatizer\u5305<\/strong>\n <\/div>\n
\n \u8fd9\u4e2aPython\u5305\u5c06\u63d0\u53d6\u5355\u8bcd\u7684\u57fa\u672c\u5f62\u5f0f\uff0c\u53d6\u51b3\u4e8e\u5b83\u662f\u7528\u4f5c\u540d\u8bcd\u8fd8\u662f\u52a8\u8bcd\u3002 \u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684Python\u4ee3\u7801\u7684\u6765\u5bfc\u5165\u8fd9\u4e2a\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>nltk.stem import <\/span>WordNetLemmatizer
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5757\u5316:\u5c06\u6570\u636e\u5206\u5272\u6210\u5757<\/h2>\n
\n \u5b83\u662f\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u7684\u91cd\u8981\u8fc7\u7a0b\u4e4b\u4e00\u3002 \u5206\u5757\u7684\u4e3b\u8981\u5de5\u4f5c\u662f\u8bc6\u522b\u8bcd\u7c7b\u548c\u77ed\u8bed\uff0c\u5982\u540d\u8bcd\u77ed\u8bed\u3002 \u6211\u4eec\u5df2\u7ecf\u7814\u7a76\u4e86\u4ee4\u724c\u5316\u7684\u8fc7\u7a0b\uff0c\u5373\u4ee4\u724c\u7684\u521b\u5efa\u3002 \u5206\u5757\u57fa\u672c\u4e0a\u5c31\u662f\u8fd9\u4e9b\u4ee4\u724c\u7684\u6807\u7b7e\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u7ec4\u5757\u4f1a\u544a\u8bc9\u6211\u4eec\u53e5\u5b50\u7684\u7ed3\u6784\u3002\n <\/div>\n
\n \u5728\u4e0b\u9762\u7684\u7ae0\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u5b66\u4e60\u4e0d\u540c\u7c7b\u578b\u7684\u5206\u5757\u3002\n <\/div>\n

\u7ec4\u5757\u7684\u7c7b\u578b<\/h2>\n
\n \u6709\u4e24\u79cd\u7c7b\u578b\u7684\u7ec4\u5757\u3002 \u7c7b\u578b\u5982\u4e0b -\n <\/div>\n

<\/p>\n

\n \u4e0a\u5206\u5757<\/strong>\u5728\u8fd9\u4e2a\u7ec4\u5757\u8fc7\u7a0b\u4e2d\uff0c\u5bf9\u8c61\uff0c\u4e8b\u7269\u7b49\u5411\u66f4\u666e\u904d\u7684\u65b9\u5411\u53d1\u5c55\uff0c\u8bed\u8a00\u53d8\u5f97\u66f4\u52a0\u62bd\u8c61\u3002 \u6709\u66f4\u591a\u7684\u534f\u8bae\u673a\u4f1a\u3002 \u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u7f29\u5c0f\u3002 \u4f8b\u5982\uff0c\u5982\u679c\u5c06\u201c\u6c7d\u8f66\u662f\u4e3a\u4e86\u4ec0\u4e48\u76ee\u7684\u201d\u8fd9\u4e2a\u95ee\u9898\u5927\u8086\u6e32\u67d3\uff1f\u6211\u4eec\u53ef\u80fd\u4f1a\u5f97\u5230\u7b54\u6848\u662f:\u201c\u8fd0\u8f93\u201d\u3002\n <\/div>\n

<\/span>
\n <\/p>\n

\n \u4e0b\u5206\u5757<\/strong>\u5728\u8fd9\u4e2a\u7ec4\u5757\u8fc7\u7a0b\u4e2d\uff0c\u5bf9\u8c61\uff0c\u4e8b\u7269\u7b49\u671d\u7740\u66f4\u5177\u4f53\u7684\u65b9\u5411\u53d1\u5c55\uff0c\u8bed\u8a00\u66f4\u52a0\u6e17\u900f\u3002 \u66f4\u6df1\u5c42\u6b21\u7684\u7ed3\u6784\u5c06\u8fdb\u884c\u4ed4\u7ec6\u68c0\u67e5\u3002 \u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\u4f1a\u653e\u5927\u3002\u4f8b\u5982\uff0c\u5982\u679c\u5c06\u201c\u4e13\u95e8\u8bb2\u8ff0\u4e00\u8f86\u6c7d\u8f66\u201d\u8fd9\u4e2a\u95ee\u9898\u5f52\u7eb3\u8d77\u6765\uff1f \u4f1a\u5f97\u5230\u5173\u4e8e\u6c7d\u8f66\u7684\u66f4\u5c0f\u7684\u4fe1\u606f\u3002\n <\/div>\n

<\/span> <\/p>\n

\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u4f7f\u7528Python\u4e2d\u7684NLTK\u6a21\u5757\u6765\u8fdb\u884cNoun-Phrase chunking\uff0c\u8fd9\u662f\u4e00\u79cdchunking\u7c7b\u522b\uff0c\u5b83\u53ef\u4ee5\u5728\u53e5\u5b50\u4e2d\u627e\u5230\u540d\u8bcd\u77ed\u8bed\u5757\uff0c\n <\/div>\n
\n \u5728python\u4e2d\u6267\u884c\u8fd9\u4e9b\u6b65\u9aa4\u6765\u5b9e\u73b0\u540d\u8bcd\u77ed\u8bed\u5206\u5757 -\n <\/div>\n
\n \u7b2c1\u6b65<\/strong> - \u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u9700\u8981\u5b9a\u4e49\u5206\u5757\u7684\u8bed\u6cd5\u3002 \u5b83\u5c06\u5305\u542b\u9700\u8981\u9075\u5faa\u7684\u89c4\u5219\u3002
\n \u7b2c2\u6b65<\/strong> - \u5728\u8fd9\u4e00\u6b65\u4e2d\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5757\u89e3\u6790\u5668\u3002 \u5b83\u4f1a\u89e3\u6790\u8bed\u6cd5\u5e76\u7ed9\u51fa\u7ed3\u679c\u3002
\n \u7b2c3\u6b65<\/strong> - \u5728\u6700\u540e\u4e00\u6b65\u4e2d\uff0c\u8f93\u51fa\u4ee5\u6811\u683c\u5f0f\u751f\u6210\u3002\n <\/div>\n
\n \u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u5bfc\u5165\u5fc5\u8981\u7684NLTK\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
import <\/span>nltk
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u8fd9\u4e2a\u53e5\u5b50\u3002 \u8fd9\u91cc\uff0cDT\u8868\u793a\u884c\u5217\u5f0f\uff0cVBP\u8868\u793a\u52a8\u8bcd\uff0cJJ\u8868\u793a\u5f62\u5bb9\u8bcd\uff0cIN\u8868\u793a\u4ecb\u8bcd\uff0cNN\u8868\u793a\u540d\u8bcd\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
sentence=[(\"a\"<\/span>,\"DT\"<\/span><\/span>),(\"clever\"<\/span>,\"JJ\"<\/span>),(\"fox\"<\/span>,\"NN\"<\/span><\/span>),(\"was\"<\/span>,\"VBP\"<\/span><\/span>),
          (\"jumping\"<\/span>,\"VBP\"),(\"over\"<\/span>,\"IN\"<\/span>),(\"the\"<\/span>,\"DT\"),(\"wall\"<\/span>,\"NN\")]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u7ed9\u51fa\u8bed\u6cd5\u3002 \u5728\u8fd9\u91cc\u4ee5\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u5f62\u5f0f\u7ed9\u51fa\u8bed\u6cd5\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
grammar = \"NP:{<DT>?<JJ>*<NN>}\"<\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u89e3\u6790\u5668\u6765\u89e3\u6790\u8bed\u6cd5\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
parser_chunking = nltk.RegexpParser<\/span>(grammar)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u89e3\u6790\u5668\u89e3\u6790\u8be5\u53e5\u5b50\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
parser_chunking.parse<\/span>(sentence)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u63a5\u4e0b\u6765\uff0c\u9700\u8981\u83b7\u5f97\u8f93\u51fa\u3002 \u8f93\u51fa\u5728\u540d\u4e3aoutput_chunk\u7684\u53d8\u91cf\u4e2d\u751f\u6210\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Output_chunk = parser_chunking.parse<\/span>(sentence)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u4ee5\u4e0b\u4ee3\u7801\u540e\uff0c\u4ee5\u6811\u7684\u5f62\u5f0f\u7ed8\u5236\u8f93\u51fa\u7ed3\u679c\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
output.draw<\/span>()
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u4e0a\u9762\u4ee3\u7801\uff0c\u5f97\u5230\u4ee5\u4e0b\u7ed3\u679c -\n <\/div>\n
\n NLTK\u5305_https:\/\/bianchenghao6.com_\u3010\u4eba\u5de5\u667a\u80fd\u6559\u7a0b\u3011_\u7b2c2\u5f20\n <\/div>\n

\u8bcd\u888b(BOW)\u6a21\u578b<\/h2>\n
\n \u8bcd\u888b - \u82f1\u6587\u4e3a:
\n Bag of Word(BoW)<\/em> \uff0c\u5b83\u662f\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4e2d\u7684\u4e00\u4e2a\u6a21\u578b\uff0c\u57fa\u672c\u4e0a\u7528\u4e8e\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u7279\u5f81\uff0c\u4ee5\u4fbf\u6587\u672c\u53ef\u7528\u4e8e\u5efa\u6a21\uff0c\u4ee5\u4fbf\u5728\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u4e2d\u4f7f\u7528\u3002\n <\/div>\n
\n \u73b0\u5728\u95ee\u9898\u51fa\u73b0\u4e86\uff0c\u4e3a\u4ec0\u4e48\u6211\u4eec\u9700\u8981\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u7279\u5f81\u3002 \u8fd9\u662f\u56e0\u4e3a\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u4e0d\u80fd\u5904\u7406\u539f\u59cb\u6570\u636e\uff0c\u4ed6\u4eec\u9700\u8981\u6570\u5b57\u6570\u636e\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u4ece\u4e2d\u63d0\u53d6\u6709\u610f\u4e49\u7684\u4fe1\u606f\u3002 \u5c06\u6587\u672c\u6570\u636e\u8f6c\u6362\u4e3a\u6570\u5b57\u6570\u636e\u79f0\u4e3a\u7279\u5f81\u63d0\u53d6\u6216\u7279\u5f81\u7f16\u7801\u3002\n <\/div>\n
\n \u5b83\u662f\u600e\u4e48\u8fd0\u884c\u7684<\/strong>\u8fd9\u662f\u4ece\u6587\u672c\u4e2d\u63d0\u53d6\u7279\u5f81\u7684\u975e\u5e38\u7b80\u5355\u7684\u65b9\u6cd5\u3002 \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u6587\u672c\u6587\u6863\uff0c\u5e76\u4e14\u5e0c\u671b\u5c06\u5176\u8f6c\u6362\u4e3a\u6570\u5b57\u6570\u636e\u6216\u8005\u8bf4\u60f3\u4ece\u4e2d\u63d0\u53d6\u7279\u5f81\uff0c\u90a3\u4e48\u9996\u5148\u8fd9\u4e2a\u6a21\u578b\u4ece\u6587\u6863\u4e2d\u7684\u6240\u6709\u5355\u8bcd\u4e2d\u63d0\u53d6\u8bcd\u6c47\u3002 \u7136\u540e\u901a\u8fc7\u4f7f\u7528\u6587\u6863\u672f\u8bed\u77e9\u9635\uff0c\u5b83\u5c06\u5efa\u7acb\u4e00\u4e2a\u6a21\u578b\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cBoW\u4ec5\u5c06\u6587\u4ef6\u8868\u793a\u4e3a\u4e00\u888b\u6587\u5b57\u3002 \u4e22\u5f03\u5173\u4e8e\u6587\u6863\u4e2d\u5355\u8bcd\u7684\u987a\u5e8f\u6216\u7ed3\u6784\u7684\u4efb\u4f55\u4fe1\u606f\u3002\n <\/div>\n
\n \u6587\u6863\u672f\u8bed\u77e9\u9635\u7684\u6982\u5ff5<\/strong>\n <\/div>\n
\n BoW\u7b97\u6cd5\u901a\u8fc7\u4f7f\u7528\u6587\u6863\u672f\u8bed\u77e9\u9635\u6765\u5efa\u7acb\u6a21\u578b\u3002 \u987e\u540d\u601d\u4e49\uff0c\u6587\u6863\u672f\u8bed\u77e9\u9635\u5c31\u662f\u6587\u6863\u4e2d\u51fa\u73b0\u7684\u5404\u79cd\u5b57\u6570\u7684\u77e9\u9635\u3002 \u5728\u8fd9\u4e2a\u77e9\u9635\u7684\u5e2e\u52a9\u4e0b\uff0c\u6587\u672c\u6587\u6863\u53ef\u4ee5\u8868\u793a\u4e3a\u5404\u79cd\u5355\u8bcd\u7684\u52a0\u6743\u7ec4\u5408\u3002 \u901a\u8fc7\u8bbe\u7f6e\u9608\u503c\u5e76\u9009\u62e9\u66f4\u6709\u610f\u4e49\u7684\u5355\u8bcd\uff0c\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u6587\u6863\u4e2d\u53ef\u7528\u4f5c\u7279\u5f81\u5411\u91cf\u7684\u6240\u6709\u5355\u8bcd\u7684\u76f4\u65b9\u56fe\u3002 \u4ee5\u4e0b\u662f\u4e86\u89e3\u6587\u6863\u672f\u8bed\u77e9\u9635\u6982\u5ff5\u7684\u793a\u4f8b -\n <\/div>\n
\n \u793a\u4f8b<\/strong>\n <\/div>\n
\n \u5047\u8bbe\u6709\u4ee5\u4e0b\u4e24\u4e2a\u53e5\u5b50 -\n <\/div>\n

\u53e5\u5b501<\/strong> - \u6b63\u5728\u4f7f\u7528\u8bcd\u888b\u6a21\u578b\u3002<\/span>
\n \u53e5\u5b502<\/strong> - Bag of Words\u6a21\u578b\u7528\u4e8e\u63d0\u53d6\u7279\u5f81\u3002<\/span> <\/p>\n

\n \u73b0\u5728\uff0c\u901a\u8fc7\u8003\u8651\u8fd9\u4e24\u53e5\u5b50\uff0c\u6709\u4ee5\u4e0b13\u4e2a\u4e0d\u540c\u7684\u5355\u8bcd -\n <\/div>\n

we<\/span>
\n are<\/span>
\n using<\/span>
\n the<\/span>
\n bag<\/span>
\n of<\/span>
\n words<\/span>
\n model<\/span>
\n is<\/span>
\n used<\/span>
\n for<\/span>
\n extracting<\/span>
\n features<\/span> <\/p>\n

\n \u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528\u6bcf\u4e2a\u53e5\u5b50\u4e2d\u7684\u5355\u8bcd\u8ba1\u6570\u4e3a\u6bcf\u4e2a\u53e5\u5b50\u5efa\u7acb\u4e00\u4e2a\u76f4\u65b9\u56fe -\n <\/div>\n

\u5b50\u53e51<\/strong> \u2212 [1,1,1,1,1,1,1,1,0,0,0,0,0]<\/span>
\n \u5b50\u53e52<\/strong> \u2212 [0,0,0,1,1,1,1,1,1,1,1,1,1]<\/span> <\/p>\n

\n \u8fd9\u6837\uff0c\u5c31\u5f97\u5230\u4e86\u5df2\u7ecf\u63d0\u53d6\u7684\u7279\u5f81\u5411\u91cf\u3002\u6bcf\u4e2a\u7279\u5f81\u5411\u91cf\u90fd\u662f13\u7ef4\u7684\uff0c\u56e0\u4e3a\u8fd9\u91cc\u670913\u4e2a\u4e0d\u540c\u7684\u5355\u8bcd\u3002\n <\/div>\n

\u7edf\u8ba1\u6982\u5ff5<\/h2>\n
\n \u7edf\u8ba1\u5b66\u7684\u6982\u5ff5\u79f0\u4e3aTermFrequency-Inverse Document Frequency(tf-idf)\u3002 \u6bcf\u4e2a\u5355\u8bcd\u5728\u6587\u6863\u4e2d\u90fd\u5f88\u91cd\u8981\u3002 \u7edf\u8ba1\u6570\u636e\u6709\u52a9\u4e8e\u6211\u4eec\u7406\u89e3\u6bcf\u4e2a\u8bcd\u7684\u91cd\u8981\u6027\u3002\n <\/div>\n
\n \u672f\u8bed\u9891\u7387(tf)<\/strong>\u8fd9\u662f\u8861\u91cf\u6bcf\u4e2a\u5355\u8bcd\u51fa\u73b0\u5728\u6587\u6863\u4e2d\u7684\u9891\u7387\u3002 \u5b83\u53ef\u4ee5\u901a\u8fc7\u5c06\u6bcf\u4e2a\u8bcd\u7684\u8ba1\u6570\u9664\u4ee5\u7ed9\u5b9a\u6587\u6863\u4e2d\u7684\u8bcd\u7684\u603b\u6570\u6765\u83b7\u5f97\u3002\n <\/div>\n
\n \u9006\u6587\u6863\u9891\u7387(idf)<\/strong>\u8fd9\u662f\u8861\u91cf\u5728\u7ed9\u5b9a\u7684\u6587\u6863\u96c6\u4e2d\u8fd9\u4e2a\u6587\u6863\u6709\u591a\u72ec\u7279\u7684\u4e00\u4e2a\u5355\u8bcd\u3002\u8981\u8ba1\u7b97idf\u548c\u5236\u5b9a\u4e00\u4e2a\u7279\u5f81\u5411\u91cf\uff0c\u6211\u4eec\u9700\u8981\u51cf\u5c11\u50cf\u8fd9\u6837\u7684\u5e38\u89c1\u8bcd\u7684\u6743\u91cd\uff0c\u5e76\u6743\u8861\u7a00\u6709\u8bcd\u3002\n <\/div>\n

\u5728NLTK\u4e2d\u5efa\u7acb\u4e00\u4e2a\u8bcd\u888b\u6a21\u578b<\/h2>\n
\n \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528CountVectorizer\u4ece\u8fd9\u4e9b\u53e5\u5b50\u4e2d\u521b\u5efa\u77e2\u91cf\u6765\u5b9a\u4e49\u5b57\u7b26\u4e32\u96c6\u5408\u3002\n <\/div>\n
\n \u5bfc\u5165\u5fc5\u8981\u7684\u8f6f\u4ef6\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>sklearn.feature_extraction.text import <\/span>CountVectorizer
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\u5b9a\u4e49\u4e00\u7ec4\u53e5\u5b50\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Sentences = ['We are using the Bag of Word model'<\/span>, 'Bag of Word model <\/span>is
           used for <\/span><\/span>extracting the features.']
vectorizer_count = CountVectorizer()
features_text = vectorizer.fit_transform<\/span>(Sentences).todense<\/span>()
print(vectorizer.vocabulary_)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4e0a\u8ff0\u7a0b\u5e8f\u751f\u6210\u5982\u4e0b\u6240\u793a\u7684\u8f93\u51fa\u3002\u5b83\u8868\u660e\u5728\u4e0a\u8ff0\u4e24\u53e5\u8bdd\u4e2d\u670913\u4e2a\u4e0d\u540c\u7684\u5355\u8bcd -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
{'we'<\/span>: 11, 'are'<\/span>: 0, 'using'<\/span>: 10, 'the'<\/span>: 8, 'bag'<\/span>: 1, 'of'<\/span>: 7,
 'word'<\/span>: 12, 'model'<\/span>: 6, 'is'<\/span>: 5, 'used'<\/span>: 9, 'for'<\/span>: 4, 'extracting'<\/span>: 2, 'features'<\/span>: 3}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8fd9\u4e9b\u662f\u53ef\u4ee5\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u7684\u7279\u5f81\u5411\u91cf(\u6587\u672c\u5230\u6570\u5b57\u5f62\u5f0f)\u3002\n <\/div>\n

\u89e3\u51b3\u95ee\u9898<\/h2>\n
\n \u5728\u672c\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u89e3\u51b3\u4e00\u4e9b\u76f8\u5173\u95ee\u9898\u3002\n <\/div>\n
\n \u7c7b\u522b\u9884\u6d4b<\/strong>\n <\/div>\n
\n \u5728\u4e00\u7ec4\u6587\u4ef6\u4e2d\uff0c\u4e0d\u4ec5\u5355\u8bcd\u800c\u4e14\u5355\u8bcd\u7684\u7c7b\u522b\u4e5f\u5f88\u91cd\u8981; \u5728\u54ea\u4e2a\u7c7b\u522b\u7684\u6587\u672c\u4e2d\u7279\u5b9a\u7684\u8bcd\u843d\u5165\u3002 \u4f8b\u5982\uff0c\u60f3\u8981\u9884\u6d4b\u7ed9\u5b9a\u7684\u53e5\u5b50\u662f\u5426\u5c5e\u4e8e\u7535\u5b50\u90ae\u4ef6\uff0c\u65b0\u95fb\uff0c\u4f53\u80b2\uff0c\u8ba1\u7b97\u673a\u7b49\u7c7b\u522b\u3002\u5728\u4e0b\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u4f7f\u7528tf-idf\u6765\u5236\u5b9a\u7279\u5f81\u5411\u91cf\u6765\u67e5\u627e\u6587\u6863\u7684\u7c7b\u522b\u3002\u4f7f\u7528sklearn\u768420\u4e2a\u65b0\u95fb\u7ec4\u6570\u636e\u96c6\u4e2d\u7684\u6570\u636e\u3002\n <\/div>\n
\n \u5bfc\u5165\u5fc5\u8981\u7684\u8f6f\u4ef6\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
from <\/span>sklearn.datasets import <\/span>fetch_20newsgroups
from <\/span>sklearn.naive_bayes import <\/span>MultinomialNB
from <\/span>sklearn.feature_extraction.text import <\/span>TfidfTransformer
from <\/span>sklearn.feature_extraction.text import <\/span>CountVectorizer
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5b9a\u4e49\u5206\u7c7b\u56fe\u3002\u4f7f\u7528\u4e94\u4e2a\u4e0d\u540c\u7684\u7c7b\u522b\uff0c\u5206\u522b\u662f\u5b97\u6559\uff0c\u6c7d\u8f66\uff0c\u4f53\u80b2\uff0c\u7535\u5b50\u548c\u7a7a\u95f4\u3002\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
category_map = {'talk.religion.misc'<\/span>:'Religion'<\/span>,'rec.autos'<\/span>'Autos'<\/span>,
   'rec.sport.hockey'<\/span>:'Hockey'<\/span>,'sci.electronics'<\/span>:'Electronics'<\/span>, 'sci.space'<\/span>: 'Space'<\/span>}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u521b\u5efa\u8bad\u7ec3\u96c6 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
training_data = fetch_20newsgroups(subset = 'train'<\/span>,
   categories = category_map.keys<\/span>(), shuffle = True<\/span>, random_state = 5)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6784\u5efa\u4e00\u4e2a\u5411\u91cf\u8ba1\u6570\u5668\u5e76\u63d0\u53d6\u672f\u8bed\u8ba1\u6570 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
vectorizer_count = CountVectorizer()
train_tc = vectorizer_count.fit_transform<\/span>(training_data.data)
print(\"\\nDimensions of training data:\"<\/span>, train_tc.shape)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n tf-idf\u8f6c\u6362\u5668\u7684\u521b\u5efa\u8fc7\u7a0b\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
tfidf = TfidfTransformer()
train_tfidf = tfidf.fit_transform<\/span>(train_tc)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u5b9a\u4e49\u6d4b\u8bd5\u6570\u636e -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
input_data = [
   'Discovery was <\/span>a space shuttle'<\/span>,
   'Hindu, Christian, Sikh all are religions'<\/span>,
   'We must have to drive safely'<\/span>,
   'Puck is <\/span>a disk made of rubber'<\/span>,
   'Television, Microwave, Refrigrated all uses electricity'<\/span>
]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4ee5\u4e0a\u6570\u636e\u5c06\u7528\u4e8e\u8bad\u7ec3\u4e00\u4e2aMultinomial\u6734\u7d20\u8d1d\u53f6\u65af\u5206\u7c7b\u5668 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
classifier = MultinomialNB().fit<\/span>(train_tfidf, training_data.target)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4f7f\u7528\u8ba1\u6570\u5411\u91cf\u5316\u5668\u8f6c\u6362\u8f93\u5165\u6570\u636e -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
input_tc = vectorizer_count.transform<\/span>(input_data)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u5c06\u4f7f\u7528tfidf\u8f6c\u6362\u5668\u6765\u8f6c\u6362\u77e2\u91cf\u5316\u6570\u636e -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
input_tfidf = tfidf.transform<\/span>(input_tc)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6267\u884c\u4e0a\u9762\u4ee3\u7801\uff0c\u5c06\u9884\u6d4b\u8f93\u51fa\u7c7b\u522b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
predictions = classifier.predict<\/span>(input_tfidf)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u8f93\u51fa\u7ed3\u679c\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
for <\/span><\/span>sent, category in <\/span>zip(input_data, predictions):
   print('\\nInput Data:'<\/span>, sent, '\\n Category:'<\/span>, \\
      category_map[training_data.target_names[category]])
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u7c7b\u522b\u9884\u6d4b\u5668\u751f\u6210\u4ee5\u4e0b\u8f93\u51fa -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Dimensions of training data: (2755, 39297)
Input Data: Discovery was <\/span>a space shuttle
Category: Space
Input Data: Hindu, Christian, Sikh all are religions
Category: Religion
Input Data: We must have to drive safely
Category: Autos
Input Data: Puck is <\/span>a disk made of rubber
Category: Hockey
Input Data: Television, Microwave, Refrigrated all uses electricity
Category: Electronics
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u6027\u522b\u53d1\u73b0\u5668<\/strong>\n <\/div>\n
\n \u5728\u8fd9\u4e2a\u95ee\u9898\u9648\u8ff0\u4e2d\uff0c\u5c06\u901a\u8fc7\u63d0\u4f9b\u540d\u5b57\u6765\u8bad\u7ec3\u5206\u7c7b\u5668\u4ee5\u627e\u5230\u6027\u522b(\u7537\u6027\u6216\u5973\u6027)\u3002 \u6211\u4eec\u9700\u8981\u4f7f\u7528\u542f\u53d1\u5f0f\u6784\u9020\u7279\u5f81\u5411\u91cf\u5e76\u8bad\u7ec3\u5206\u7c7b\u5668\u3002\u8fd9\u91cc\u4f7f\u7528scikit-learn\u8f6f\u4ef6\u5305\u4e2d\u7684\u6807\u7b7e\u6570\u636e\u3002 \u4ee5\u4e0b\u662f\u6784\u5efa\u6027\u522b\u67e5\u627e\u5668\u7684Python\u4ee3\u7801 -\n <\/div>\n
\n \u5bfc\u5165\u5fc5\u8981\u7684\u8f6f\u4ef6\u5305 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
import <\/span>random
from <\/span>nltk import <\/span>NaiveBayesClassifier
from <\/span>nltk.classify import <\/span>accuracy as <\/span>nltk_accuracy
from <\/span>nltk.corpus import <\/span>names
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\u9700\u8981\u4ece\u8f93\u5165\u5b57\u4e2d\u63d0\u53d6\u6700\u540e\u7684N\u4e2a\u5b57\u6bcd\u3002 \u8fd9\u4e9b\u5b57\u6bcd\u5c06\u4f5c\u4e3a\u529f\u80fd -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
def <\/span>extract_features(word, N = 2):
   last_n_letters = word[-N:]
   return <\/span>{'feature'<\/span>: last_n_letters.lower<\/span>()}
if <\/span>__name__=='__main__'<\/span>:
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4f7f\u7528NLTK\u4e2d\u63d0\u4f9b\u7684\u6807\u7b7e\u540d\u79f0(\u7537\u6027\u548c\u5973\u6027)\u521b\u5efa\u57f9\u8bad\u6570\u636e -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
male_list = [(name, 'male'<\/span>) for <\/span><\/span>name in <\/span>names.words<\/span>('male.txt'<\/span>)]
female_list = [(name, 'female'<\/span>) for <\/span><\/span>name in <\/span>names.words<\/span>('female.txt'<\/span>)]
data = (male_list + female_list)
random.seed<\/span>(5)
random.shuffle<\/span>(data)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u6d4b\u8bd5\u6570\u636e\u5c06\u88ab\u521b\u5efa\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
namesInput = ['Rajesh'<\/span>, 'Gaurav'<\/span>, 'Swati'<\/span>, 'Shubha'<\/span>]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5b9a\u4e49\u7528\u4e8e\u5217\u8f66\u548c\u6d4b\u8bd5\u7684\u6837\u672c\u6570 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
train_sample = int(0.8 * len<\/span>(data))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u9700\u8981\u8fed\u4ee3\u4e0d\u540c\u7684\u957f\u5ea6\uff0c\u4ee5\u4fbf\u53ef\u4ee5\u6bd4\u8f83\u7cbe\u5ea6 -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
for <\/span><\/span>i in <\/span>range(1, 6):
   print('\\nNumber of end letters:'<\/span>, i)
   features = [(extract_features(n, i), gender) for <\/span><\/span>(n, gender) in <\/span>data]
   train_data, test_data = features[:train_sample],
features[train_sample:]
   classifier = NaiveBayesClassifier.train<\/span>(train_data)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5206\u7c7b\u5668\u7684\u51c6\u786e\u5ea6\u53ef\u4ee5\u8ba1\u7b97\u5982\u4e0b -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
accuracy_classifier = round(100 * nltk_accuracy(classifier, test_data), 2)
   print('Accuracy = '<\/span> + str(accuracy_classifier) + '%'<\/span>)
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u73b0\u5728\uff0c\u53ef\u4ee5\u9884\u6d4b\u8f93\u51fa\u7ed3\u679c -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
for <\/span><\/span>name in <\/span>namesInput:
   print(name, '==>'<\/span>, classifier.classify<\/span>(extract_features(name, i))
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u4e0a\u8ff0\u7a0b\u5e8f\u5c06\u751f\u6210\u4ee5\u4e0b\u8f93\u51fa -\n <\/div>\n
\n
 # Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-26<\/span>
Number of end letters: 1
Accuracy = 74.7%
Rajesh -> female
Gaurav -> male
Swati -> female
Shubha -> female
Number of end letters: 2
Accuracy = 78.79%
Rajesh -> male
Gaurav -> male
Swati -> female
Shubha -> female
Number of end letters: 3
Accuracy = 77.22%
Rajesh -> male
Gaurav -> female
Swati -> female
Shubha -> female
Number of end letters: 4
Accuracy = 69.98%
Rajesh -> female
Gaurav -> female
Swati -> female
Shubha -> female
Number of end letters: 5
Accuracy = 64.63%
Rajesh -> female
Gaurav -> female
Swati -> female
Shubha -> female
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n \u5728\u4e0a\u9762\u7684\u8f93\u51fa\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u7ed3\u675f\u5b57\u6bcd\u7684\u6700\u5927\u6570\u91cf\u7684\u51c6\u786e\u6027\u662f\u4e24\u4e2a\uff0c\u5e76\u4e14\u968f\u7740\u7ed3\u675f\u5b57\u6bcd\u6570\u91cf\u7684\u589e\u52a0\u800c\u51cf\u5c11\u3002\n <\/div>\n

\u4e3b\u9898\u5efa\u6a21:\u8bc6\u522b\u6587\u672c\u6570\u636e\u4e2d\u7684\u6a21\u5f0f<\/h2>\n
\n \u6211\u4eec\u77e5\u9053\uff0c\u4e00\u822c\u800c\u8a00\uff0c\u6587\u6863\u88ab\u5206\u7ec4\u4e3a\u4e3b\u9898\u3002 \u6709\u65f6\u9700\u8981\u786e\u5b9a\u6587\u672c\u4e2d\u4e0e\u7279\u5b9a\u4e3b\u9898\u76f8\u5bf9\u5e94\u7684\u6a21\u5f0f\u3002 \u8fd9\u6837\u505a\u7684\u6280\u672f\u79f0\u4e3a\u4e3b\u9898\u5efa\u6a21\u3002 \u6362\u53e5\u8bdd\u8bf4\uff0c\u53ef\u4ee5\u8bf4\u4e3b\u9898\u5efa\u6a21\u662f\u4e00\u79cd\u63ed\u793a\u7ed9\u5b9a\u6587\u6863\u96c6\u5408\u4e2d\u62bd\u8c61\u4e3b\u9898\u6216\u9690\u85cf\u7ed3\u6784\u7684\u6280\u672f\u3002\n <\/div>\n
\n \u53ef\u4ee5\u5728\u4ee5\u4e0b\u573a\u666f\u4e2d\u4f7f\u7528\u4e3b\u9898\u5efa\u6a21\u6280\u672f -\n <\/div>\n
\n \u6587\u672c\u5206\u7c7b<\/strong>\u5728\u4e3b\u9898\u5efa\u6a21\u7684\u5e2e\u52a9\u4e0b\uff0c\u5206\u7c7b\u53ef\u4ee5\u5f97\u5230\u6539\u8fdb\uff0c\u56e0\u4e3a\u5b83\u5c06\u76f8\u4f3c\u7684\u5355\u8bcd\u5206\u7ec4\u5728\u4e00\u8d77\uff0c\u800c\u4e0d\u662f\u5206\u522b\u5c06\u6bcf\u4e2a\u5355\u8bcd\u7528\u4f5c\u7279\u5f81\u3002\n <\/div>\n
\n \u63a8\u8350\u7cfb\u7edf<\/strong>\u5728\u4e3b\u9898\u5efa\u6a21\u7684\u5e2e\u52a9\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u4f3c\u6027\u5ea6\u91cf\u6765\u6784\u5efa\u63a8\u8350\u7cfb\u7edf\u3002\n <\/div>\n

\u4e3b\u9898\u5efa\u6a21\u7b97\u6cd5<\/h2>\n
\n \u4e3b\u9898\u5efa\u6a21\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528\u7b97\u6cd5\u6765\u5b9e\u73b0\u3002 \u7b97\u6cd5\u5982\u4e0b -\n <\/div>\n
\n \u6f5c\u5728\u72c4\u5229\u514b\u96f7\u5206\u914d(LDA)<\/strong>\u8be5\u7b97\u6cd5\u662f\u4e3b\u9898\u5efa\u6a21\u4e2d\u6700\u6d41\u884c\u7684\u7b97\u6cd5\u3002 \u5b83\u4f7f\u7528\u6982\u7387\u56fe\u5f62\u6a21\u578b\u6765\u5b9e\u73b0\u4e3b\u9898\u5efa\u6a21\u3002 \u6211\u4eec\u9700\u8981\u5728Python\u4e2d\u5bfc\u5165gensim\u5305\u4ee5\u4f7f\u7528LDA slgorithm\u3002\n <\/div>\n
\n \u6f5c\u5728\u8bed\u4e49\u5206\u6790(LDA)\u6216\u6f5c\u5728\u8bed\u4e49\u7d22\u5f15(LSI)<\/strong>\u8be5\u7b97\u6cd5\u57fa\u4e8e\u7ebf\u6027\u4ee3\u6570\u3002 \u57fa\u672c\u4e0a\u5b83\u5728\u6587\u6863\u672f\u8bed\u77e9\u9635\u4e0a\u4f7f\u7528SVD(\u5947\u5f02\u503c\u5206\u89e3)\u7684\u6982\u5ff5\u3002\n <\/div>\n
\n \u975e\u8d1f\u77e9\u9635\u5206\u89e3(NMF)<\/strong>\u5b83\u4e5f\u57fa\u4e8e\u7ebf\u6027\u4ee3\u6570\u3002\n <\/div>\n
\n \u4e0a\u8ff0\u6240\u6709\u7528\u4e8e\u8bdd\u9898\u5efa\u6a21\u7684\u7b97\u6cd5\u90fd\u5c06\u4e3b\u9898\u6570\u91cf\u4f5c\u4e3a\u53c2\u6570\uff0c\u5c06\u6587\u6863 - \u8bcd\u6c47\u77e9\u9635\u4f5c\u4e3a\u8f93\u5165\uff0c\u5c06WTM(\u8bcd\u4e3b\u9898\u77e9\u9635)\u548cTDM(\u4e3b\u9898\u6587\u6863\u77e9\u9635)\u4f5c\u4e3a\u8f93\u51fa\u3002\n <\/div>\n

<\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"NLTK\u5305zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[137],"tags":[],"class_list":["post-1315","post","type-post","status-publish","format-standard","hentry","category-rgznjc1"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1315"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1315"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1315\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1315"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1315"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1315"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}