summaryrefslogtreecommitdiff
path: root/vendor/github.com/DavidBelicza/TextRank/v2/rank/ranking.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/DavidBelicza/TextRank/v2/rank/ranking.go')
-rw-r--r--vendor/github.com/DavidBelicza/TextRank/v2/rank/ranking.go66
1 files changed, 66 insertions, 0 deletions
diff --git a/vendor/github.com/DavidBelicza/TextRank/v2/rank/ranking.go b/vendor/github.com/DavidBelicza/TextRank/v2/rank/ranking.go
new file mode 100644
index 0000000..5fd2dfa
--- /dev/null
+++ b/vendor/github.com/DavidBelicza/TextRank/v2/rank/ranking.go
@@ -0,0 +1,66 @@
+package rank
+
+// Calculate function ranking words by the given algorithm implementation.
+func Calculate(ranks *Rank, algorithm Algorithm) {
+ updateRanks(ranks, algorithm)
+}
+
+func updateRanks(ranks *Rank, algorithm Algorithm) {
+ for _, word := range ranks.Words {
+ weight := algorithm.WeightingHits(word.ID, ranks)
+ word.Weight = weight
+
+ if ranks.Max < word.Weight {
+ ranks.Max = word.Weight
+ }
+
+ if ranks.Min > word.Weight || ranks.Min == 0 {
+ ranks.Min = word.Weight
+ }
+ }
+
+ for _, word := range ranks.Words {
+ word.Weight = normalize(word.Weight, ranks.Min, ranks.Max)
+ }
+
+ for x, xMap := range ranks.Relation.Node {
+ for y := range xMap {
+ sentenceIDs := ranks.Relation.Node[x][y].SentenceIDs
+ weight := algorithm.WeightingRelation(x, y, ranks)
+
+ ranks.Relation.Node[x][y] = Score{
+ ranks.Relation.Node[x][y].Qty,
+ weight,
+ sentenceIDs,
+ }
+
+ if ranks.Relation.Max < weight {
+ ranks.Relation.Max = weight
+ }
+
+ if ranks.Relation.Min > weight || ranks.Relation.Min == 0 {
+ ranks.Relation.Min = weight
+ }
+ }
+ }
+
+ for x, xMap := range ranks.Relation.Node {
+ for y := range xMap {
+ weight := normalize(
+ ranks.Relation.Node[x][y].Weight,
+ ranks.Relation.Min,
+ ranks.Relation.Max,
+ )
+
+ ranks.Relation.Node[x][y] = Score{
+ ranks.Relation.Node[x][y].Qty,
+ weight,
+ ranks.Relation.Node[x][y].SentenceIDs,
+ }
+ }
+ }
+}
+
+func normalize(weight float32, min float32, max float32) float32 {
+ return (weight - min) / (max - min)
+}