{"id":1919,"date":"2024-03-05T09:25:40","date_gmt":"2024-03-05T01:25:40","guid":{"rendered":"http:\/\/blog.xtaa.cn\/?p=1919"},"modified":"2024-04-01T11:36:34","modified_gmt":"2024-04-01T03:36:34","slug":"doc2bow%e7%ae%80%e4%bb%8b%e4%b8%8e%e5%ae%9e%e8%b7%b5demo","status":"publish","type":"post","link":"http:\/\/blog.xtaa.cn\/index.php\/2024\/03\/05\/doc2bow%e7%ae%80%e4%bb%8b%e4%b8%8e%e5%ae%9e%e8%b7%b5demo\/","title":{"rendered":"Doc2Bow\u7b80\u4ecb\u4e0e\u5b9e\u8df5Demo"},"content":{"rendered":"\n<p>Doc2Bow\u662fGensim\u4e2d\u5c01\u88c5\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4e3b\u8981\u7528\u4e8e\u5b9e\u73b0Bow\u6a21\u578b\uff0c\u4e0b\u9762\u4e3b\u8981\u4ecb\u7ecd\u4e0bBow\u6a21\u578b\u3002<\/p>\n\n\n\n<p>1\u3001BoW\u6a21\u578b\u539f\u7406<br>Bag-of-words model (BoW model) \u6700\u65e9\u51fa\u73b0\u5728\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08Natural Language Processing\uff09\u548c\u4fe1\u606f\u68c0\u7d22\uff08Information Retrieval\uff09\u9886\u57df.\u3002\u8be5\u6a21\u578b\u5ffd\u7565\u6389\u6587\u672c\u7684\u8bed\u6cd5\u548c\u8bed\u5e8f\u7b49\u8981\u7d20\uff0c\u5c06\u5176\u4ec5\u4ec5\u770b\u4f5c\u662f\u82e5\u5e72\u4e2a\u8bcd\u6c47\u7684\u96c6\u5408\uff0c\u6587\u6863\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u7684\u51fa\u73b0\u90fd\u662f\u72ec\u7acb\u7684\u3002BoW\u4f7f\u7528\u4e00\u7ec4\u65e0\u5e8f\u7684\u5355\u8bcd(words)\u6765\u8868\u8fbe\u4e00\u6bb5\u6587\u5b57\u6216\u4e00\u4e2a\u6587\u6863.\u3002\u8fd1\u5e74\u6765\uff0cBoW\u6a21\u578b\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u8ba1\u7b97\u673a\u89c6\u89c9\u4e2d\u3002<br>\u57fa\u4e8e\u6587\u672c\u7684BoW\u6a21\u578b\u7684\u4e00\u4e2a\u7b80\u5355\u4f8b\u5b50\u5982\u4e0b\uff1a<br>\u9996\u5148\u7ed9\u51fa\u4e24\u4e2a\u7b80\u5355\u7684\u6587\u672c\u6587\u6863\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>John likes to watch movies. Mary likes too.\nJohn also likes to watch football games.<\/code><\/pre>\n\n\n\n<p>1<br>2<br>\u57fa\u4e8e\u4e0a\u8ff0\u4e24\u4e2a\u6587\u6863\u4e2d\u51fa\u73b0\u7684\u5355\u8bcd\uff0c\u6784\u5efa\u5982\u4e0b\u4e00\u4e2a\u8bcd\u5178 (dictionary)\uff1a<\/p>\n\n\n\n<p>{&#8220;John&#8221;: 1, &#8220;likes&#8221;: 2,&#8221;to&#8221;: 3, &#8220;watch&#8221;: 4, &#8220;movies&#8221;: 5,&#8221;also&#8221;: 6, &#8220;football&#8221;: 7, &#8220;games&#8221;: 8,&#8221;Mary&#8221;: 9, &#8220;too&#8221;: 10}<br>1<br>\u4e0a\u9762\u7684\u8bcd\u5178\u4e2d\u5305\u542b10\u4e2a\u5355\u8bcd, \u6bcf\u4e2a\u5355\u8bcd\u6709\u552f\u4e00\u7684\u7d22\u5f15, \u90a3\u4e48\u6bcf\u4e2a\u6587\u672c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a10\u7ef4\u7684\u5411\u91cf\u6765\u8868\u793a\u3002\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> &#91;1, 2, 1, 1, 1, 0, 0, 0, 1, 1]\n &#91;1, 1,1, 1, 0, 1, 1, 1, 0, 0]<\/code><\/pre>\n\n\n\n<p>1<br>2<br>\u8be5\u5411\u91cf\u4e0e\u539f\u6765\u6587\u672c\u4e2d\u5355\u8bcd\u51fa\u73b0\u7684\u987a\u5e8f\u6ca1\u6709\u5173\u7cfb\uff0c\u800c\u662f\u8bcd\u5178\u4e2d\u6bcf\u4e2a\u5355\u8bcd\u5728\u6587\u672c\u4e2d\u51fa\u73b0\u7684\u9891\u7387\u3002<br>\u4e5f\u662f\u901a\u8fc7\u4f59\u5f26\u5b9a\u7406\u8ba1\u7b97\u4e24\u4e2a\u53e5\u5b50\u7684\u76f8\u4f3c\u5ea6\u3002<br><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5b9e\u8df5DEMO<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import gensim\r\nimport jieba\r\n# \u8bad\u7ec3\u6837\u672c\r\nfrom gensim import corpora\r\nfrom gensim.similarities import Similarity\r\n\r\n\r\n# fin = open(\"questions.txt\",encoding='utf8').read().strip(' ')   #strip()\u53d6\u51fa\u9996\u4f4d\u7a7a\u683c\r\njieba.load_userdict(\"userdict.txt\")\r\nstopwords = set(open('stopwords.txt',encoding='utf8').read().strip('\\n').split('\\n'))   #\u8bfb\u5165\u505c\u7528\u8bcd\r\nraw_documents = &#91;\r\n    '0\u65e0\u507f\u5c45\u95f4\u4ecb\u7ecd\u4e70\u5356\u6bd2\u54c1\u7684\u884c\u4e3a\u5e94\u5982\u4f55\u5b9a\u6027',\r\n    '1\u5438\u6bd2\u7537\u52a8\u6001\u6301\u6709\u5927\u91cf\u6bd2\u54c1\u7684\u884c\u4e3a\u8be5\u5982\u4f55\u8ba4\u5b9a',\r\n    '2\u5982\u4f55\u533a\u5206\u662f\u975e\u6cd5\u79cd\u690d\u6bd2\u54c1\u539f\u690d\u7269\u7f6a\u8fd8\u662f\u975e\u6cd5\u5236\u9020\u6bd2\u54c1\u7f6a',\r\n    '3\u4e3a\u6bd2\u8d29\u8d29\u5356\u6bd2\u54c1\u63d0\u4f9b\u5e2e\u52a9\u6784\u6210\u8d29\u5356\u6bd2\u54c1\u7f6a',\r\n    '4\u5c06\u81ea\u5df1\u5438\u98df\u7684\u6bd2\u54c1\u539f\u4ef7\u8f6c\u8ba9\u7ed9\u670b\u53cb\u5438\u98df\u7684\u884c\u4e3a\u8be5\u5982\u4f55\u8ba4\u5b9a',\r\n    '5\u4e3a\u83b7\u62a5\u916c\u5e2e\u4eba\u8d2d\u4e70\u6bd2\u54c1\u7684\u884c\u4e3a\u8be5\u5982\u4f55\u8ba4\u5b9a',\r\n    '6\u6bd2\u8d29\u51fa\u72f1\u540e\u518d\u6b21\u591f\u4e70\u6bd2\u54c1\u9014\u4e2d\u88ab\u6293\u7684\u884c\u4e3a\u8ba4\u5b9a',\r\n    '7\u865a\u5938\u6bd2\u54c1\u529f\u6548\u529d\u4eba\u5438\u98df\u6bd2\u54c1\u7684\u884c\u4e3a\u8be5\u5982\u4f55\u8ba4\u5b9a',\r\n    '8\u59bb\u5b50\u4e0b\u843d\u4e0d\u660e\u4e08\u592b\u53c8\u4e0e\u4ed6\u4eba\u767b\u8bb0\u7ed3\u5a5a\u662f\u5426\u4e3a\u65e0\u6548\u5a5a\u59fb',\r\n    '9\u4e00\u65b9\u672a\u7b7e\u5b57\u529e\u7406\u7684\u7ed3\u5a5a\u767b\u8bb0\u662f\u5426\u6709\u6548',\r\n    '10\u592b\u59bb\u53cc\u65b91990\u5e74\u6309\u519c\u6751\u4e60\u4fd7\u4e3e\u529e\u5a5a\u793c\u6ca1\u6709\u7ed3\u5a5a\u8bc1 \u4e00\u65b9\u53ef\u5426\u8d77\u8bc9\u79bb\u5a5a',\r\n    '11\u7ed3\u5a5a\u524d\u5bf9\u65b9\u7236\u6bcd\u51fa\u8d44\u8d2d\u4e70\u7684\u4f4f\u623f\u5199\u6211\u4eec\u4e8c\u4eba\u7684\u540d\u5b57\u6709\u6548\u5417',\r\n    '12\u8eab\u4efd\u8bc1\u88ab\u522b\u4eba\u5192\u7528\u65e0\u6cd5\u767b\u8bb0\u7ed3\u5a5a\u600e\u4e48\u529e\uff1f',\r\n    '13\u540c\u5c45\u540e\u53c8\u4e0e\u4ed6\u4eba\u767b\u8bb0\u7ed3\u5a5a\u662f\u5426\u6784\u6210\u91cd\u5a5a\u7f6a',\r\n    '14\u672a\u529e\u767b\u8bb0\u53ea\u4e3e\u529e\u7ed3\u5a5a\u4eea\u5f0f\u53ef\u8d77\u8bc9\u79bb\u5a5a\u5417',\r\n    '15\u540c\u5c45\u591a\u5e74\u672a\u529e\u7406\u7ed3\u5a5a\u767b\u8bb0\uff0c\u662f\u5426\u53ef\u4ee5\u5411\u6cd5\u9662\u8d77\u8bc9\u8981\u6c42\u79bb\u5a5a'\r\n]\r\ncorpora_documents = &#91;]\r\nfor item_text in raw_documents:\r\n    item_str = jieba.lcut(item_text)\r\n    print(item_str)\r\n    corpora_documents.append(item_str)\r\nprint(corpora_documents)\r\n# \u751f\u6210\u5b57\u5178\u548c\u5411\u91cf\u8bed\u6599\r\ndictionary = corpora.Dictionary(corpora_documents)\r\ncorpus = &#91;dictionary.doc2bow(text) for text in corpora_documents]\r\n#num_features\u4ee3\u8868\u751f\u6210\u7684\u5411\u91cf\u7684\u7ef4\u6570\uff08\u6839\u636e\u8bcd\u888b\u7684\u5927\u5c0f\u6765\u5b9a\uff09\r\nsimilarity = Similarity('-Similarity-index', corpus, num_features=400)\r\n\r\ntest_data_1 = '\u4f60\u597d\uff0c\u6211\u60f3\u95ee\u4e00\u4e0b\u6211\u60f3\u79bb\u5a5a\u4ed6\u4e0d\u60f3\u79bb\uff0c\u5b69\u5b50\u4ed6\u8bf4\u4e0d\u8981\uff0c\u662f\u516d\u4e2a\u6708\u5c31\u81ea\u52a8\u751f\u6548\u79bb\u5a5a'\r\ntest_cut_raw_1 = jieba.lcut(test_data_1)\r\n\r\nprint(test_cut_raw_1)\r\ntest_corpus_1 = dictionary.doc2bow(test_cut_raw_1)\r\nsimilarity.num_best = 5\r\nprint(similarity&#91;test_corpus_1])  # \u8fd4\u56de\u6700\u76f8\u4f3c\u7684\u6837\u672c\u6750\u6599,(index_of_document, similarity) tuples\r\n\r\nprint('################################')\r\n\r\ntest_data_2 = '\u5bb6\u4eba\u56e0\u6d89\u5acc\u8fd0\u8f93\u6bd2\u54c1\u88ab\u6293\uff0c\u5979\u53ea\u662f\u53bb\u670b\u53cb\u5bb6\u63a2\u671b\u670b\u53cb\u7684\uff0c\u7ed3\u679c\u5c31\u88ab\u6293\u4e86\uff0c\u8fd8\u5728\u670b\u53cb\u5bb6\u6536\u51fa\u6bd2\u54c1\uff0c\u53ef\u5bb6\u4eba\u7684\u8eab\u4e0a\u548c\u884c\u674e\u4e2d\u90fd\u6ca1\u6709\u3002\u73b0\u5728\u5df2\u7ecf\u62d8\u755910\u591a\u5929\u4e86\uff0c\u8bf7\u95ee\u4f1a\u88ab\u5224\u5211\u5417'\r\ntest_cut_raw_2 = jieba.lcut(test_data_2)\r\nprint(test_cut_raw_2)\r\ntest_corpus_2 = dictionary.doc2bow(test_cut_raw_2)\r\nsimilarity.num_best = 5\r\nprint(similarity&#91;test_corpus_2])  # \u8fd4\u56de\u6700\u76f8\u4f3c\u7684\u6837\u672c\u6750\u6599,(index_of_document, similarity) tuples<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Doc2Bow\u662fGensim\u4e2d\u5c01\u88c5\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u4e3b\u8981\u7528\u4e8e\u5b9e\u73b0Bow\u6a21\u578b\uff0c\u4e0b\u9762\u4e3b\u8981\u4ecb\u7ecd\u4e0bBow\u6a21\u578b\u3002 1\u3001BoW\u6a21 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-1919","post","type-post","status-publish","format-standard","hentry","category-30"],"_links":{"self":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1919"}],"version-history":[{"count":1,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1919\/revisions"}],"predecessor-version":[{"id":1920,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/posts\/1919\/revisions\/1920"}],"wp:attachment":[{"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1919"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.xtaa.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}