<?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>Luin&#039;s Blog &#187; LAMP</title>
	<atom:link href="http://luinlee.com/category/study-tech/web/lamp/feed/" rel="self" type="application/rss+xml" />
	<link>http://luinlee.com</link>
	<description>A long river</description>
	<lastBuildDate>Sat, 07 Aug 2010 04:55:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>有关中文分词技术和搜索结果按相似度排序</title>
		<link>http://luinlee.com/448/%e6%9c%89%e5%85%b3%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e6%8a%80%e6%9c%af%e5%92%8c%e6%90%9c%e7%b4%a2%e7%bb%93%e6%9e%9c%e6%8c%89%e7%9b%b8%e4%bc%bc%e5%ba%a6%e6%8e%92%e5%ba%8f/</link>
		<comments>http://luinlee.com/448/%e6%9c%89%e5%85%b3%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e6%8a%80%e6%9c%af%e5%92%8c%e6%90%9c%e7%b4%a2%e7%bb%93%e6%9e%9c%e6%8c%89%e7%9b%b8%e4%bc%bc%e5%ba%a6%e6%8e%92%e5%ba%8f/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 08:19:15 +0000</pubDate>
		<dc:creator>Luin</dc:creator>
				<category><![CDATA[LAMP]]></category>

		<guid isPermaLink="false">http://luinlee.com/?p=448</guid>
		<description><![CDATA[做小网站的站内搜索时要考虑到用户的查询关键字未必和数据库中相应数据准确对应，所以搜索时就要用到模糊查询。但是很多情况下Like语句也未必管用，比如用户搜索“打篮球”，可是数据库里只有“篮球”，这时做查询“LIKE &#8216;%打篮球%&#8217;”肯定不会找到结果。这时就需要对关键字进行分词。对于英文来说可以简单的根据空格来划分单词，而对于中文就有些麻烦。 好在现在有些开源的中文分词引擎分词的准确度已经相当高了，如PHP的SCWS可以把“打篮球”分成“打”和“篮球”，这样再分别查找就可以把“篮球”条目找到。然后由于SCWS是基于词频词典的，所以如果有些新词就分不出来了，比如“我爱美女”可以分解成“我爱”和“美女”，可是“我爱凤姐”就只能分成“我爱”“凤”和“姐”了。 在无法根据词库分词的情况下，我们可以采用二元分词法，即全部分成两个字组成的词，如“我爱凤姐”分成“我爱”“爱凤”“凤姐”。代码也很简单： function Fenci&#40;$word&#41; &#123; $len = mb_strlen&#40;$word, 'GBK'&#41;; for&#40;$i = 0; $i &#38;lt; $len - 1; ++$i&#41; $result&#91;&#93; = mb_substr&#40;$word, $i, 2, 'GBK'&#41;; return $result; &#125; 然而由于词语的错分也产生了一些无关的结果，如搜索“全国计算机等级考试”，SCWS会把它分成“全国”“计算机”“等级”“考试”，而数据库里有“计算机等级考试”和“大学英语水平考试”，这时这两条结果全部命中，可是后者显然不是我们想要的结果，这就需要多结果进行相似的排序。 PHP提供了一个函数可以轻易实现这个功能。它的名字就是&#8230;similar_text，如similar_text(&#8216;全国计算机等级考试&#8217;, &#8216;大学英语水平考试&#8217;, $percent), $percent返回两个字符串的相似度（百分比），测试后可知“计算机等级考试”更接近搜索词，所以把它排前面。]]></description>
			<content:encoded><![CDATA[<p><a class="highslide img_2" href="http://luinlee.com/wp-content/uploads/2010/07/Untitled-1.png" onclick="return hs.expand(this)"><img class="alignleft size-full wp-image-451" title="我爱凤姐" src="http://luinlee.com/wp-content/uploads/2010/07/Untitled-1.png" alt="" width="328" height="158" /></a>做小网站的站内搜索时要考虑到用户的查询关键字未必和数据库中相应数据准确对应，所以搜索时就要用到模糊查询。但是很多情况下Like语句也未必管用，比如用户搜索“打篮球”，可是数据库里只有“篮球”，这时做查询“LIKE &#8216;%打篮球%&#8217;”肯定不会找到结果。这时就需要对关键字进行分词。对于英文来说可以简单的根据空格来划分单词，而对于中文就有些麻烦。</p>
<p>好在现在有些开源的中文分词引擎分词的准确度已经相当高了，如PHP的<a href="http://www.ftphp.com/scws/">SCWS</a>可以把“打篮球”分成“打”和“篮球”，这样再分别查找就可以把“篮球”条目找到。然后由于SCWS是基于词频词典的，所以如果有些新词就分不出来了，比如“我爱美女”可以分解成“我爱”和“美女”，可是“我爱凤姐”就只能分成“我爱”“凤”和“姐”了。</p>
<p>在无法根据词库分词的情况下，我们可以采用二元分词法，即全部分成两个字组成的词，如“我爱凤姐”分成“我爱”“爱凤”“凤姐”。代码也很简单：</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> Fenci<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$len</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mb_strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'GBK'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #000088;">$len</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$result</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mb_substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #339933;">,</span> <span style="color: #000088;">$i</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'GBK'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$result</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>然而由于词语的错分也产生了一些无关的结果，如搜索“全国计算机等级考试”，SCWS会把它分成“全国”“计算机”“等级”“考试”，而数据库里有“计算机等级考试”和“大学英语水平考试”，这时这两条结果全部命中，可是后者显然不是我们想要的结果，这就需要多结果进行相似的排序。<br />
PHP提供了一个函数可以轻易实现这个功能。它的名字就是&#8230;similar_text，如similar_text(&#8216;全国计算机等级考试&#8217;, &#8216;大学英语水平考试&#8217;, $percent), $percent返回两个字符串的相似度（百分比），测试后可知“计算机等级考试”更接近搜索词，所以把它排前面。</p>
]]></content:encoded>
			<wfw:commentRss>http://luinlee.com/448/%e6%9c%89%e5%85%b3%e4%b8%ad%e6%96%87%e5%88%86%e8%af%8d%e6%8a%80%e6%9c%af%e5%92%8c%e6%90%9c%e7%b4%a2%e7%bb%93%e6%9e%9c%e6%8c%89%e7%9b%b8%e4%bc%bc%e5%ba%a6%e6%8e%92%e5%ba%8f/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tips:PHP中截取中文字符串避免乱码</title>
		<link>http://luinlee.com/261/tips-phpstring/</link>
		<comments>http://luinlee.com/261/tips-phpstring/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 03:20:00 +0000</pubDate>
		<dc:creator>Luin</dc:creator>
				<category><![CDATA[LAMP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://luinlee.com/?p=261</guid>
		<description><![CDATA[使用substr截取中文字符串可能会出现意外的乱码，而不同编码下中文字符串长度是不同的，特殊情况比较多。下面的csubstr()函数是我在开发博客巢系统时编写的，它用了一个小方法：strlen(&#8216;汉&#8217;)比较取巧的解决了这个问题。 (2010-01-23：根据Gill的回复似乎这样做并不稳妥，回来再好好研究研究，有更好的方法欢迎与我交流) /*截取字符串(避免中文乱码)*/ function csubstr&#40;$string,$sublength&#41; &#123; $len = strlen&#40;$string&#41;; if &#40;$len &#38;lt;= $sublength&#41;&#123; $string = $string; &#125;else&#123; $string = substr&#40;$string,0,$sublength&#41;; $parity= 0; for&#40;$j=0;$j&#38;lt;$sublength;$j++&#41;&#123; $temp_str=substr&#40;$string,$j,1&#41;; if&#40;Ord&#40;$temp_str&#41;&#38;gt;127&#41; $parity+=1; &#125; $n = $parity % strlen&#40;'汉'&#41;; if&#40;$n == 0&#41; &#123; $string=substr&#40;$string,0,$sublength&#41;; &#125; else &#123; $string=substr&#40;$string,0,$sublength - $n&#41;; &#125; &#125; return $string; &#125; 更新：可以在设定编码的前提下使用mb_substr避免乱码： string mb_substr &#40; string $str , [...]]]></description>
			<content:encoded><![CDATA[<p>使用substr截取中文字符串可能会出现意外的乱码，而不同编码下中文字符串长度是不同的，特殊情况比较多。下面的csubstr()函数是我在开发博客巢系统时编写的，它用了一个小方法：strlen(&#8216;汉&#8217;)比较取巧的<del datetime="2010-01-23T15:49:56+00:00">解决了这个问题。</del></p>
<p><strong>(2010-01-23：根据Gill的回复似乎这样做并不稳妥，回来再好好研究研究，有更好的方法欢迎与我交流)</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/*截取字符串(避免中文乱码)*/</span>
<span style="color: #000000; font-weight: bold;">function</span> csubstr<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #000088;">$sublength</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$len</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$len</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #000088;">$sublength</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$sublength</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$parity</span><span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$j</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span><span style="color: #000088;">$j</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span><span style="color: #000088;">$sublength</span><span style="color: #339933;">;</span><span style="color: #000088;">$j</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$temp_str</span><span style="color: #339933;">=</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #000088;">$j</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">Ord</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$temp_str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #cc66cc;">127</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$parity</span><span style="color: #339933;">+=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$parity</span> <span style="color: #339933;">%</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'汉'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$string</span><span style="color: #339933;">=</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$sublength</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$string</span><span style="color: #339933;">=</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #000088;">$sublength</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$string</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>更新：可以在设定编码的前提下使用mb_substr避免乱码：</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">string <span style="color: #990000;">mb_substr</span> <span style="color: #009900;">&#40;</span> string <span style="color: #000088;">$str</span> <span style="color: #339933;">,</span> int <span style="color: #000088;">$start</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> int <span style="color: #000088;">$length</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> string <span style="color: #000088;">$encoding</span> <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span></pre></div></div>

<p><strong>详见：<cite><a href="http://php.net/manual/en/function.mb-substr.php">http://php.net/manual/en/function.mb-substr.php</a></cite></strong></p>
<p>感谢Gill的回复</p>
]]></content:encoded>
			<wfw:commentRss>http://luinlee.com/261/tips-phpstring/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
