{"id":1120842,"date":"2025-01-08T19:02:19","date_gmt":"2025-01-08T11:02:19","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1120842.html"},"modified":"2025-01-08T19:02:37","modified_gmt":"2025-01-08T11:02:37","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%af%b9txt%e6%96%87%e6%a1%a3%e6%8b%86%e5%88%86%e5%8f%a5%e5%ad%90","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1120842.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u5bf9txt\u6587\u6863\u62c6\u5206\u53e5\u5b50"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083335\/04f29f20-0a42-4a8b-a813-e1604e2fff91.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u5bf9txt\u6587\u6863\u62c6\u5206\u53e5\u5b50\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5b9e\u73b0\u5bf9txt\u6587\u6863\u8fdb\u884c\u53e5\u5b50\u62c6\u5206\uff0c\u53ef\u4ee5\u5229\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u5de5\u5177\u548c\u5e93\uff0c\u5982NLTK\u3001SpaCy\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/strong> \u5176\u4e2d\uff0c<strong>NLTK\uff08Natural Language Toolkit\uff09<\/strong>\u662f\u4e00\u79cd\u5f3a\u5927\u7684Python\u5e93\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u6587\u672c\u6570\u636e\u3002\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u6765\u5206\u8bcd\u3001\u6807\u8bb0\u8bcd\u6027\u3001\u63d0\u53d6\u4fe1\u606f\u7b49\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528NLTK\u6765\u5b8c\u6210\u8fd9\u4e00\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u51c6\u5907\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u8981\u5f00\u59cb\u4f7f\u7528NLTK\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u5b89\u88c5\u5b83\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install nltk<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u4e0b\u8f7d\u4e00\u4e9b\u5fc5\u8981\u7684\u6570\u636e\u5305\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>nltk.download(&#39;punkt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bfb\u53d6txt\u6587\u6863<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6587\u672c\u5185\u5bb9\u3002Python\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u5904\u7406\u51fd\u6570\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u8bfb\u53d6txt\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        return file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528NLTK\u62c6\u5206\u53e5\u5b50<\/h3>\n<\/p>\n<p><p>NLTK\u63d0\u4f9b\u4e86\u53e5\u5b50\u5206\u5272\u5668<code>sent_tokenize<\/code>\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6587\u672c\u5206\u5272\u6210\u53e5\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from nltk.tokenize import sent_tokenize<\/p>\n<p>def split_sentences(text):<\/p>\n<p>    return sent_tokenize(text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<\/p>\n<p><p>\u5c06\u4ee5\u4e0a\u6b65\u9aa4\u6574\u5408\u8d77\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>from nltk.tokenize import sent_tokenize<\/p>\n<h2><strong>\u4e0b\u8f7d\u5fc5\u8981\u7684\u6570\u636e\u5305<\/strong><\/h2>\n<p>nltk.download(&#39;punkt&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6txt\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>def read_file(file_path):<\/p>\n<p>    with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        return file.read()<\/p>\n<h2><strong>\u62c6\u5206\u53e5\u5b50<\/strong><\/h2>\n<p>def split_sentences(text):<\/p>\n<p>    return sent_tokenize(text)<\/p>\n<h2><strong>\u4e3b\u51fd\u6570<\/strong><\/h2>\n<p>if __name__ == &quot;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&quot;:<\/p>\n<p>    file_path = &#39;example.txt&#39;<\/p>\n<p>    text = read_file(file_path)<\/p>\n<p>    sentences = split_sentences(text)<\/p>\n<p>    for sentence in sentences:<\/p>\n<p>        print(sentence)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65\u4f18\u5316\u548c\u6269\u5c55\u8fd9\u4e2a\u529f\u80fd\uff0c\u4ee5\u9002\u5e94\u66f4\u591a\u7684\u9700\u6c42\u548c\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u4e0d\u540c\u8bed\u8a00\u7684\u6587\u672c<\/h4>\n<\/p>\n<p><p>NLTK\u652f\u6301\u591a\u79cd\u8bed\u8a00\u7684\u53e5\u5b50\u5206\u5272\uff0c\u53ea\u9700\u8981\u5728<code>sent_tokenize<\/code>\u51fd\u6570\u4e2d\u6307\u5b9a\u8bed\u8a00\u53c2\u6570\u5373\u53ef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">sentences = sent_tokenize(text, language=&#39;english&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u53ef\u4ee5\u5206\u5757\u8bfb\u53d6\u548c\u5904\u7406\uff0c\u4ee5\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_file_in_chunks(file_path, chunk_size=1024):<\/p>\n<p>    with open(file_path, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:<\/p>\n<p>        while True:<\/p>\n<p>            chunk = file.read(chunk_size)<\/p>\n<p>            if not chunk:<\/p>\n<p>                break<\/p>\n<p>            yield chunk<\/p>\n<p>def split_sentences_in_chunks(file_path):<\/p>\n<p>    for chunk in read_file_in_chunks(file_path):<\/p>\n<p>        sentences = sent_tokenize(chunk)<\/p>\n<p>        for sentence in sentences:<\/p>\n<p>            yield sentence<\/p>\n<h2><strong>\u4e3b\u51fd\u6570<\/strong><\/h2>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    file_path = &#39;example.txt&#39;<\/p>\n<p>    for sentence in split_sentences_in_chunks(file_path):<\/p>\n<p>        print(sentence)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u5904\u7406\u6807\u70b9\u7b26\u53f7\u590d\u6742\u7684\u6587\u672c<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6807\u70b9\u7b26\u53f7\u590d\u6742\u7684\u6587\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u9884\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def preprocess_text(text):<\/p>\n<p>    # \u66ff\u6362\u8fde\u7eed\u7684\u7a7a\u767d\u5b57\u7b26\u4e3a\u4e00\u4e2a\u7a7a\u683c<\/p>\n<p>    text = re.sub(r&#39;\\s+&#39;, &#39; &#39;, text)<\/p>\n<p>    # \u79fb\u9664\u4e0d\u5fc5\u8981\u7684\u6807\u70b9\u7b26\u53f7<\/p>\n<p>    text = re.sub(r&#39;[^\\w\\s\\.\\,\\!\\?]&#39;, &#39;&#39;, text)<\/p>\n<p>    return text<\/p>\n<h2><strong>\u4fee\u6539\u4e3b\u51fd\u6570<\/strong><\/h2>\n<p>if __name__ == &quot;__main__&quot;:<\/p>\n<p>    file_path = &#39;example.txt&#39;<\/p>\n<p>    text = read_file(file_path)<\/p>\n<p>    preprocessed_text = preprocess_text(text)<\/p>\n<p>    sentences = split_sentences(preprocessed_text)<\/p>\n<p>    for sentence in sentences:<\/p>\n<p>        print(sentence)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p><strong>\u901a\u8fc7\u4f7f\u7528NLTK\u5e93\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5b9e\u73b0\u5bf9txt\u6587\u6863\u7684\u53e5\u5b50\u62c6\u5206<\/strong>\u3002<strong>NLTK\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5904\u7406\u4e0d\u540c\u8bed\u8a00\u548c\u590d\u6742\u6807\u70b9\u7b26\u53f7\u7684\u6587\u672c<\/strong>\u3002\u901a\u8fc7\u5bf9\u4ee3\u7801\u8fdb\u884c\u4f18\u5316\u548c\u6269\u5c55\uff0c\u6211\u4eec\u53ef\u4ee5\u5904\u7406\u5927\u6587\u4ef6\u5e76\u5bf9\u6587\u672c\u8fdb\u884c\u9884\u5904\u7406\uff0c\u8fdb\u4e00\u6b65\u63d0\u5347\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u7406\u89e3\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5bf9txt\u6587\u6863\u7684\u53e5\u5b50\u62c6\u5206\uff0c\u5e76\u4e3a\u4f60\u63d0\u4f9b\u4e00\u4e9b\u6709\u7528\u7684\u6280\u5de7\u548c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5904\u7406txt\u6587\u6863\u4e2d\u7684\u53e5\u5b50\u62c6\u5206\uff1f<\/strong><br \/>\u4f7f\u7528Python\u5904\u7406txt\u6587\u6863\u4e2d\u7684\u53e5\u5b50\u62c6\u5206\uff0c\u901a\u5e38\u53ef\u4ee5\u5229\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5e38\u89c1\u7684\u505a\u6cd5\u662f\u8bfb\u53d6\u6587\u6863\u5185\u5bb9\uff0c\u7136\u540e\u4f7f\u7528<code>nltk<\/code>\u5e93\u4e2d\u7684<code>sent_tokenize()<\/code>\u51fd\u6570\uff0c\u6216\u8005\u5229\u7528Python\u7684\u5b57\u7b26\u4e32<code>split()<\/code>\u65b9\u6cd5\u6309\u7279\u5b9a\u6807\u70b9\u62c6\u5206\u3002\u786e\u4fdd\u5728\u62c6\u5206\u8fc7\u7a0b\u4e2d\u6ce8\u610f\u6807\u70b9\u7b26\u53f7\u548c\u7a7a\u683c\u7684\u5904\u7406\uff0c\u4ee5\u83b7\u5f97\u66f4\u51c6\u786e\u7684\u53e5\u5b50\u5206\u5272\u3002<\/p>\n<p><strong>\u5728\u62c6\u5206\u53e5\u5b50\u7684\u8fc7\u7a0b\u4e2d\u5982\u4f55\u5904\u7406\u4e0d\u540c\u8bed\u8a00\u7684\u6587\u672c\uff1f<\/strong><br \/>\u5bf9\u4e8e\u591a\u8bed\u8a00\u6587\u672c\uff0c\u4f7f\u7528<code>nltk<\/code>\u5e93\u4e2d\u7684\u53e5\u5b50\u62c6\u5206\u529f\u80fd\u662f\u4e00\u4e2a\u4e0d\u9519\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u652f\u6301\u591a\u79cd\u8bed\u8a00\u3002\u4f60\u53ef\u4ee5\u6839\u636e\u6587\u672c\u7684\u8bed\u8a00\u8bbe\u7f6e<code>nltk<\/code>\u7684\u6a21\u578b\uff0c\u786e\u4fdd\u53e5\u5b50\u62c6\u5206\u7684\u51c6\u786e\u6027\u3002\u540c\u65f6\uff0c\u8003\u8651\u4f7f\u7528\u8bed\u8a00\u68c0\u6d4b\u5e93\u5982<code>langdetect<\/code>\u6765\u81ea\u52a8\u8bc6\u522b\u6587\u672c\u8bed\u8a00\uff0c\u4ece\u800c\u9009\u62e9\u5408\u9002\u7684\u53e5\u5b50\u62c6\u5206\u5de5\u5177\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u957f\u53e5\u5b50\u6216\u590d\u6742\u53e5\u5b50\u4ee5\u63d0\u9ad8\u62c6\u5206\u51c6\u786e\u6027\uff1f<\/strong><br \/>\u5728\u5904\u7406\u957f\u53e5\u5b50\u6216\u590d\u6742\u53e5\u5b50\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u66f4\u5f3a\u5927\u7684\u81ea\u7136\u8bed\u8a00\u5904\u7406\u5de5\u5177\uff0c\u4f8b\u5982<code>spaCy<\/code>\u3002\u8fd9\u4e2a\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u8fdb\u884c\u53e5\u5b50\u62c6\u5206\uff0c\u8fd8\u80fd\u8bc6\u522b\u53e5\u5b50\u4e2d\u7684\u8bed\u6cd5\u7ed3\u6784\uff0c\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u53e5\u5b50\u7684\u7ec4\u6210\u90e8\u5206\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u53e5\u5b50\u62c6\u5206\u548c\u4f9d\u5b58\u5206\u6790\uff0c\u53ef\u4ee5\u5927\u5e45\u63d0\u9ad8\u62c6\u5206\u7684\u51c6\u786e\u6027\u548c\u5408\u7406\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5b9e\u73b0\u5bf9txt\u6587\u6863\u8fdb\u884c\u53e5\u5b50\u62c6\u5206\uff0c\u53ef\u4ee5\u5229\u7528\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u5de5\u5177\u548c\u5e93\uff0c\u5982NLTK\u3001SpaCy [&hellip;]","protected":false},"author":3,"featured_media":1120868,"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\/1120842"}],"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=1120842"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120842\/revisions"}],"predecessor-version":[{"id":1120874,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1120842\/revisions\/1120874"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1120868"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1120842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1120842"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1120842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}