mysql 实现TF-IDF 遍历不定个数的列

有这样一道题,要求使用纯mysql实现一个TF-IDF算法。

原始的输入是一个有articles表,有100列,每列存储一个单词。其实核心难点就是怎么遍历对比这100个词和指定词比如'apple'进行对比。首先蛮力穷举所有的列名,如word1 、word2。。。但是这样做代码肯定丑的不像样,而且如果是1000列怎么办呢。

 

要遍历列的话第一步是要把列名取出来。

create temporary table column_name  #建立临时表

select ORDINAL_POSITION, COLUMN_NAME #选择主位置(类似主键),列名

from information_schema.columns  

where table_name='articles1' and COLUMN_NAME like 'word%';# 从 articles1表中取列,仅取出以word开头的

 

第二步就是要实现循环,使用循环来取出所有列名依次对比,这里主要使用到了procedure、concat、preparestatement。prcedure类似其他语言里的函数,concat涌来拼接字符串,而preparestatement用来将字符串按sql程序执行。

 

tip1:procedure有个坑,开始时一直调试不成功,这是因为delimiter的问题,也就是怎么样算是你的语句结束了。默认delimiter是';',但是定义procedure时因为中间不能被中断而又会有多条语句,所以先使用 delimter // 命令改变默认delimiter

create procedure pro1( 

    in article_id integer,   

    in wordmatch varchar(30) 

)  

begin

    set @wordmatch = wordmatch; #声明要匹配的词

    set @col_cnt=(select count(COLUMN_NAME) from information_schema.columns where table_name='articles1' and COLUMN_NAME like 'word%'); #得到列的总素数量

 

    set @article_id=article_id;#声明要匹配的文章id

    set @i=2;//因为的articles1表第一个字段时id,所以从第二个字段开始匹配

    set @cnt=0;//与制定单词相同的列值的个数

    while @i<=@col_cnt+1 do

            #产生需要执行的命令

            set @thiscol=(select COLUMN_NAME from column_name where ORDINAL_POSITION=@i); //从列名表中取出字段

            set @s=concat('set @cnt=@cnt + (select count(', @thiscol, ')', ' from articles1 where id=', @article_id , ' and ', @thiscol, '=\'' , @wordmatch, '\');');

            prepare stmt1 from @s;

            #执行命令

            execute stmt1;                                                                                                                                            

            deallocate prepare stmt1;

        set @i=@i+1;

    end while;

    select @cnt;

   

end//

 

参考资料

[1]http://stackoverflow.com/questions/8809943/how-to-select-from-mysql-where-table-name-is-variable preparestatement用法

[2]http://www.cnblogs.com/wangtao_20/archive/2011/02/21/1959734.html  mysql中变量的使用

[2]http://blog.sina.com.cn/s/blog_71f4cdbe0100yut4.html  procedure的使用

posted on 2015-05-04 21:14  Barthu  阅读(598)  评论(0编辑  收藏  举报