language agnostic - Sorting search results -


i'm implementing phrase , keyword search (most kind of search has name, don't know it). exemplify, search i turtles should match:

i turtles said turtles turtles reptiles called turtles turtles 

in short, string must contain keywords match.

then comes problem of sorting search results.

naively, i'm assuming closest matches beginning of result , original query, better result. how can express code?

my first approach assign score each keyword in each result based on how close keyword expected position, based in original query. in pseudo-code:

score(result,query) {     keywords = query.split(" ");     score = 0     keywords.length() {        score += score(result,query,keywords,i)     }     return score }  score(result,query,keywords,i) {     index = text.indexof(keywords[i])     if (i == 0) return index;      previousindex = text.indexof(keywords[i-1])     indexinsearch = query.indexof(keywords[i])     previousindexinsearch = query.indexof(keywords[i-1])      expectedindex = previousindex + (indexinsearch - previousindexinsearch)      return abs(index - expectedindex) } 

the lower score better result. scores above examples seem decent enough:

i turtles = 0 turtles = 7 said turtles = 8 reptiles called turtles = 38 turtles = 39 

is viable approach sort search results?

leaving kind of semantic analysis aside, else considering improve it?


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -