2012-12-28 19 views
2

我使用下面的代碼進行基本的,區分大小寫的特定於術語的搜索。它會現在工作,但我想要的東西,(按重要性排序):我想用JavaScript進行高級搜索。怎麼樣?

1:忽略大小寫(即「嗨」和「嗨」都是相同的。toLowerCase是不是一個選項,是不一樣的東西)

2:如果搜索查詢是'搜索條件'並且搜索到的字符串是'搜索條件',將會產生一個命中例子。

3:即使在找到命中以獲得更多命中之後,也搜索整個字符串。

目的是搜索帶有特定id一個術語的<p>標籤。如果有,則顯示它。最終,我將在一個循環中使用它,該循環將搜索許多<p>標籤並顯示帶有點擊的標籤,並將其隱藏。

CODE:

<!DOCTYPE html> 
<html> 
    <body> 
     <p id="demo">Click the button to locate where in the string a specifed value occurs.</p> 
     <p id="demo1" style="display:none;">Hello world, welcome to the universe.</p> 
     <button onclick="myFunction()">Try it</button> 

     <script> 
      function myFunction() { 
       var x = document.getElementById("demo1") 
       var str = x.innerHTML.toString(); 
       var n = str.indexOf("welcome"); 
       if (n != -1) { 
        x.style.display = 'inline'; 
       } else { 
        x.innerHTML = 'Negative'; 
        x.style.display = 'inline'; 
       } 
      } 
     </script> 

    </body> 
</html> 
+1

這不是JavaScript的任務。要做你想做的事,你需要使用自然語言處理。我將開始標記輸入字符串並刪除後綴。從那裏,你可以嘗試搜索你的數據庫。 – Blender

+0

我知道一些java。一個小程序能夠處理這個問題嗎? – fredsbend

+0

這種東西通常不是客戶端完成的。 Java有許多很好的自然語言處理庫,你可以使用它們,但它們的確有一個陡峭的學習曲線。 「 – Blender

回答

2

這與一些調整,應該滿足您的要求,我相信。 雖然=),但在後端執行此操作可能會更好。

function tokenize(input) { 
    return input.toLowerCase().replace(/[^a-z0-9_\s]/g, '').split(/\s+/g) 
} 

哪些這樣做是爲了您的搜索字詞:通過您的標記化輸入字符串從here

+0

不僅我是否也需要在光盤上使用此服務器端。我會仔細看看這個。謝謝 – fredsbend

0

以正則表達式引擎看看。學習需要一些時間,但一旦你知道它,你可能會在這裏實現你的目標。

這裏是一個:link

希望這有助於

+0

到目前爲止,我可以看到如何得到不區分大小寫。我一定會使用它。看看我可以如何解決原始帖子中的第2項。不太明白如何解決原始帖子中的第3項。 – fredsbend

+0

總之 - 你可以使用組和全球搜索(標籤/克)。對於更多的答案,你可以閱讀:http://stackoverflow.com/questions/520611/how-can-i-match-multiple-occurrences-with-a-regex-in-javascript-similar-to-phps –

2

我開始借

// returns the indices of the found searchStr within str, case sensitive if needed 
function getIndicesOf(searchStr, str, caseSensitive) { 
    var startIndex = 0, searchStrLen = searchStr.length; 
    var index, indices = []; 
    if (!caseSensitive) { 
     str = str.toLowerCase(); 
     searchStr = searchStr.toLowerCase(); 
    } 
    while ((index = str.indexOf(searchStr, startIndex)) > -1) { 
     indices.push(index); 
     startIndex = index + searchStrLen; 
    } 
    return indices; 
} 

// this splits the search string in an array of search strings 
var myStringArray = mySearchString.split("\\s+"); 
var result = true; 
// loop over all the split search strings, and search each seperately 
for (var i = 0; i < myStringArray.length; i++) { 
    var indices = getIndicesOf(myStringArray[i], "I learned to play the Ukulele in Lebanon.", false); 
    if(indices && indices.length>0){ 
     // do something with the indices of the found string 
    } else { 
     result = false; 
    } 
} 
// result will be false here if one of the search terms was not found. 

> tokenize("I'm your search string.") 
["im", "your", "search", "string"] 

接下來,剝離後綴(我甚至不會去嘗試處理這種情況下不起作用的情況,這就是N LP是):

function remove_suffix(token) { 
    return token.replace(/(ing|s)$/, ''); 
} 

它會做到這一點每個令牌:

> remove_suffix('searching') 
"search" 
> remove_suffix('terms') 
"term" 

所以對於每個查詢字符串,可以構建關鍵字列表:

function get_keywords(query) { 
    var tokens = tokenize(query); 
    var keywords = tokens.map(remove_suffix); 
    keywords.sort(); 

    return keywords; 
} 

而且它會將您的查詢轉換爲關鍵字:

> get_keywords('searching terms') 
["search", "term"] 
> get_keywords('term search') 
["search", "term"] 

現在,您只需檢查查詢字符串的關鍵字是否包含在搜索字符串的關鍵字中。

這是一個非常簡單的例子,不會處理大量的角落案例,但至少您可以看到如何使用關鍵字進行搜索。

+0

只是舉一個例子:'remove_suffix('string')' - >'「str」'。仍然是+1,因爲對其餘的來說這是一個很好的答案。 – Cerbrus

+0

@Cerbrus:和''跑' - >'奔跑'。例外列表只是繼續。 – Blender