PageRenderTime 10ms CodeModel.GetById 0ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/branch-3x/src/xml/DefaultSimilarity.js

http://luke.googlecode.com/
JavaScript | 61 lines | 32 code | 11 blank | 18 comment | 1 complexity | cd15c80920cb6ff9aad62a08a4ea4ca0 MD5 | raw file
 1// This is an implementation of DefaultSimilarity
 2// in JavaScript.
 3//
 4// NOTE: Since JavaScript is a weakly-typed language, some
 5// overloaded methods have been renamed to avoid ambiguity.
 6// You need to keep these changed names as they are, because
 7// the plugin depends on them. Other than that you are free
 8// to change anything else.
 9
10//--- ABSTRACT METHODS ---
11// You HAVE TO implement these
12
13function coord(overlap, maxOverlap) {
14  return overlap / (1.0 * maxOverlap);
15}
16
17function idf(docFreq, numDocs) {
18  return (Math.log(numDocs/(docFreq+1)) + 1.0);
19}
20
21function lengthNorm(fieldName, numTerms) {
22  return (1.0 / Math.sqrt(numTerms));
23}
24
25function queryNorm(sumOfSquaredWeights) {
26  return (1.0 / Math.sqrt(sumOfSquaredWeights));
27}
28
29function sloppyFreq(distance) {
30  return 1.0 / (distance + 1);
31}
32
33function tf(freq) {
34  return Math.sqrt(freq);
35}
36
37//--- PUBLIC METHODS ---
38// You may choose to override these. If they are not overridden, the
39// plugin will use DefaultSimilarity implementation, which is equivalent
40// to the code reproduced below.
41
42// RENAMED: float idf(Collection terms, Searcher searcher)
43function idf_cs(terms, searcher) {
44  var idf = 0.0;
45  var i = terms.iterator();
46  while (i.hasNext()) {
47    // NOTE: we use a renamed method, due to ambiguity in overloading
48    idf += idf_ts(i.next(), searcher);
49  }
50  return idf;
51}
52
53// RENAMED: float idf(Term term, Searcher searcher)
54function idf_ts(term, searcher) {
55  return idf(searcher.docFreq(term), searcher.maxDoc());
56}
57
58// RENAMED: float tf(int freq)
59function tf_i(freq) {
60  return tf(freq);
61}