1package rank
2
3// Calculate function ranking words by the given algorithm implementation.
4func Calculate(ranks *Rank, algorithm Algorithm) {
5 updateRanks(ranks, algorithm)
6}
7
8func updateRanks(ranks *Rank, algorithm Algorithm) {
9 for _, word := range ranks.Words {
10 weight := algorithm.WeightingHits(word.ID, ranks)
11 word.Weight = weight
12
13 if ranks.Max < word.Weight {
14 ranks.Max = word.Weight
15 }
16
17 if ranks.Min > word.Weight || ranks.Min == 0 {
18 ranks.Min = word.Weight
19 }
20 }
21
22 for _, word := range ranks.Words {
23 word.Weight = normalize(word.Weight, ranks.Min, ranks.Max)
24 }
25
26 for x, xMap := range ranks.Relation.Node {
27 for y := range xMap {
28 sentenceIDs := ranks.Relation.Node[x][y].SentenceIDs
29 weight := algorithm.WeightingRelation(x, y, ranks)
30
31 ranks.Relation.Node[x][y] = Score{
32 ranks.Relation.Node[x][y].Qty,
33 weight,
34 sentenceIDs,
35 }
36
37 if ranks.Relation.Max < weight {
38 ranks.Relation.Max = weight
39 }
40
41 if ranks.Relation.Min > weight || ranks.Relation.Min == 0 {
42 ranks.Relation.Min = weight
43 }
44 }
45 }
46
47 for x, xMap := range ranks.Relation.Node {
48 for y := range xMap {
49 weight := normalize(
50 ranks.Relation.Node[x][y].Weight,
51 ranks.Relation.Min,
52 ranks.Relation.Max,
53 )
54
55 ranks.Relation.Node[x][y] = Score{
56 ranks.Relation.Node[x][y].Qty,
57 weight,
58 ranks.Relation.Node[x][y].SentenceIDs,
59 }
60 }
61 }
62}
63
64func normalize(weight float32, min float32, max float32) float32 {
65 return (weight - min) / (max - min)
66}