summaryrefslogtreecommitdiff
path: root/vendor/github.com/DavidBelicza/TextRank/v2/rank/algorithm.go
blob: 8f9345ffa888ab621fe7466bc8a20b15879eafb0 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package rank

import (
	"math"
)

// Algorithm interface and its methods make possible the polimorf usage of
// weighting process.
type Algorithm interface {
	WeightingRelation(
		word1ID int,
		word2ID int,
		rank *Rank,
	) float32

	WeightingHits(
		wordID int,
		rank *Rank,
	) float32
}

// AlgorithmDefault struct is the basic implementation of Algorithm. It can
// weight a word or phrase by comparing them.
type AlgorithmDefault struct{}

// NewAlgorithmDefault constructor retrieves an AlgorithmDefault pointer.
func NewAlgorithmDefault() *AlgorithmDefault {
	return &AlgorithmDefault{}
}

// WeightingRelation method is the traditional algorithm of text rank to
// weighting a phrase.
func (a *AlgorithmDefault) WeightingRelation(
	word1ID int,
	word2ID int,
	rank *Rank,
) float32 {
	relationQty := rank.Relation.Node[word1ID][word2ID].Qty

	return float32(relationQty)
}

// WeightingHits method ranks the words by their occurrence.
func (a *AlgorithmDefault) WeightingHits(
	wordID int,
	rank *Rank,
) float32 {
	weight := rank.Words[wordID].Qty

	return float32(weight)
}

// AlgorithmChain struct is the combined implementation of Algorithm. It is a
// good example how weighting can be changed by a different implementations. It
// can weight a word or phrase by comparing them.
type AlgorithmChain struct{}

// NewAlgorithmChain constructor retrieves an AlgorithmChain pointer.
func NewAlgorithmChain() *AlgorithmChain {
	return &AlgorithmChain{}
}

// WeightingRelation method is a combined algorithm of text rank and word
// occurrence, it weights a phrase.
func (a *AlgorithmChain) WeightingRelation(
	word1ID int,
	word2ID int,
	rank *Rank,
) float32 {
	relationQty := rank.Relation.Node[word1ID][word2ID].Qty
	word1Qty := rank.Words[word1ID].Qty
	word2Qty := rank.Words[word2ID].Qty

	qDiff := float32(math.Abs(float64(word1Qty)-float64(word2Qty))) / 100
	weight := float32(relationQty) + qDiff

	return weight
}

// WeightingHits method ranks the words by their occurrence.
func (a *AlgorithmChain) WeightingHits(
	wordID int,
	rank *Rank,
) float32 {
	word := rank.Words[wordID]
	qty := 0

	for leftWordID, leftWordQty := range word.ConnectionLeft {
		qty += rank.Words[leftWordID].Qty * leftWordQty
	}

	for rightWordID, rightWordQty := range word.ConnectionRight {
		qty += rank.Words[rightWordID].Qty * rightWordQty
	}

	weight := float32(word.Qty) + (float32(qty))

	return float32(weight)
}