I have a basic, case sensitive, term specific search with the code below. It will work for now but I would like something that (In order of importance):
1: ignores case (ie "hi" and "Hi" are both the same. toLowerCase
is not an option and is not the same thing)
2: Will yield a hit if the search query is 'Search Term' and the searched string is 'searching terms', as an example.
3: Searches the entire string even after finding a hit for more hits.
The purpose is to search a <p>
tag with a specific id
for a term. If it has it then display it. Ultimately, I will use this in a loop that will search many <p>
tags and display the ones with hits and leave hidden the ones without.
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, wele 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("wele");
if (n != -1) {
x.style.display = 'inline';
} else {
x.innerHTML = 'Negative';
x.style.display = 'inline';
}
}
</script>
</body>
</html>
I have a basic, case sensitive, term specific search with the code below. It will work for now but I would like something that (In order of importance):
1: ignores case (ie "hi" and "Hi" are both the same. toLowerCase
is not an option and is not the same thing)
2: Will yield a hit if the search query is 'Search Term' and the searched string is 'searching terms', as an example.
3: Searches the entire string even after finding a hit for more hits.
The purpose is to search a <p>
tag with a specific id
for a term. If it has it then display it. Ultimately, I will use this in a loop that will search many <p>
tags and display the ones with hits and leave hidden the ones without.
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, wele 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("wele");
if (n != -1) {
x.style.display = 'inline';
} else {
x.innerHTML = 'Negative';
x.style.display = 'inline';
}
}
</script>
</body>
</html>
Share
Improve this question
edited Dec 28, 2012 at 11:45
Cerbrus
73k19 gold badges136 silver badges150 bronze badges
asked Dec 28, 2012 at 10:57
user1934286user1934286
1,7625 gold badges23 silver badges43 bronze badges
9
- 1 This isn't a task for JavaScript. To do what you'd like, you will need to use natural language processing. I'd start with tokenizing your input string and removing the suffixes. From there, you can try to search your database. – Blender Commented Dec 28, 2012 at 11:00
- I know some java. Would an applet be able to handle this? – user1934286 Commented Dec 28, 2012 at 11:02
- This sort of stuff usually isn't done clientside. Java has a bunch of good natural language processing libraries that you could use, but they do have a steep learning curve. – Blender Commented Dec 28, 2012 at 11:03
- "toLowerCase is not an option and is not the same thing" why not? – PeeHaa Commented Dec 28, 2012 at 11:05
- toLowerCase changes the searh term which is unhelpful since the case of the searched string is unknown. toLowerCase is to normalize text. – user1934286 Commented Dec 28, 2012 at 11:13
3 Answers
Reset to default 6I'd start by tokenizing your input string:
function tokenize(input) {
return input.toLowerCase().replace(/[^a-z0-9_\s]/g, '').split(/\s+/g)
}
Which does this to your search terms:
> tokenize("I'm your search string.")
["im", "your", "search", "string"]
Next, strip off the suffixes (I'm not even going to try to handle the cases where this won't work. This is what NLP is for):
function remove_suffix(token) {
return token.replace(/(ing|s)$/, '');
}
It'll do this to each token:
> remove_suffix('searching')
"search"
> remove_suffix('terms')
"term"
So for each query string, you can construct a list of keywords:
function get_keywords(query) {
var tokens = tokenize(query);
var keywords = tokens.map(remove_suffix);
keywords.sort();
return keywords;
}
And it will convert your query into keywords:
> get_keywords('searching terms')
["search", "term"]
> get_keywords('term search')
["search", "term"]
Now, you just check to see if your query string's keywords are contained within the keywords of your search string.
This is a really simple example and won't handle the multitude of corner cases, but at least you see somewhat how you can use keywords for searching.
This, with some tweaking, should fulfill your requirements I believe. It might be better to do this in the backend though =).
// 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.
borrowed from here
Take a look on Regular expressions engine. It take some time to learn but once you know it you'll probably achieve your goal here.
Here is a: link
Hope this helps