<?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>文本分类 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E6%96%87%E6%9C%AC%E5%88%86%E7%B1%BB/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Wed, 29 Jul 2020 12:06:34 +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 做中文文本分类》文章合集</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e3%80%8a%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%bb%e3%80%8b%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e3%80%8a%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%bb%e3%80%8b%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 29 Jul 2020 10:46:59 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[fastText]]></category>
		<category><![CDATA[中文]]></category>
		<category><![CDATA[文本分类]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=12856</guid>

					<description><![CDATA[<p>本文描述了如何使用 <a href="https://fasttext.cc/" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">fastText</span></a> 对中文进行文本分类的过程，只有实操，基本没有理论。<br />
以下按顺序编排。</p>
<p><span style="background-color: rgb(255, 255, 0);">✓</span>&#160;<a href="https://www.codelast.com/?p=12754" rel="noopener noreferrer" target="_blank">使用 fastText 做中文文本分类(1)</a><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&#160;<a href="https://www.codelast.com/?p=12777" rel="noopener noreferrer" target="_blank">使用 fastText 做中文文本分类(2)</a><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&#160;<a href="https://www.codelast.com/?p=12796" rel="noopener noreferrer" target="_blank">使用 fastText 做中文文本分类(3)</a><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&#160;<a href="https://www.codelast.com/?p=12827" rel="noopener noreferrer" target="_blank">使用 fastText 做中文文本分类(4)</a><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&#160;<a href="https://www.codelast.com/?p=12840" rel="noopener noreferrer" target="_blank">使用 fastText 做中文文本分类(5)</a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b%e3%80%8a%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%bb%e3%80%8b%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>本文描述了如何使用 <a href="https://fasttext.cc/" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">fastText</span></a> 对中文进行文本分类的过程，只有实操，基本没有理论。<br />
以下按顺序编排。</p>
<p><span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;<a href="https://www.codelast.com/?p=12754" rel="noopener noreferrer" target="_blank">使用 fastText 做中文文本分类(1)</a><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;<a href="https://www.codelast.com/?p=12777" rel="noopener noreferrer" target="_blank">使用 fastText 做中文文本分类(2)</a><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;<a href="https://www.codelast.com/?p=12796" rel="noopener noreferrer" target="_blank">使用 fastText 做中文文本分类(3)</a><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;<a href="https://www.codelast.com/?p=12827" rel="noopener noreferrer" target="_blank">使用 fastText 做中文文本分类(4)</a><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;<a href="https://www.codelast.com/?p=12840" rel="noopener noreferrer" target="_blank">使用 fastText 做中文文本分类(5)</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b%e3%80%8a%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%bb%e3%80%8b%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 使用 fastText 做中文文本分类(5)</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%bb5/</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%bb5/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 29 Jul 2020 09:48:07 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[fastText]]></category>
		<category><![CDATA[FastText4j]]></category>
		<category><![CDATA[中文]]></category>
		<category><![CDATA[文本分类]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=12840</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 可执行程序完成的。fastText提供了Python的接口，同样的功能也可以用Python实现。如果数据量比较小，单机做文本分类没啥问题。但我的数据量比较大，几十G的文本数据，单机加载模型、预测分类太耗资源了，而且速度慢。<br />
并行这种事嘛，交给Map-Reduce job来做是最合适不过了，不过，要在Hadoop集群上安装fastText的Python包是不可能的，所以我只能找一下，fastText的模型怎么用Java加载，从而在M-R job中并行地去做预测。<br />
<span id="more-12840"></span><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&#160;选择<br />
网上能搜到好些个 fastText 的&#8220;Java版&#8221;，比如 <a href="https://github.com/vinhkhuc/JFastText" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">JFastText</span></a>，它是 fastText 的一个Java wrapper；又比如&#160;<a href="https://github.com/mayabot/mynlp/tree/master/fastText4j" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">FastText4j</span></a>，它是一个完全由 Kotlin &#38; Java 实现的 fastText 实现。还有其他的，没有调研。<br />
看了 FastText4j 的自我介绍：</p>
<blockquote>
<div>
		● 100%由Kotlin &#38; Java实现</div>
<div>
		● 良好的API</div>
<div>
		● 兼容官方原版的预训练模型</div>
<div>
		● 提供所有的包括train、test等api</div>
<div>
		● 支持自有模型存储格式，可以使用MMAP快速加载大模型</div>
</blockquote>
<div>
	我心动了，马上试用。
<p>	<span style="background-color: rgb(255, 255, 0);">✓</span>&#160;Maven项目引入 FastText4j 依赖</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono',monospace;font-size:12.0pt;">
<span style="color:#e8bf6a;">&#60;dependency&#62;
</span><span style="color:#e8bf6a;">  &#60;groupId&#62;</span>com.mayabot.mynlp</pre></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%bb5/" 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 可执行程序完成的。fastText提供了Python的接口，同样的功能也可以用Python实现。如果数据量比较小，单机做文本分类没啥问题。但我的数据量比较大，几十G的文本数据，单机加载模型、预测分类太耗资源了，而且速度慢。<br />
并行这种事嘛，交给Map-Reduce job来做是最合适不过了，不过，要在Hadoop集群上安装fastText的Python包是不可能的，所以我只能找一下，fastText的模型怎么用Java加载，从而在M-R job中并行地去做预测。<br />
<span id="more-12840"></span><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;选择<br />
网上能搜到好些个 fastText 的&ldquo;Java版&rdquo;，比如 <a href="https://github.com/vinhkhuc/JFastText" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">JFastText</span></a>，它是 fastText 的一个Java wrapper；又比如&nbsp;<a href="https://github.com/mayabot/mynlp/tree/master/fastText4j" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">FastText4j</span></a>，它是一个完全由 Kotlin &amp; Java 实现的 fastText 实现。还有其他的，没有调研。<br />
看了 FastText4j 的自我介绍：</p>
<blockquote>
<div>
		● 100%由Kotlin &amp; Java实现</div>
<div>
		● 良好的API</div>
<div>
		● 兼容官方原版的预训练模型</div>
<div>
		● 提供所有的包括train、test等api</div>
<div>
		● 支持自有模型存储格式，可以使用MMAP快速加载大模型</div>
</blockquote>
<div>
	我心动了，马上试用。</p>
<p>	<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;Maven项目引入 FastText4j 依赖</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono',monospace;font-size:12.0pt;">
<span style="color:#e8bf6a;">&lt;dependency&gt;
</span><span style="color:#e8bf6a;">  &lt;groupId&gt;</span>com.mayabot.mynlp<span style="color:#e8bf6a;">&lt;/groupId&gt;
</span><span style="color:#e8bf6a;">  &lt;artifactId&gt;</span>fastText4j<span style="color:#e8bf6a;">&lt;/artifactId&gt;
</span><span style="color:#e8bf6a;">  &lt;version&gt;</span>3.1.7<span style="color:#e8bf6a;">&lt;/version&gt;
</span><span style="color:#e8bf6a;">&lt;/dependency&gt;</span></pre>
<p>	这样就能在代码里用了。<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;训练模型</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono',monospace;font-size:12.0pt;">
<span style="color:#808080;">// </span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">用</span><span style="color:#808080;">FastText4j</span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">训练一个文本分类模型，模型保存成单个文件
</span>File trainFile = <span style="color:#cc7832;">new </span>File(<span style="color:#6a8759;">&quot;/home/codelast/labeled-data_train&quot;</span>)<span style="color:#cc7832;">;
</span>InputArgs inputArgs = <span style="color:#cc7832;">new </span>InputArgs()<span style="color:#cc7832;">;
</span>inputArgs.setLoss(LossName.<span style="color:#9876aa;font-style:italic;">softmax</span>)<span style="color:#cc7832;">;
</span>inputArgs.setLr(<span style="color:#6897bb;">1.0</span>)<span style="color:#cc7832;">;
</span>inputArgs.setEpoch(<span style="color:#6897bb;">25</span>)<span style="color:#cc7832;">;
</span>inputArgs.setWordNgrams(<span style="color:#6897bb;">2</span>)<span style="color:#cc7832;">;
</span>
FastText model = FastText.<span style="font-style:italic;">trainSupervised</span>(trainFile<span style="color:#cc7832;">, </span>inputArgs)<span style="color:#cc7832;">;
</span>model.saveModelToSingleFile(<span style="color:#cc7832;">new </span>File(<span style="color:#6a8759;">&quot;/home/codelast/model&quot;</span>))<span style="color:#cc7832;">;</span></pre>
<p>	训练的参数，包括 lr，epoch，wordNgrams 的含义，都和 fastText 的原版一致。和 fastText 默认生成 .bin &amp; .vec 两个模型文件不同，FastText4j 可以用 saveModelToSingleFile() 方法来生成一个单一的模型文件，如果用 saveModel() 方法的话，则会在一个目录下生成4个文件（如果是这种形式的话，加载模型的时候，4个文件缺一不可）：</p>
<blockquote>
<div>
			args.bin</div>
<div>
			dict.bin</div>
<div>
			input.matrix</div>
<div>
			output.matrix</div>
</blockquote>
<div>
		如果要在Java Map-Reduce job中加载模型，把模型放到 distributed cache 中分发，当然是一个文件最方便。所以强烈建议把模型save成单一文件。<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;加载模型并测试效果</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono',monospace;font-size:12.0pt;">
<span style="color:#808080;">// </span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">加载模型
</span>FastText model = FastText.<span style="color:#9876aa;font-style:italic;">Companion</span>.loadModelFromSingleFile(<span style="color:#cc7832;">new </span>File(<span style="color:#6a8759;">&quot;/home/codelast/model&quot;</span>))<span style="color:#cc7832;">;
</span>System.<span style="color:#9876aa;font-style:italic;">out</span>.println(<span style="color:#6a8759;">&quot;load model done, will do test...&quot;</span>)<span style="color:#cc7832;">;
</span><span style="color:#808080;">// </span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">测试模型效果
</span>model.test(<span style="color:#cc7832;">new </span>File(<span style="color:#6a8759;">&quot;/home/codelast/labeled-data_valid&quot;</span>)<span style="color:#cc7832;">, </span><span style="color:#6897bb;">1</span><span style="color:#cc7832;">, </span><span style="color:#6897bb;">0</span><span style="color:#cc7832;">, true</span>)<span style="color:#cc7832;">;</span></pre>
<p>		输出：</p>
<blockquote>
<div>
				load model done, will do test...</div>
<div>
				F1-Score : 0.953652 Precision : 0.949348 Recall : 0.957996&nbsp; __label__娱乐</div>
<div>
				F1-Score : 0.704064 Precision : 0.702055 Recall : 0.706085&nbsp; __label__社会</div>
<div>
				F1-Score : 0.929426 Precision : 0.917355 Recall : 0.941818&nbsp; __label__历史</div>
<div>
				F1-Score : 0.784775 Precision : 0.784232 Recall : 0.785319&nbsp; __label__时政</div>
<div>
				F1-Score : 0.969314 Precision : 0.967568 Recall : 0.971067&nbsp; __label__汽车</div>
<div>
				F1-Score : 0.910314 Precision : 0.914414 Recall : 0.906250&nbsp; __label__时尚</div>
<div>
				F1-Score : 0.899281 Precision : 0.903614 Recall : 0.894988&nbsp; __label__健康</div>
<div>
				F1-Score : 0.929919 Precision : 0.905512 Recall : 0.955679&nbsp; __label__美食</div>
<div>
				F1-Score : 0.908136 Precision : 0.894057 Recall : 0.922667&nbsp; __label__军事</div>
<div>
				F1-Score : 0.967391 Precision : 0.975342 Recall : 0.959569&nbsp; __label__体育</div>
<div>
				F1-Score : 0.907618 Precision : 0.915033 Recall : 0.900322&nbsp; __label__育儿</div>
<div>
				F1-Score : 0.782895 Precision : 0.760383 Recall : 0.806780&nbsp; __label__情感</div>
<div>
				F1-Score : 0.863946 Precision : 0.866894 Recall : 0.861017&nbsp; __label__财经</div>
<div>
				F1-Score : 0.905188 Precision : 0.920000 Recall : 0.890845&nbsp; __label__教育</div>
<div>
				F1-Score : 0.781431 Precision : 0.792157 Recall : 0.770992&nbsp; __label__文化</div>
<div>
				F1-Score : 0.892495 Precision : 0.894309 Recall : 0.890688&nbsp; __label__游戏</div>
<div>
				F1-Score : 0.830882 Precision : 0.801418 Recall : 0.862595&nbsp; __label__科技</div>
<div>
				F1-Score : 0.795455 Precision : 0.781250 Recall : 0.810185&nbsp; __label__旅游</div>
<div>
				F1-Score : 0.843537 Precision : 0.826667 Recall : 0.861111&nbsp; __label__动漫</div>
<div>
				F1-Score : 0.960961 Precision : 0.969697 Recall : 0.952381&nbsp; __label__占卜</div>
<div>
				F1-Score : 0.915361 Precision : 0.912500 Recall : 0.918239&nbsp; __label__数码</div>
<div>
				F1-Score : 0.553191 Precision : 0.601852 Recall : 0.511811&nbsp; __label__搞笑</div>
<div>
				F1-Score : 0.788104 Precision : 0.834646 Recall : 0.746479&nbsp; __label__农林牧副渔</div>
<div>
				F1-Score : 0.797048 Precision : 0.830769 Recall : 0.765957&nbsp; __label__科学</div>
<div>
				F1-Score : 0.788462 Precision : 0.828283 Recall : 0.752294&nbsp; __label__家居</div>
<div>
				F1-Score : 0.831579 Precision : 0.877778 Recall : 0.790000&nbsp; __label__房产</div>
<div>
				F1-Score : 0.674286 Precision : 0.710843 Recall : 0.641304&nbsp; __label__生活方式</div>
<div>
				F1-Score : 0.908108 Precision : 0.933333 Recall : 0.884211&nbsp; __label__宠物</div>
<div>
				F1-Score : 0.546667 Precision : 0.546667 Recall : 0.546667&nbsp; __label__宗教</div>
<div>
				F1-Score : 0.706767 Precision : 0.671429 Recall : 0.746032&nbsp; __label__职场</div>
<div>
				F1-Score : 0.951220 Precision : 0.928571 Recall : 0.975000&nbsp; __label__天气</div>
<div>
				F1-Score : 0.666667 Precision : 0.909091 Recall : 0.526316&nbsp; __label__摄影</div>
<div>
				F1-Score : 0.707692 Precision : 0.718750 Recall : 0.696970&nbsp; __label__法律</div>
<div>
				F1-Score : 0.750000 Precision : 1.000000 Recall : 0.600000&nbsp; __label__彩票</div>
<div>
				F1-Score : 0.333333 Precision : 1.000000 Recall : 0.200000&nbsp; __label__移民</div>
<div>
				F1-Score : 0.000000 Precision : -------- Recall : 0.000000&nbsp; __label__生活百科</div>
<div>
				N<span style="white-space:pre"> </span>10703</div>
<div>
				P@1<span style="white-space:pre"> </span>0.870</div>
<div>
				R@1<span style="white-space:pre"> </span>0.870</div>
</blockquote>
<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="background-color: rgb(255, 255, 0);">✓</span>&nbsp;预测一段文本的label</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono',monospace;font-size:12.0pt;">
<span style="color:#808080;">// </span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">预测一个分好词的</span><span style="color:#808080;">string</span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">的</span><span style="color:#808080;">label
</span>List&lt;ScoreLabelPair&gt; result = model.predict(Arrays.<span style="font-style:italic;">asList</span>(<span style="color:#6a8759;">&quot;</span><span style="color:#6a8759;font-family:'DejaVu Sans Mono',monospace;">人民网 辽宁 频道 人民网 沈阳 月</span><span style="color:#6a8759;"> 10 </span><span style="color:#6a8759;font-family:'DejaVu Sans Mono',monospace;">日电 日前 进一步 增强 全民 节能 意识</span><span style="color:#6a8759;">&quot;</span>.split(<span style="color:#6a8759;">&quot; &quot;</span>))<span style="color:#cc7832;">, </span><span style="color:#6897bb;">1</span><span style="color:#cc7832;">, </span><span style="color:#6897bb;">0</span>)<span style="color:#cc7832;">;
</span>System.<span style="color:#9876aa;font-style:italic;">out</span>.println(result.get(<span style="color:#6897bb;">0</span>).getLabel())<span style="color:#cc7832;">;</span></pre>
<p>		输出：</p>
<blockquote>
<p>
				__label__社会</p>
</blockquote>
<p>		注意这里的文本应该是分好词的、空格分隔的、清洗过的文本。<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 />
		如果一个模型文件体积太大，可能放不进 distributed cache 中，所以压缩模型体积这个功能很有用。以我的模型为例，接近900MB的大小，压缩之后会变成 100 多MB，模型的Precision &amp; Recall指标却没有变差多少，值。</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'JetBrains Mono',monospace;font-size:12.0pt;">
<span style="color:#808080;">// </span><span style="color:#808080;font-family:'DejaVu Sans Mono',monospace;">压缩模型并保存。加载压缩过的模型可以节省内存
</span>FastText qmodel = model.quantize(<span style="color:#6897bb;">2</span><span style="color:#cc7832;">, false, false</span>)<span style="color:#cc7832;">;
</span>qmodel.saveModelToSingleFile(<span style="color:#cc7832;">new </span>File(<span style="color:#6a8759;">&quot;/home/codelast/model_compressed&quot;</span>))<span style="color:#cc7832;">;</span></pre>
<p>		保存成压缩过的模型是一次性的操作，以后再加载模型的话，就加载这个压缩过的模型了。</p>
<p>		<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;后记<br />
		通过 FastText4j 在&nbsp;Map-Reduce job 中并行做文本分类，成功地让文本分类任务提高了无数倍的速度，达到了实用的水平。</div>
</div>
<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>
]]></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%bb5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 使用 fastText 做中文文本分类(4)</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%bb4/</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%bb4/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 29 Jul 2020 09:47:42 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[fastText]]></category>
		<category><![CDATA[中文]]></category>
		<category><![CDATA[文本分类]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=12827</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><span style="background-color: rgb(255, 255, 0);">✓</span>&#160;开始训练第一个文本分类模型<br />
标注好的数据，其格式为：</p>
<blockquote>
<p>
		__label__科技 月 10 日 网通 社从 高合 汽车 获悉 华人 运通 微软 2020 世界 人工智能 大会 云端 峰会 WAIC 2020 上 达成 战略 合作 依托 微软 小冰 人工智能 技术 高合 汽车 上 落地 全球 首个 主动式 人工智能 伙伴 HiPhiGo 用户 提供 更好 交通 出行 体验 人工智能 交通 行业 创新 融合 发展 探讨 成立 联合 智能 计算 实验室 智能 汽车 载体 智捷 交通 多个 领域 展开 深度 合作 人工智能 前瞻 技术 研发 推动 智慧 出行 社会 持续 发展 微软 亚洲 互联网 工程院 院长 王永东 微软 华人 运通 合作 使 人工智能 技术 切实可行 落地 场景 得以 转化 真实有效 生产力 发挥 更大 价值 微软 华人 运通 携手 推进 人工智能 新兴 科技 汽车 智慧 智慧 出行 领域 广泛应用 产业 升级 社会 持续 发展 注入 新 活力 华人 运通 董事长 丁磊 此次 合作 顶级 人工智能 企业 智能 汽车 公司 强强 联手 AI 多项 领先 技术 全球 汽车行业 首次 量产 落地 世界 内 技术 领先 性 首款 智能 汽车 高合 HiPhi 有条不紊 推进 全球 首条 车路 协同 自动 驾驶 智能化 城市道路 示范 项目 盐城 开通 试运行 再 全球 首个 车路 城 一体化 5G 无人驾驶 交通 运营 样板 上海 张江 未来 公园 成功 落地 华人 运通 以人为本 人性化 需求 出发 人性化 智慧 打造 智能 汽车 智捷 交通 智慧 城市 三智 战略 各项 业务 稳步 推进 高合 首款 量产 车 HiPhi 2020 年底 小批量 试生产 2021 年 上市 交付</p>
</blockquote>
<p>按 fastText 的<a href="https://fasttext.cc/docs/en/supervised-tutorial.html" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">指南</span></a>，把这份数据按大概 9:1 的比例，分成training集和validation集，然后开始训练模型：</p>
<blockquote>
<p>
		./fasttext</p></blockquote>&#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%bb4/" 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><span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;开始训练第一个文本分类模型<br />
标注好的数据，其格式为：</p>
<blockquote>
<p>
		__label__科技 月 10 日 网通 社从 高合 汽车 获悉 华人 运通 微软 2020 世界 人工智能 大会 云端 峰会 WAIC 2020 上 达成 战略 合作 依托 微软 小冰 人工智能 技术 高合 汽车 上 落地 全球 首个 主动式 人工智能 伙伴 HiPhiGo 用户 提供 更好 交通 出行 体验 人工智能 交通 行业 创新 融合 发展 探讨 成立 联合 智能 计算 实验室 智能 汽车 载体 智捷 交通 多个 领域 展开 深度 合作 人工智能 前瞻 技术 研发 推动 智慧 出行 社会 持续 发展 微软 亚洲 互联网 工程院 院长 王永东 微软 华人 运通 合作 使 人工智能 技术 切实可行 落地 场景 得以 转化 真实有效 生产力 发挥 更大 价值 微软 华人 运通 携手 推进 人工智能 新兴 科技 汽车 智慧 智慧 出行 领域 广泛应用 产业 升级 社会 持续 发展 注入 新 活力 华人 运通 董事长 丁磊 此次 合作 顶级 人工智能 企业 智能 汽车 公司 强强 联手 AI 多项 领先 技术 全球 汽车行业 首次 量产 落地 世界 内 技术 领先 性 首款 智能 汽车 高合 HiPhi 有条不紊 推进 全球 首条 车路 协同 自动 驾驶 智能化 城市道路 示范 项目 盐城 开通 试运行 再 全球 首个 车路 城 一体化 5G 无人驾驶 交通 运营 样板 上海 张江 未来 公园 成功 落地 华人 运通 以人为本 人性化 需求 出发 人性化 智慧 打造 智能 汽车 智捷 交通 智慧 城市 三智 战略 各项 业务 稳步 推进 高合 首款 量产 车 HiPhi 2020 年底 小批量 试生产 2021 年 上市 交付</p>
</blockquote>
<p>按 fastText 的<a href="https://fasttext.cc/docs/en/supervised-tutorial.html" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">指南</span></a>，把这份数据按大概 9:1 的比例，分成training集和validation集，然后开始训练模型：</p>
<blockquote>
<p>
		./fasttext supervised -input labeled-data_train -output model</p>
</blockquote>
<p>其中，labeled-data_train 是training集文件，model 是输出模型的文件名前缀。<br />
经过一段时间的等待（速度很快），当前目录下就生成了模型文件：<span style="color:#0000ff;">model.bin</span> 和 <span style="color:#0000ff;">model.vec</span>。<br />
<span id="more-12827"></span><br />
现在可以用validation集来检验一下模型效果了：</p>
<blockquote>
<p>
		./fasttext test model.bin labeled-data_valid 1</p>
</blockquote>
<p>其中，labeled-data_valid 是validation集文件，1表示只预测top 1的label。<br />
输出：</p>
<blockquote>
<div>
		N<span style="white-space:pre"> </span>10705</div>
<div>
		P@1<span style="white-space:pre"> </span>0.842</div>
<div>
		R@1<span style="white-space:pre"> </span>0.842</div>
</blockquote>
<div>
	即 <span style="color:#0000ff;">Precision</span>为0.842，<span style="color:#0000ff;">Recall</span>为0.842。这个结果还可以，但还能优化。<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 />
	设置更详细的参数重新训练模型：</div>
<blockquote>
<div>
		./fasttext supervised -input labeled-data_train -output model -lr 1.0 -epoch 25 -wordNgrams 2</div>
</blockquote>
<div>
	其中：<br />
	-lr 1.0 表示 learning rate 设置成 1.0（通常值：0.1 ～ 1.0）。<br />
	-epoch 25 表示迭代的轮数设置成 25。<br />
	-wordNgrams 2 表示n-gram的值，一般使用2，表示2-gram。<br />
	这样训练出来的模型，再做一次test，结果变好了一些：</p>
<blockquote>
<div>
			N<span style="white-space:pre"> </span>10705</div>
<div>
			P@1<span style="white-space:pre"> </span>0.878</div>
<div>
			R@1<span style="white-space:pre"> </span>0.878</div>
</blockquote>
<div>
		这里只对模型优化做了简单的尝试。</p>
<p>		<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;交互模式下预测文本的分类<br />
		执行以下命令：</p>
<blockquote>
<p>
				./fasttext predict model.bin -</p>
</blockquote>
<p>		fastText会加载 model.bin 模型，进入交互模式，等待用户输入。<br />
		此时输入的应该是分好词之后的文本，例如&ldquo;网通 社从 高合 汽车 获悉 华人 运通 微软 2020 世界 人工智能 大会 云端 峰会 WAIC 2020 上 达成 战略 合作&rdquo;。一回车，fastText马上会返回该段文本的预测分类：</p>
<blockquote>
<p>
				__label__科技</p>
</blockquote>
<p>		这是个比较方便的调试方法。</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>
</p></div>
</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%bb4/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 使用 fastText 做中文文本分类(3)</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%bb3/</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%bb3/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 29 Jul 2020 09:47:20 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[fastText]]></category>
		<category><![CDATA[中文]]></category>
		<category><![CDATA[文本分类]]></category>
		<category><![CDATA[腾讯云]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=12796</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>为 training 数据做标注，这可能是一个艰巨的任务，也可能是一个有捷径的任务。<br />
有时候，我们可以依据一些已知的规则来标注文本，比如不同的数据是从不同的来源获取到的，从来源可以知道它们所属的类别，这是一个捷径。不过我这里不具备这样的条件。<br />
我的数据来源是网上的各种新闻，不是某些专业领域的数据，这种比较常见的文本分类任务，可以利用国内的几大云服务商提供的免费接口来完成。阿里云、腾讯云都有这样的接口。<br />
以腾讯云为例，其&#8220;人工智能&#8594;<a href="https://cloud.tencent.com/product/nlp" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">自然语言处理</span></a>&#8221;产品提供了文本分类功能：</p>
<blockquote>
<div>
		文本分类接口能够对用户输入的文本进行自动分类，将其映射到具体的类目上，用户只需要提供待分类的文本，而无需关注具体实现。</div>
<div>
		该功能基于千亿级大规模互联网语料和LSTM、BERT等深度神经网络模型进行训练，并持续迭代更新，以保证效果不断提升。</div>
<div>
		目前已提供：</div>
<div>
		● 通用领域分类体系，包括15个分类类目，分别是汽车、科技、健康、体育、旅行、教育、职业、文化、军事、房产、娱乐、女性、奥运、财经以及其他，适用于通用的场景。</div>
<div>
		● 新闻领域分类体系，包括37个一级分类类目，285个二级分类（详细请见 类目体系映射表），已应用于腾讯新闻的文章分类。</div>
<div>
		更多垂直领域的分类体系即将推出，敬请期待。</div>
<div>
		默认接口请求频率限制：20次/秒。</div>
</blockquote>
<div>
	该API每天有50万次免费调用额度，用来标注数据够用了：<br />
	<a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="tencent cloud nlp free resource" src="https://www.codelast.com/wp-content/uploads/2020/07/tencent_nlp_free_resource.png" style="width: 375px; height: 81px;" /></a><br />
	如果你对这个接口的分类结果准确性有疑虑的话，可以亲自拿一些新闻的文本试一试，就会发现它的效果真的不错，完全可以用来当作人工标注的结果了，毕竟是大厂出品嘛。<br />
	<span id="more-12796"></span><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 />
	下面，就来看看怎么薅腾讯云的羊毛。注册腾讯云账号、开通自然语言处理接口的权限，这些就不用说了，自己在网页上点几下就可以完成。<br />
	<span style="background-color: rgb(255, 255, 0);">✓</span>&#160;测试接口<br />
	腾讯云NLP接口提供了一个在线测试的网页，从<a href="https://cloud.tencent.com/document/product/271/35496" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">这个</span></a>链接可以进去（&#8220;<span style="color:#0000ff;">点击调试</span>&#8221;）：</div>
<p><img decoding="async" alt="tencent cloud nlp debug entrance" src="https://www.codelast.com/wp-content/uploads/2020/07/tencent_nlp_debug_entrance.png" style="width: 700px; height: 137px;" /></p>
<p><span style="color:#0000ff;">进入调试页面：</span><br />
<a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="" src="https://www.codelast.com/wp-content/uploads/2020/07/tencent_nlp_web_debug.png" style="width: 700px; height: 413px;" /></a><br />
对这个接口：<br />
Region只能选&#8220;华南地区(广州)&#8221;。<br />
Text是待分类的文本，注意<span style="color:#ff0000;">不需要分词</span>。只需要把我们前面清洗过（去掉了HTML tag等内容）的句子拿来用即可。<br />
Flag为&#160;1 表示通用领域，为 2 表示新闻领域。<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>&#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%bb3/" class="read-more">Read More </a></p>]]></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>为 training 数据做标注，这可能是一个艰巨的任务，也可能是一个有捷径的任务。<br />
有时候，我们可以依据一些已知的规则来标注文本，比如不同的数据是从不同的来源获取到的，从来源可以知道它们所属的类别，这是一个捷径。不过我这里不具备这样的条件。<br />
我的数据来源是网上的各种新闻，不是某些专业领域的数据，这种比较常见的文本分类任务，可以利用国内的几大云服务商提供的免费接口来完成。阿里云、腾讯云都有这样的接口。<br />
以腾讯云为例，其&ldquo;人工智能&rarr;<a href="https://cloud.tencent.com/product/nlp" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">自然语言处理</span></a>&rdquo;产品提供了文本分类功能：</p>
<blockquote>
<div>
		文本分类接口能够对用户输入的文本进行自动分类，将其映射到具体的类目上，用户只需要提供待分类的文本，而无需关注具体实现。</div>
<div>
		该功能基于千亿级大规模互联网语料和LSTM、BERT等深度神经网络模型进行训练，并持续迭代更新，以保证效果不断提升。</div>
<div>
		目前已提供：</div>
<div>
		● 通用领域分类体系，包括15个分类类目，分别是汽车、科技、健康、体育、旅行、教育、职业、文化、军事、房产、娱乐、女性、奥运、财经以及其他，适用于通用的场景。</div>
<div>
		● 新闻领域分类体系，包括37个一级分类类目，285个二级分类（详细请见 类目体系映射表），已应用于腾讯新闻的文章分类。</div>
<div>
		更多垂直领域的分类体系即将推出，敬请期待。</div>
<div>
		默认接口请求频率限制：20次/秒。</div>
</blockquote>
<div>
	该API每天有50万次免费调用额度，用来标注数据够用了：<br />
	<a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="tencent cloud nlp free resource" src="https://www.codelast.com/wp-content/uploads/2020/07/tencent_nlp_free_resource.png" style="width: 375px; height: 81px;" /></a><br />
	如果你对这个接口的分类结果准确性有疑虑的话，可以亲自拿一些新闻的文本试一试，就会发现它的效果真的不错，完全可以用来当作人工标注的结果了，毕竟是大厂出品嘛。<br />
	<span id="more-12796"></span><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 />
	下面，就来看看怎么薅腾讯云的羊毛。注册腾讯云账号、开通自然语言处理接口的权限，这些就不用说了，自己在网页上点几下就可以完成。<br />
	<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;测试接口<br />
	腾讯云NLP接口提供了一个在线测试的网页，从<a href="https://cloud.tencent.com/document/product/271/35496" rel="noopener noreferrer" target="_blank"><span style="background-color:#fff0f5;">这个</span></a>链接可以进去（&ldquo;<span style="color:#0000ff;">点击调试</span>&rdquo;）：</div>
<p><img decoding="async" alt="tencent cloud nlp debug entrance" src="https://www.codelast.com/wp-content/uploads/2020/07/tencent_nlp_debug_entrance.png" style="width: 700px; height: 137px;" /></p>
<p><span style="color:#0000ff;">进入调试页面：</span><br />
<a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="" src="https://www.codelast.com/wp-content/uploads/2020/07/tencent_nlp_web_debug.png" style="width: 700px; height: 413px;" /></a><br />
对这个接口：<br />
Region只能选&ldquo;华南地区(广州)&rdquo;。<br />
Text是待分类的文本，注意<span style="color:#ff0000;">不需要分词</span>。只需要把我们前面清洗过（去掉了HTML tag等内容）的句子拿来用即可。<br />
Flag为&nbsp;1 表示通用领域，为 2 表示新闻领域。<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 />
接口返回的数据是JSON格式，FirstClassName 是一级分类的名称，SecondClassName 是二级分类的名称。<br />
调试页面还提供了&ldquo;代码生成&rdquo;功能，Java、Python、PHP、Go等都有，支持非常全面。选一个你熟悉的语言，就可以在代码里实现了。</p>
<p>由于免费的接口的QPS上限为20次/秒，所以并行什么的就不要想了，单线程就能跑满。<br />
通过腾讯云的接口，我们能在半天内为几十万条文本打上标签，再整理成 fastText 规定的格式，就可以训练模型了。<br />
如果觉得50万条都不够多，可以注册多个腾讯云账号来薅羊毛，或者用几天的时间累积够标注数据。</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>
]]></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%bb3/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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>
		<item>
		<title>[原创] 使用 fastText 做中文文本分类(1)</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%bb1/</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%bb1/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 29 Jul 2020 09:46:10 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[fastText]]></category>
		<category><![CDATA[中文]]></category>
		<category><![CDATA[文本分类]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=12754</guid>

					<description><![CDATA[<p>查看本系列文章合集，请看<a href="https://www.codelast.com/?p=12856" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">这里</span></a>。</p>
<p><span style="background-color: rgb(255, 255, 0);">✓</span>&#160;什么是&#8220;文本分类&#8221;</p>
<blockquote>
<p>
		它是图书馆学， 信息学和计算机科学中的一个问题。其任务是将一个文档分配到一个或者多个类别中。</p>
</blockquote>
<p>举例：</p>
<table selecttype="cells" style="border-collapse: collapse;">
<colgroup>
<col style="width: 550px;" width="550" />
<col style="width: 107px;" width="107" />
	</colgroup>
<tbody>
<tr height="24" style="height: 24px;">
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; white-space: nowrap; text-align: center; font-family: &#34;Microsoft YaHei&#34;; font-size: 10pt; background-color: rgb(223, 248, 255); border-width: 0.5pt; border-style: solid; border-color: rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">文本</font></td>
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; white-space: nowrap; text-align: center; font-family: &#34;Microsoft YaHei&#34;; font-size: 10pt; background-color: rgb(223, 248, 255); border-width: 0.5pt; border-style: solid; border-color: rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">类别</font></td>
</tr>
<tr height="24" style="height: 24px;">
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; font-family: &#34;Microsoft YaHei&#34;; font-size: 10pt; border-top: 0.5pt solid rgb(0, 0, 0); border-right: 0.5pt solid rgb(0, 0, 0); border-left: 0.5pt solid rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">瑞幸咖啡今日发布声明称，瑞幸咖啡公司将于6月29日在纳斯达克停牌，并进行退市备案。声明表示，在国内消费市场方面，瑞幸咖啡全国4000多家门店将正常运营，近3万名员工仍将一如既往的为用户提供优质产品和服务。公司衷心感谢广大消费者的支持厚爱，并再次为事件造成的恶劣影响向社会各界诚挚道歉。</font></td>
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; white-space: nowrap; text-align: center; font-family: &#34;Microsoft YaHei&#34;; font-size: 10pt; border-top: 0.5pt solid rgb(0, 0, 0); border-right: 0.5pt solid rgb(0, 0, 0); border-left: 0.5pt solid rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">财经</font></td>
</tr>
<tr height="24" style="height: 24px;">
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; font-family: &#34;Microsoft YaHei&#34;; font-size: 10pt; border-width: 0.5pt; border-style: solid; border-color: rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">明朝中后期西南沿海一个重要的问题就是倭寇的骚扰，这个问题本来是有希望被当时的浙江总督用和平的方法解决掉的，但是中途就出了个王本固这个插曲。</font></td>
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; white-space: nowrap; text-align: center; font-family: &#34;Microsoft YaHei&#34;; font-size: 10pt; border-width: 0.5pt; border-style: solid; border-color: rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">历史</font></td>
</tr>
</tbody>
</table>
<p>
其中，&#8220;类别&#8221;是事先定义好的。一段文本可以属于多个类别，例如，第2个例子可以同时属于&#8220;历史&#8221;和&#8220;明朝&#8221;这两个类别。<br />
<span id="more-12754"></span><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&#160;fastText是什么<br />
fastText是Facebook于2016年开源的一个词向量计算和<span style="color:#0000ff;">文本分类</span>工具，官方定义：</p>
<blockquote>
<p>
		Library for efficient text classification and representation learning</p>
</blockquote>
<p>工具如其名，&#8220;fast&#8221;，表明它的速度很快。<br />
fastText应用很广泛，本文以<span style="color:#0000ff;">只有1个类别</span>的<span style="color:#ff0000;">中文</span>文本分类任务为例，展示如何从源数据开始，到数据处理，到最后完成分类的过程。<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>&#160;安装fastText<br />
fastText 有一个在命令行直接就能用的可执行程序，也有Python的接口，这两样东西我们都装上。<br />
安装、使用fastText对编译器版本、Python版本都有一定的要求，可以查看fastText网站了解这些信息。</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: &#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="bash language-bash 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;">wget&#160;https://github.com/facebookresearch/fastText/archive/v0.9.2.zip</code></pre>&#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%bb1/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>查看本系列文章合集，请看<a href="https://www.codelast.com/?p=12856" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">这里</span></a>。</p>
<p><span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;什么是&ldquo;文本分类&rdquo;</p>
<blockquote>
<p>
		它是图书馆学， 信息学和计算机科学中的一个问题。其任务是将一个文档分配到一个或者多个类别中。</p>
</blockquote>
<p>举例：</p>
<table selecttype="cells" style="border-collapse: collapse;">
<colgroup>
<col style="width: 550px;" width="550" />
<col style="width: 107px;" width="107" />
	</colgroup>
<tbody>
<tr height="24" style="height: 24px;">
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; white-space: nowrap; text-align: center; font-family: &quot;Microsoft YaHei&quot;; font-size: 10pt; background-color: rgb(223, 248, 255); border-width: 0.5pt; border-style: solid; border-color: rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">文本</font></td>
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; white-space: nowrap; text-align: center; font-family: &quot;Microsoft YaHei&quot;; font-size: 10pt; background-color: rgb(223, 248, 255); border-width: 0.5pt; border-style: solid; border-color: rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">类别</font></td>
</tr>
<tr height="24" style="height: 24px;">
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; font-family: &quot;Microsoft YaHei&quot;; font-size: 10pt; border-top: 0.5pt solid rgb(0, 0, 0); border-right: 0.5pt solid rgb(0, 0, 0); border-left: 0.5pt solid rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">瑞幸咖啡今日发布声明称，瑞幸咖啡公司将于6月29日在纳斯达克停牌，并进行退市备案。声明表示，在国内消费市场方面，瑞幸咖啡全国4000多家门店将正常运营，近3万名员工仍将一如既往的为用户提供优质产品和服务。公司衷心感谢广大消费者的支持厚爱，并再次为事件造成的恶劣影响向社会各界诚挚道歉。</font></td>
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; white-space: nowrap; text-align: center; font-family: &quot;Microsoft YaHei&quot;; font-size: 10pt; border-top: 0.5pt solid rgb(0, 0, 0); border-right: 0.5pt solid rgb(0, 0, 0); border-left: 0.5pt solid rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">财经</font></td>
</tr>
<tr height="24" style="height: 24px;">
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; font-family: &quot;Microsoft YaHei&quot;; font-size: 10pt; border-width: 0.5pt; border-style: solid; border-color: rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">明朝中后期西南沿海一个重要的问题就是倭寇的骚扰，这个问题本来是有希望被当时的浙江总督用和平的方法解决掉的，但是中途就出了个王本固这个插曲。</font></td>
<td style="empty-cells: show; line-height: normal; vertical-align: middle; outline-width: 0px; overflow-wrap: break-word; word-break: normal; white-space: nowrap; text-align: center; font-family: &quot;Microsoft YaHei&quot;; font-size: 10pt; border-width: 0.5pt; border-style: solid; border-color: rgb(0, 0, 0);">
				<font style="white-space: pre-wrap; font-size: 10pt;">历史</font></td>
</tr>
</tbody>
</table>
<p>
其中，&ldquo;类别&rdquo;是事先定义好的。一段文本可以属于多个类别，例如，第2个例子可以同时属于&ldquo;历史&rdquo;和&ldquo;明朝&rdquo;这两个类别。<br />
<span id="more-12754"></span><br />
<span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;fastText是什么<br />
fastText是Facebook于2016年开源的一个词向量计算和<span style="color:#0000ff;">文本分类</span>工具，官方定义：</p>
<blockquote>
<p>
		Library for efficient text classification and representation learning</p>
</blockquote>
<p>工具如其名，&ldquo;fast&rdquo;，表明它的速度很快。<br />
fastText应用很广泛，本文以<span style="color:#0000ff;">只有1个类别</span>的<span style="color:#ff0000;">中文</span>文本分类任务为例，展示如何从源数据开始，到数据处理，到最后完成分类的过程。<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;安装fastText<br />
fastText 有一个在命令行直接就能用的可执行程序，也有Python的接口，这两样东西我们都装上。<br />
安装、使用fastText对编译器版本、Python版本都有一定的要求，可以查看fastText网站了解这些信息。</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="bash language-bash 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;">wget&nbsp;https://github.com/facebookresearch/fastText/archive/v0.9.2.zip
unzip&nbsp;v0.9.2.zip
<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">cd</span>&nbsp;fastText-0.9.2
make
pip&nbsp;install&nbsp;.
</code></pre>
</section>
<p>本文写作时，fastText的最新版是 0.9.2，如果版本有更新，上面的命令要跟着修改。<br />
我们会看到当前目录下生成了一个名为&ldquo;fasttext&rdquo;的可执行程序。这样，fastText就可以开始用了。但这只是万里长征第一步，之后的文本预处理、类别标注才是最麻烦的。</p>
<p><span style="background-color: rgb(255, 255, 0);">✓</span>&nbsp;文本分类的流程<br />
并不是我们有了文本数据（一行一个字符串），就能直接喂给fastText训练出来一个分类模型了，我们不仅要对数据进行一些细致的清洗，还要把输入fastText的数据做成它要求的格式，这样它才能识别并训练出模型。</p>
<p>简单地说，文本分类的过程主要由以下步骤构成：<br />
● 对待分类的文本数据做预处理。例如：去掉文本里的乱七八糟的字符、标点符号，等等。并且，由于中文不像英文有天然的空格做为词与词之间的分隔符，因此，我们只能使用分词器来对中文句子进行分词。<br />
● 对文本进行标注。fastText训练的是有监督的文本分类模型，你得先给它一堆例子：什么样的文本，对应的正确分类是什么。这样它才能学出一个模型，用于预测一个没有见过的文本的类别。标注可以由人工完成（效率低，一般不现实），也可以从一些可信的数据源获取文本的正确分类。<br />
● 把处理好的文本数据拆分成training集和validation集，training集用于训练模型，validation集用于检验模型效果。<br />
● 用fastText训练training集得到一个模型，用validation集检验模型效果，如果效果不好，调整模型训练的参数训练一个新模型继续检验效果。</p>
<p>下面，就依次描述以上各项。请看下一篇文章。</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>
]]></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%bb1/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
