简体   繁体   English

如何进行类似stackoverflow的相似标题搜索

[英]How to make something like stackoverflow's similar title search

* UPDATE: *I've already answered my question. * 更新: *我已经回答了我的问题。 But you can still give me advise and i'll take your answer as selected 但是您仍然可以给我建议,我将按您的选择回答

NOTE: If you don't need to know what I want to do with the codes, just skip the first several paragraphs and directly see the codes and tell me why they doesn't work without error. 注意:如果您不需要知道我要如何使用代码,只需跳过前几段,直接查看代码,然后告诉我为什么它们没有错误就无法工作。

I want to make something like stackoverflow's similar title search when you enter your title in the ask page. 当您在询问页面中输入标题时,我想进行类似stackoverflow的相似标题搜索的操作。

I need to split words to make regex and then search in the database. 我需要将单词拆分成正则表达式,然后在数据库中搜索。 Since my application is in Chinese(no spaces between each words) and I think splitting chinese into meaningful phrases using PHP is too hard. 由于我的应用程序是中文(每个单词之间没有空格),因此我认为使用PHP将中文分成有意义的短语太难了。 I have an idea splitting it in the client side using javascript according to chinese IME's characteristic that, for example, if you want to type the word "你好中国" in chinese, people usually type "nihao[space]zhongguo" in IME(note where the space bar is), since '你好'(nihao - hello) is a phrase and '中国'(zhongguo - china) is another. 我有一个想法,是根据中文IME的特性在客户端使用javascript进行拆分,例如,如果您想用中文输入单词“你好中国”,人们通常会在IME中输入“ nihao [space] zhongguo”(请注意空格在哪里),因为“你好”(nihao-hello)是一个短语,而“中国”(zhongguo-china)是另一个短语。 So when people press space bar i record the word he entered before the space and start a timer of 2 seconds , if he or she enters another words clear the timer and continue to record if he or she doesn't, send each words recorded to the server. 因此,当人们按下空格键时,我会记录他在空格之前输入的单词并启动2秒钟的计时器,如果他或她输入了另一个单词,请清除计时器并继续记录,如果他或她没有输入,请将记录的每个单词发送到服务器。

Qustion is, is this a good idea? 问题是,这是个好主意吗? Are there any other convenient way to do this? 还有其他方便的方法吗? And why these lines i wrote to test won't work without error. 以及为什么我写来测试的这些行将不会出现错误。

script: 脚本:

$(function(){
        var i=0;
        $('#t').keyup(function(e){
            if(e.keyCode==32)
            {
                eval("a"+i+"=$(this).val()");
                i++;
                var timer=setTimeout("for(b=0;b<i;b++){alert(eval('a'+b));}",1000);
                if($("#t").keydown())
                {
                    clearTimeout(timer);
                }
            }
        })
    })

html: 的HTML:

<input id="t"/> 

I think i know why it won't work now: 我想我知道为什么现在不起作用:

First, i should be a global variable; 首先, i应该是一个全局变量;

Second, the keydown() function should be outside of the keyup() . 其次, keydown()函数应该在keyup() modified js: 修改后的js:

$(function(){
    i=0;
    timer=null;
    $('#t').keyup(function(e){
        if(e.keyCode==32)
        {
            eval("a"+i+"=$(this).val()");
            i++;
            timer=setTimeout("for(b=0;b<i;b++){alert(eval('a'+b));}",1000)
        }
    })
    if($("#t").keydown())
    {
        clearTimeout(timer);
    }
})

If you have other suggestions on my idea or codes you can still answer this question or i'll select this one after some time. 如果您对我的想法或代码有其他建议,您仍然可以回答此问题,或者我将在一段时间后选择此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM