最近遇到了一个问题,怎么对字符串解析后,进行分块存储。比如说:
我有五个表,
t1,t2,t3,t4,t5现在要插入一条信息,
$a = "要插入的信息标题是这样子的";问题是,如何通过将这个字符串来划分,需储到哪个表中?找到字符串的某个特性来判断一下。而且,因为要进行模糊搜索,所以还要考虑到提出其中的某几个字,也能算出是包含在哪个表中的。求这个算法的提示!
不知道大家有没有看懂我的意思。请大家帮帮忙给些思路。
我有五个表,
t1,t2,t3,t4,t5现在要插入一条信息,
$a = "要插入的信息标题是这样子的";问题是,如何通过将这个字符串来划分,需储到哪个表中?找到字符串的某个特性来判断一下。而且,因为要进行模糊搜索,所以还要考虑到提出其中的某几个字,也能算出是包含在哪个表中的。求这个算法的提示!
不知道大家有没有看懂我的意思。请大家帮帮忙给些思路。
你好,因为我的总体需求是因为存储量大,而需要分表存储来减轻数据库负担,所以我想根据标题来划分出不同的区块来存储,比如说算出“我们的”这三个字的某个值为1,然后,我的表t1,是存储1-10的,那么这个标题的信息就存储到t1表中,然后,搜索的时候,搜索“我”这个字,算出他的值,应该是在t1这个表搜索,就是这个标准来怎么划分我实在想不出来,恳请你能帮我一下。
$arr = array(
// 中文算法的词根
1=>array('我的', '你的'),
2=>array('他的', '谁的')
);$str = '我的给谁的';$word_arr = 中文切词算法($str);$rate = 0;
foreach($word_arr as $word) {
if($arr中含有词根) {
$rate += $arr中的key;
}
}$rate = $rate % 10;// 下面你随便处理
比如说:
$str = '快拉萨的飞机历史的飞机我拉架了刷卡的积分 ';
echo ord(substr(md5($str), 0, 1)) % 5;
得到的是2$str = '积分';
echo ord(substr(md5($str), 0, 1)) % 5;
得到的是3那么我就没办法去指定的库搜索了
你又听不进去,这函数好用的很,我都用好几年了,看下面的例子$words那几个词只要固定死了,相互间不要太接近(要从字码方面考虑),分类就算完成了
即使$input怎么换,也能找出最相似的,$input你自己不断换不同的内容测试一下就知道了
关键是设好$words就足够了<?php
// input misspelled word
$input = '麒sf麟g345厍%#%人是个大坏蛋banana';// array of words to check against
$words = array('苹果','pineapple','大banana','orange',
'radish','麒麟坏蛋','pea','bean','坏蛋');// no shortest distance found, yet
$shortest = -1;// loop through words to find the closest
foreach ($words as $word) { // calculate the distance between the input word,
// and the current word
$lev = levenshtein($input, $word); // check for an exact match
if ($lev == 0) { // closest word is this one (exact match)
$closest = $word;
$shortest = 0; // break out of the loop; we've found an exact match
break;
} // if this distance is less than the next found shortest
// distance, OR if a next shortest word has not yet been found
if ($lev <= $shortest || $shortest < 0) {
// set the closest match, and shortest distance
$closest = $word;
$shortest = $lev;
}
}echo "Input word: $input\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}?>
这咚咚还能用来河蟹,即使输入“河&*^*蟹”也能找出来,所以我一般不愿意直接贴例子
看你问了又问,真是看不下去了……中文分词是世界难题,不要再从这方面想了,留给砖家做吧
levenshtein() 是有数学算法原理在里面的,不是简单比较匹配,
有兴趣自己再深入查一下“string pattern match”会有更多算法
+3楼主对自己的需要并不很清楚.按照楼主的说法,
假设AAA在a表,BBB在b表,那么AAA BBB这个标题应该在哪个表?
AAA标题也进a表,BBB标题也进b表,然后搜索A,AA,AAA都去a表去搜索标题包含A的内容