-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScoreComputer.java
More file actions
59 lines (54 loc) · 1.5 KB
/
Copy pathScoreComputer.java
File metadata and controls
59 lines (54 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @author Chuka Okoye
*
*/
public class ScoreComputer
{
String[] searchTerms;
public ScoreComputer(String[] searchTerm)
{
this.searchTerms = searchTerm;
}
/**
* Responsible for computing the score values for each snippet and
* can if specified, tag each snippet with [[HIGHLIGH]] & [[ENDHIGHLIGHT]]
*
* @param value, the Snippet whose score is to be computed
* @param tag, an indicator for tagging. Passing true will enable tagging.
*
* @return The new snippet with a computed score and optionally tagged.
*/
protected Snippet computeScore(Snippet value, boolean tag)
{
int tempScore = 0;
Word aWord = null;
Snippet snippet = value;
for(int i=0; i<snippet.getWordList().size(); i++)
{
aWord = snippet.getWordList().get(i);
tempScore = score(aWord);
if(tempScore != 0)
{
snippet.setScore(snippet.getScore()+tempScore);
if(tag)
snippet.setWord(i, aWord.tagWord());
}
}
return snippet;
}
/**
* Compares each word in a snippet to terms in the search string.
* @param word, the word whose similarity to the search string is to be tested
* @return int, 0 if no match, else 1 if there is a string similar in search string.
*/
private int score(Word word)
{
String[] wordArray = word.getWord().split("\\W"); //Ignore all non-word characters when scoring.
int score = 0;
for(int j=0; j<wordArray.length; j++)
for(int i=0; i<searchTerms.length; i++)
if(wordArray[j].equalsIgnoreCase(searchTerms[i]))
score++;
return score;
}
}