Sqlite China  
首页 | 各种语言的sqlite编程 |sqlite研究 | sqlite应用实例与杂谈 | sqlite相关下载 | SQlite论坛
当前位置 : 主页>各种语言的sqlite编程>列表
走进全文搜索(PHP+SQLite) 二
来源:工友 作者:工友 时间:2007-12-21
下面是两个分词算法,一个是二元分词,一个是词典分词:

<?php
function _tokenizer ($text)
 {
  
// UTF8_only
  // 2-Base Cut
  
$len strlen ($text);
  
$mbc '';
  
$last_mbc '';
  
$tmp '';
  
$tokens = array ();
  
  for (
$i 0$i $len$i++) {
   
$c $text[$i];
   
$v ord ($c);
   
   if (
$v 0xe0) {
    
// 3-bytes chars
    
$tmp '';
    
$mbc $c $text[$i 1] . $text[$i 2];
    
$i += 2;
   }
   
   elseif (
$v 0xc0) {
    
// 2-bytes chars
    
$tmp '';
    
$mbc $c $text[$i 1];
    
$i ++;
   }
   
   else {
    
$mbc '';
    if (
$c == ' ') {
     if (
$tmp) {
      
$p $i strlen ($tmp);
      
$tokens[$p] = $tmp;
     }
     
     
$tmp '';
    }
    else {
     
$tmp .= $c;
    }
   }
   
   if (
$mbc) {
    if (
$last_mbc) {
     
$p $i strlen ($last_mbc $mbc) + 1;
     
$tokens[$p] = $last_mbc $mbc;
    }
    
$last_mbc $mbc;
   }
   
   else {
    
$last_mbc '';
   }
  }
  
  return 
$tokens;
 }
 
 function 
_tokenizer_dict ($text$non_word false)
 {
  
$len strlen ($text);
  
$mbc '';
  
//$mbc_str = '';
  
$mbc_str = array ();
  
$tmp '';
  
$tokens = array ();
  
  for (
$i 0$i $len$i++) {
   
$c $text[$i];
   
$v ord ($c);
   
   if (
$v 0xe0) {
    
// 3-bytes chars
    
$tmp '';
    
$mbc $c $text[$i 1] . $text[$i 2];
    
$i += 2;
   }
   
   elseif (
$v 0xc0) {
    
// 2-bytes chars
    
$tmp '';
    
$mbc $c $text[$i 1];
    
$i ++;
   }
   
   else {
    
$mbc '';
    if (
$c == ' ') {
     if (
$tmp) {
      
$p $i strlen ($tmp);
      
$tokens[$p] = $tmp;
     }
     
     
$tmp '';
    }
    
    else {
     
$tmp .= $c;
    }
    
    if (
count ($mbc_str) > 0) {
     
// Div_dict
     //mb_internal_encoding ('UTF-8');
     
$start_offset $i strlen (implode (''$mbc_str));
     
$mbc_str_left $mbc_str;
     while (
count ($mbc_str_left)) {
      
//$mb_len = mb_strlen ($mbc_str_left);
      
$mb_len count ($mbc_str_left);
      
$word '';
      
      for (
$j = ($mb_len $mb_len); $j >= 1$j --) {
       
//$test = mb_substr ($mbc_str_left, 0, $j);
       
$test '';
       for (
$k 0$k $j$k++) {
        
$test .= $mbc_str_left[$k];
       }
       
       
//$mb_test_len = mb_strlen ($test);
       
if ($j == 1) {
        
// 1 only
        
$word $test;
       }
       
       else {
        if (
$this->dict->find ($test)) {
         
$word $test;
        }
       }
       
       if (
$word) {
        
//$mbc_str_left = mb_substr ($mbc_str_left, $mb_test_len);
        
        
$arr_tmp = array ();
        for (
$k $j$k $mb_len$k++) {
         
$arr_tmp[] = $mbc_str_left[$k];
        }
        
        
$mbc_str_left $arr_tmp;
        if (!
$non_word) {
         if (
$j 1)
          
$tokens[$start_offset] = $word;
        }
        else
         
$tokens[$start_offset] = $word;
        
        
$start_offset += strlen ($word);
        continue 
2;
       }
      }
     }
    }
    
    
//$mbc_str = '';
    
$mbc_str = array ();
   }
   
   if (
$mbc) {
    
$mbc_str[] = $mbc;
   }
  }
  
  return 
$tokens;
 }
?>
(阅读次数:
上一篇:走进全文搜索(PHP+SQLite) 一 下一篇:走进全文搜索(PHP+SQLite) 三
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
§最新评论
热点文章
·简单的在Java中使用SQLite
·C/C++中调用SQLITE3的基本
·PHP+SQLITE制作简单的视频
·DISQLite3 简介 SQLite de
·VB.NET 中使用 SQLite3 的
·SQLiteJDBC 100%纯JAVA的s
·如何在Windows下编译SQLit
·关于SQLite的一些简单介绍
·在VC6.0中使用C++访问sqli
·C/C++中调用SQLITE3的基本
·SQLite 与 PHP 结合开发(
·在VC6.0中使用C++访问sqli
·使用SQLite进行网站搜索
·SQLite ADO.NET 驱动(C#
·在.NET C#中使用sqlite
·python模块之sqlite数据库
·PHP中的SQlite数据库应用
·如何在PHP5中通过PDO连接S
·PHP中如何使用sqlite_crea
·SQLite 、 PHP混合扩展编
相关文章
·让你的PHP4也用上Sqlite3
·VB.NET 中使用 SQLite3 的
·C/C++中调用SQLITE3的基本
·python模块之sqlite数据库
·在.NET C#中使用sqlite
·用Ruby进行SQLite的开发指
·PHP5中的 sqlite_create_f
·SQLite ADO.NET 驱动(C#
·使用SQLite进行网站搜索
·如何在Windows下编译SQLit

版权Power by DedeCms   后台登陆
Copyright @ 2007