<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>HanLP &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/hanlp/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Wed, 29 Jul 2020 10:51:03 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] 使用 fastText 做中文文本分类(2)</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8-fasttext-%e5%81%9a%e4%b8%ad%e6%96%87%e6%96%87%e6%9c%ac%e5%88%86%e7%b1%bb2/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8-fasttext-%e5%81%9a%e4%b8%ad%e6%96%87%e6%96%87%e6%9c%ac%e5%88%86%e7%b1%bb2/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 29 Jul 2020 09:46:54 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[fastText]]></category>
		<category><![CDATA[HanLP]]></category>
		<category><![CDATA[中文]]></category>
		<category><![CDATA[文本分类]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=12777</guid>

					<description><![CDATA[<p>查看本系列文章合集，请看<a href="https://www.codelast.com/?p=12856" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>做好文本预处理，才能输入fastText训练一个效果好的模型出来。</p>
<p><span style="background-color: rgb(255, 255, 0);">✓</span>&#160;原文示例<br />
有时我们拿到的源数据是很粗糙的，带有很多会影响模型效果的内容，例如下面这样：</p>
<blockquote><p>
	&#60;p&#62;罗望子，是豆科酸豆属唯一的种，是热带乔木，原产于东部非洲，包括马达加斯加落叶森林，但已被引入热带亚洲、拉丁美洲和加勒比海。柽柳是中国海南省三亚的一种城市树木。罗望子最适合生长在温度高、日照长、气候干燥、干湿季节分明的地区。&#60;/p&#62;&#60;p&#62;&#60;img src=&#34;http://p0.qhimg.com/t014b83dc78c7cc5000.jpg?size=741x320&#34;/&#62;&#60;br /&#62;&#60;/p&#62;&#60;p&#62;罗望子富含糖、乙酸、酒石酸、甲酸、柠檬酸等成分，主要用于调味品、饮料、果酱等食品领域。吃一点罗望子有很多好处。当我们吃罗望子时，罗望子中含有的多糖是一种非常好的抗光物质。当我们吃这种物质时，它可以防止紫外线辐射伤害皮肤。通常吃一点罗望子，饭前吃一点罗望子可以增进食欲，改善我们的饮食质量。在炎热的夏日，吃一点罗望子可以生津止渴，清热解毒，降低中暑的风险。&#60;/p&#62;&#60;p&#62;&#60;img src=&#34;http://p1.qhimg.com/t01ecdbbc26c329a78b.jpg?size=533x409&#34;/&#62;&#60;br /&#62;&#60;/p&#62;&#60;p&#62;罗望子种子含有丰富的抗氧化物质。多吃罗望子籽可以延缓人体衰老，保持皮肤湿润有光泽。罗望子种子还含有一些清热解毒、消炎的物质，可以帮助我们的人体抵抗一些有害细菌，保护我们的健康。</p></blockquote>
<div>
	这里面不仅带有URL、大量的HTML标签，而且还有标点符号等，这些都要清洗掉。<br />
	<span id="more-12777"></span><br />
	<span style="background-color: rgb(255, 255, 0);">✓</span>&#160;清除掉HTML标签、URL<br />
	这里给出Java版的一个实现(从网上借鉴来的)，它们并不完善，但是够用了：
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">&#160;&#160;&#160;&#160;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">/**
&#160;&#160;&#160;&#160;&#160;*&#160;移除一个字符串中的HTML标签。
&#160;&#160;&#160;&#160;&#160;*
&#160;&#160;&#160;&#160;&#160;*&#160;<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">@param</span>&#160;inputStr&#160;输入的字符串。
&#160;&#160;&#160;&#160;&#160;*&#160;<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">@return</span>&#160;移除了HTML标签之后的字符串。
&#160;&#160;&#160;&#160;&#160;*/</span>
&#160;&#160;&#160;&#160;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">private</span>&#160;String&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">removeHtmlTags</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String&#160;inputStr)</span>&#160;</span>{
&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&#160;(StringUtils.isEmpty(inputStr))&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&#160;inputStr;
&#160;&#160;&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&#160;inputStr.replaceAll(</code></pre></section></div>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8-fasttext-%e5%81%9a%e4%b8%ad%e6%96%87%e6%96%87%e6%9c%ac%e5%88%86%e7%b1%bb2/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>查看本系列文章合集，请看<a href="https://www.codelast.com/?p=12856" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这里</span></a>。</p>
<p>做好文本预处理，才能输入fastText训练一个效果好的模型出来。</p>
<p><span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;原文示例<br />
有时我们拿到的源数据是很粗糙的，带有很多会影响模型效果的内容，例如下面这样：</p>
<blockquote><p>
	&lt;p&gt;罗望子，是豆科酸豆属唯一的种，是热带乔木，原产于东部非洲，包括马达加斯加落叶森林，但已被引入热带亚洲、拉丁美洲和加勒比海。柽柳是中国海南省三亚的一种城市树木。罗望子最适合生长在温度高、日照长、气候干燥、干湿季节分明的地区。&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;http://p0.qhimg.com/t014b83dc78c7cc5000.jpg?size=741x320&quot;/&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;罗望子富含糖、乙酸、酒石酸、甲酸、柠檬酸等成分，主要用于调味品、饮料、果酱等食品领域。吃一点罗望子有很多好处。当我们吃罗望子时，罗望子中含有的多糖是一种非常好的抗光物质。当我们吃这种物质时，它可以防止紫外线辐射伤害皮肤。通常吃一点罗望子，饭前吃一点罗望子可以增进食欲，改善我们的饮食质量。在炎热的夏日，吃一点罗望子可以生津止渴，清热解毒，降低中暑的风险。&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;http://p1.qhimg.com/t01ecdbbc26c329a78b.jpg?size=533x409&quot;/&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;罗望子种子含有丰富的抗氧化物质。多吃罗望子籽可以延缓人体衰老，保持皮肤湿润有光泽。罗望子种子还含有一些清热解毒、消炎的物质，可以帮助我们的人体抵抗一些有害细菌，保护我们的健康。</p></blockquote>
<div>
	这里面不仅带有URL、大量的HTML标签，而且还有标点符号等，这些都要清洗掉。<br />
	<span id="more-12777"></span><br />
	<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;清除掉HTML标签、URL<br />
	这里给出Java版的一个实现(从网上借鉴来的)，它们并不完善，但是够用了：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;移除一个字符串中的HTML标签。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">@param</span>&nbsp;inputStr&nbsp;输入的字符串。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">@return</span>&nbsp;移除了HTML标签之后的字符串。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;String&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">removeHtmlTags</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String&nbsp;inputStr)</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;(StringUtils.isEmpty(inputStr))&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&nbsp;inputStr;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&nbsp;inputStr.replaceAll(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;&lt;[^&gt;]*&gt;&quot;</span>,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;&quot;</span>);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">/**
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;移除一个字符串中的URL。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">@param</span>&nbsp;inputStr&nbsp;输入的字符串。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">@return</span>&nbsp;移除了URL之后的字符串。
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">private</span>&nbsp;String&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">removeUrl</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(String&nbsp;inputStr)</span>&nbsp;</span>{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;urlPattern&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;((https?|ftp|gopher|telnet|file|Unsure|http):((//)|(\\\$$)+[\\w\\d:#@%/;$()~_?+-=\\\\.&amp;]*)&quot;</span>;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pattern&nbsp;p&nbsp;=&nbsp;Pattern.compile(urlPattern,&nbsp;Pattern.CASE_INSENSITIVE);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Matcher&nbsp;m&nbsp;=&nbsp;p.matcher(inputStr);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringBuffer&nbsp;sb&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;StringBuffer(inputStr.length());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&nbsp;(m.find())&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.appendReplacement(sb,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;&quot;</span>);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>&nbsp;sb.length()&nbsp;==&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">0</span>&nbsp;?&nbsp;inputStr&nbsp;:&nbsp;sb.toString();
&nbsp;&nbsp;&nbsp;&nbsp;}
</code></pre>
</section>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	以前面的内容为例，依次经过上面两个方法处理后，得到的结果是：</div>
<blockquote>
<div>
		罗望子，是豆科酸豆属唯一的种，是热带乔木，原产于东部非洲，包括马达加斯加落叶森林，但已被引入热带亚洲、拉丁美洲和加勒比海。柽柳是中国海南省三亚的一种城市树木。罗望子最适合生长在温度高、日照长、气候干燥、干湿季节分明的地区。罗望子富含糖、乙酸、酒石酸、甲酸、柠檬酸等成分，主要用于调味品、饮料、果酱等食品领域。吃一点罗望子有很多好处。当我们吃罗望子时，罗望子中含有的多糖是一种非常好的抗光物质。当我们吃这种物质时，它可以防止紫外线辐射伤害皮肤。通常吃一点罗望子，饭前吃一点罗望子可以增进食欲，改善我们的饮食质量。在炎热的夏日，吃一点罗望子可以生津止渴，清热解毒，降低中暑的风险。罗望子种子含有丰富的抗氧化物质。多吃罗望子籽可以延缓人体衰老，保持皮肤湿润有光泽。罗望子种子还含有一些清热解毒、消炎的物质，可以帮助我们的人体抵抗一些有害细菌，保护我们的健康。</div>
</blockquote>
<div>
	这样看上去就干净多了。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	除此之外，还有标点符号，以及一些无用的词需要被除掉，这些词对表达文本的主要含义作用不大，比如&ldquo;啊&rdquo;，&ldquo;嘿&rdquo;之类的词，它们一般称为<span style="color:#0000ff;">停用词</span>(stop words)。我们可以从网上下载到常用的停用词表，比如<a href="https://github.com/goto456/stopwords" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">这个</span></a>链接。<br />
	在对文本进行分词之后，出现在停用词表中的，全都过滤掉。</p>
<p>	<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;分词并去除停用词<br />
	Python下比较流行的中文分词器之一是<a href="https://github.com/fxsjy/jieba" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">结巴分词</span></a>：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;jieba


my_text&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;罗望子，是豆科酸豆属唯一的种，...&#39;

</span>

<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;分词</span>
segmented_words&nbsp;=&nbsp;jieba.cut(my_text,&nbsp;cut_all=<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">False</span>)&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;精确分词模式

</span>

<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;去除停用词
</span>
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&nbsp;single_word&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;segmented_words:
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">TODO:</span>&nbsp;去除停用词</span></code></pre>
</section>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	Java版的结巴分词作者已经不维护了，建议换其他的分词器，比如用户量很大的<a href="https://github.com/hankcs/HanLP" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">HanLP</span></a>。其使用也非常简单，在Maven项目的 pom.xml&nbsp;中添加：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="xml language-xml hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>com.hankcs<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">groupId</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>hanlp<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">artifactId</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>portable-1.7.8<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">version</span>&gt;</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-tag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">&lt;/<span class="hljs-name" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">dependency</span>&gt;</span></code></pre>
</section>
<p>就可以用了：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="java language-java hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;com.hankcs.hanlp.seg.common.Term;
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;com.hankcs.hanlp.tokenizer.StandardTokenizer;

String&nbsp;myText&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;罗望子，是豆科酸豆属唯一的种，...&quot;</span>;

List&lt;Term&gt;&nbsp;termList&nbsp;=&nbsp;StandardTokenizer.segment(myText);
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&nbsp;(Term&nbsp;term&nbsp;:&nbsp;termList)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(term.word);&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;term.word即分出来的每个词</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//<span class="hljs-doctag" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;">TODO:</span>&nbsp;去除停用词</span>
}
</code></pre>
</section>
<p>现在得到的是分好的词，并且把停用词等杂七杂八的东西都去掉了，也许对某些情况很糟糕的文本，还会有漏网之鱼，但对一般情况来说基本上够用了。<br />
	不同的分词器分出来的词结果不尽相同，这里以Java版为例，得到：</div>
<blockquote>
<div>
		罗 望子 豆科 酸 豆 属 唯一 种 热带 乔木 原产 东部 非洲 包括 马达加斯加 落 叶森林 已 引入 热带 亚洲 拉丁美洲 加勒比海 柽 柳是 中国 海南省 三亚 种 城市 树木 罗 望子 最 适合 生长 温度 高 日照 长 气候 干燥 干湿 季节 分明 地区 罗 望子 富含 糖 乙酸 酒石酸 甲酸 柠檬酸 成分 用于 调味品 饮料 果酱 食品 领域 吃 一点 罗 望子 很多 好处 吃 罗 望子 时 罗 望子 中 含有 多糖 种 好 抗光 物质 吃 物质 时 紫外线 辐射 伤害 皮肤 吃 一点 罗 望子 饭前 吃 一点 罗 望子 增进 食欲 改善 饮食 质量 炎热 夏日 吃 一点 罗 望子 生津止渴 清热 解毒 降低 中暑 风险 罗 望子 种子 含有 抗氧化 物质 吃 罗 望子 籽 延缓 人体 衰老 皮肤 湿润 光泽 罗 望子 种子 还 含有 清热 解毒 消炎 物质 人体 抵抗 有害 细菌 保护 健康</div>
</blockquote>
<div>
	为了清晰展示结果，词与词之间用空格隔开了。<br />
	如果对分词结果不满意，比如人名，特殊物品名，可以为分词器添加字典，让分词器识别它们。这些调优的工作就不详述了。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;文本标注<br />
	fastText对训练数据的格式要求是这样的：</p>
<blockquote>
<p>
			<span style="color:#0000ff;">__label__健康</span> 罗 望子 豆科 酸 豆 属 唯一 种 热带 乔木 原产 东部 非洲 包括 马达加斯加 落 叶森林 已 引入 热带 亚洲 拉丁美洲 加勒比海 柽 柳是 中国 海南省 三亚 种 城市 树木 罗 望子 最 适合 生长 温度 高 日照 长 气候 干燥 干湿 季节 分明 地区 罗 望子 富含 糖 乙酸 酒石酸 甲酸 柠檬酸 成分 用于 调味品 饮料 果酱 食品 领域 吃 一点 罗 望子 很多 好处 吃 罗 望子 时 罗 望子 中 含有 多糖 种 好 抗光 物质 吃 物质 时 紫外线 辐射 伤害 皮肤 吃 一点 罗 望子 饭前 吃 一点 罗 望子 增进 食欲 改善 饮食 质量 炎热 夏日 吃 一点 罗 望子 生津止渴 清热 解毒 降低 中暑 风险 罗 望子 种子 含有 抗氧化 物质 吃 罗 望子 籽 延缓 人体 衰老 皮肤 湿润 光泽 罗 望子 种子 还 含有 清热 解毒 消炎 物质 人体 抵抗 有害 细菌 保护 健康</p>
</blockquote>
<p>	每两个字符串之间都用空格隔开，以&ldquo;<span style="color:#0000ff;">__label__</span>&rdquo;开头的（前后均为双下划线）是后面文本的标签，这里可以有多个标签，比如&ldquo;__label__健康 __label__生活&rdquo;，也是用空格分隔开。前面说过，本文以1个标签举例，所以这里只有1个。<br />
	这是文本预处理的最后一步，完成之后就可以把数据喂给fastText训练出一个模型啦。<br />
	比如说上面分好词的文本，我们要为它标注一个类别（例如&ldquo;健康&rdquo;）。<br />
	然鹅，我们难道用人工肉眼看的方法来标注吗？并且，类别有哪些，我怎么知道？<br />
	就算有一目十行的能力，看瞎我的钛合金狗眼，我一天也标不了10万条数据啊！<br />
	欲知解决方法，请看下一篇文章。</p>
<p>	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
		<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bd%bf%e7%94%a8-fasttext-%e5%81%9a%e4%b8%ad%e6%96%87%e6%96%87%e6%9c%ac%e5%88%86%e7%b1%bb2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
