reval

package module
v0.0.0-...-8198ce7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2025 License: MIT Imports: 2 Imported by: 0

README

reval

PkgGoDev Go Report Card tests

  • Evaluation metrics for ranking and retrieval

Documentation

Index

Examples

Constants

View Source
const (
	AbsTol = 1e-08
	RelTol = 1e-05
)

Variables

This section is empty.

Functions

func BERTScore

func BERTScore(candidates, refs [][]float64) (precision, recall, f1 float64)

BERTScore returns the BERTScore between candidates and refs.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/reval"
)

func main() {
	candidates := [][]float64{
		{0.1, 0.2, 0.3},
		{0.4, 0.5, 0.6},
	}
	refs := [][]float64{
		{0.1, 0.2, 0.3},
		{0.7, 0.8, 0.9},
	}

	precision, recall, f1 := reval.BERTScore(candidates, refs)
	fmt.Printf("%.4f, %.4f, %.4f\n", precision, recall, f1)

}
Output:

0.8600, 0.7700, 0.8125

func DCG

func DCG(relevance []int) float64

func DotProduct

func DotProduct(a, b []float64) float64

DotProduct returns the dot product of two vectors a and b.

func F1

func F1(precision, recall float64) float64

F1 returns the F1 score given precision and recall.

func FBeta

func FBeta(precision, recall, beta float64) float64

FBeta returns the F-beta score given precision and recall.

func IsClose

func IsClose(a, b float64) bool

func IsZero

func IsZero(a float64) bool

func L2Norm

func L2Norm(a []float64) float64

L2Norm returns the L2 norm (Euclidean norm) of vector a.

func LCS

func LCS(a, b []string) int

LCS returns the length of the Longest Common Subsequence between a and b.

func NDCG

func NDCG(predicted []string, relevance map[string]int, k int) float64
Example
package main

import (
	"fmt"

	"github.com/itsubaki/reval"
)

func main() {
	predicted := []string{"A", "B", "C", "D"}
	relevance := map[string]int{
		"A": 3,
		"B": 2,
		"C": 1,
		"D": 0,
		"E": 3,
	}

	s := reval.NDCG(predicted, relevance, 3)
	fmt.Println("NDCG@3:", s)

}
Output:

NDCG@3: 0.7271926019583822

func Normalize

func Normalize(a []float64) []float64

Normalize returns the L2-normalized version of vector a.

func Overlap

func Overlap(a, b []string) int

Overlap returns the count of overlapping tokens between a and b.

func Precision

func Precision(predicted []string, relevance map[string]int, k int) float64
Example
package main

import (
	"fmt"

	"github.com/itsubaki/reval"
)

func main() {
	predicted := []string{"A", "B", "C", "D"}
	relevance := map[string]int{
		"A": 3,
		"B": 2,
		"C": 0,
		"D": 0,
		"E": 3,
	}

	s := reval.Precision(predicted, relevance, 3)
	fmt.Println("Precision@3:", s)

}
Output:

Precision@3: 0.6666666666666666

func ROUGE1

func ROUGE1(candidates, refs []string) (precision, recall, f1 float64)

ROUGE1 returns the ROUGE-1 score (unigram overlap)

Example
package main

import (
	"fmt"

	"github.com/itsubaki/reval"
)

func main() {
	candidates := []string{"the", "cat", "is", "sitting", "on", "the", "mat"}
	refs := []string{"the", "cat", "sat", "on", "the", "mat"}

	precision, recall, f1 := reval.ROUGE1(candidates, refs)
	fmt.Printf("%.4f, %.4f, %.4f\n", precision, recall, f1)

}
Output:

0.7143, 0.8333, 0.7692

func ROUGEL

func ROUGEL(candidates, refs []string) (precision, recall, f1 float64)

ROUGEL returns the ROUGE-L score (Longest Common Subsequence)

Example
package main

import (
	"fmt"

	"github.com/itsubaki/reval"
)

func main() {
	candidates := []string{"the", "cat", "is", "sitting", "on", "the", "mat"}
	refs := []string{"the", "cat", "sat", "on", "the", "mat"}

	precision, recall, f1 := reval.ROUGEL(candidates, refs)
	fmt.Printf("%.4f, %.4f, %.4f\n", precision, recall, f1)

}
Output:

0.7143, 0.8333, 0.7692

func ROUGELsum

func ROUGELsum(candidates, refs [][]string) (precision, recall, f1 float64)

ROUGELsum returns the ROUGE-Lsum score for multiple sentences.

Example
package main

import (
	"fmt"

	"github.com/itsubaki/reval"
)

func main() {
	candidates := [][]string{
		{"the", "cat", "is", "on", "the", "mat"},
		{"it", "is", "cute"},
	}

	refs := [][]string{
		{"the", "dog", "is", "on", "the", "mat"},
		{"the", "animal", "is", "cute"},
		{"the", "pet", "sleeps", "well"},
	}

	precision, recall, f1 := reval.ROUGELsum(candidates, refs)
	fmt.Printf("%.4f, %.4f, %.4f\n", precision, recall, f1)

}
Output:

0.7778, 0.5000, 0.6087

func Recall

func Recall(predicted []string, relevance map[string]int, k int) float64
Example
package main

import (
	"fmt"

	"github.com/itsubaki/reval"
)

func main() {
	predicted := []string{"A", "B", "C", "D"}
	relevance := map[string]int{
		"A": 3,
		"B": 2,
		"C": 1,
		"D": 0,
		"E": 3,
	}

	s := reval.Recall(predicted, relevance, 3)
	fmt.Println("Recall@3:", s)

}
Output:

Recall@3: 0.75

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL