tldr

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2025 License: MIT Imports: 7 Imported by: 2

README

tldr

When you are too lazy to read the entire text

Coverage Status GoDoc Go Report Card FOSSA Status

What?

tldr is a golang package to summarize a text automatically using lexrank algorithm.

How?

There are two main steps in lexrank, weighing, and ranking. tldr have two weighing and two ranking algorithm included, they are Jaccard coeficient and Hamming distance, then PageRank and centrality, respectively. The default settings use Hamming distance and pagerank.

Is This Fast?
$ go test -bench . -benchmem -benchtime 5s -cpu 4
Running Suite: Tldr Suite
=========================
Random Seed: 1759506557
Will run 8 of 8 specs

••••••••
Ran 8 of 8 Specs in 0.012 seconds
SUCCESS! -- 8 Passed | 0 Failed | 0 Pending | 0 Skipped
goos: linux
goarch: amd64
pkg: github.com/didasy/tldr
cpu: AMD Ryzen 5 5600G with Radeon Graphics
BenchmarkSummarizeCentralityHamming-4               5877            896338 ns/op          177320 B/op       1898 allocs/op
BenchmarkSummarizeCentralityJaccard-4               6562            885374 ns/op          177221 B/op       1898 allocs/op
BenchmarkSummarizePagerankHamming-4                 5832            962000 ns/op          200830 B/op       2086 allocs/op
BenchmarkSummarizePagerankJaccard-4                 5949            962579 ns/op          200865 B/op       2086 allocs/op
PASS
ok      github.com/didasy/tldr  22.840s

So, not bad huh?

Installation

go get github.com/didasy/tldr

Example
package main

import (
	"fmt"
	"io/ioutil"
	"github.com/didasy/tldr"
)

func main() {
	intoSentences := 3
	textB, _ := ioutil.ReadFile("./sample.txt")
	text := string(textB)
	bag := tldr.New()
	result, _ := bag.Summarize(text, intoSentences)
	fmt.Println(result)
}
Testing

To test, just run go test, but you need to have gomega and ginkgo installed.

Dependencies?

tldr depends on pagerank package, and you can install it with go get github.com/alixaxel/pagerank.

License?

Check the LICENSE file. tldr: MIT.

Have fun!

Documentation

Overview

Dependencies:

go get github.com/alixaxel/pagerank

WARNING: This package is not thread safe, so you cannot use *Bag from many goroutines.

Index

Constants

View Source
const (
	VERSION                              = "0.6.0"
	DEFAULT_ALGORITHM                    = "pagerank"
	DEFAULT_WEIGHING                     = "hamming"
	DEFAULT_DAMPING                      = 0.85
	DEFAULT_TOLERANCE                    = 0.0001
	DEFAULT_THRESHOLD                    = 0.001
	DEFAULT_MAX_CHARACTERS               = 0
	DEFAULT_SENTENCES_DISTANCE_THRESHOLD = 0.95
)

The default values of each settings

Variables

This section is empty.

Functions

func Intersection

func Intersection(src, dst []int) []int

func ReverseEdge added in v0.4.1

func ReverseEdge(num []*Edge)

func ReverseRank added in v0.4.1

func ReverseRank(num []*Rank)

func SanitizeWord

func SanitizeWord(word string) string

func SymmetricDifference

func SymmetricDifference(src, dst []int) []int

func TokenizeSentences added in v0.4.1

func TokenizeSentences(text string) []string

func UniqSentences

func UniqSentences(sentences [][]string, sentenceDistanceThreshold float64)

Types

type Bag

type Bag struct {
	BagOfWordsPerSentence [][]string
	OriginalSentences     []string
	Dict                  map[string]int
	Nodes                 []*Node
	Edges                 []*Edge
	Ranks                 []int

	MaxCharacters              int
	Algorithm                  string // "centrality" or "pagerank" or "custom"
	Weighing                   string // "hamming" or "jaccard" or "custom"
	Damping                    float64
	Tolerance                  float64
	Threshold                  float64
	SentencesDistanceThreshold float64
	// contains filtered or unexported fields
}

func New

func New() *Bag

New creates a new summarizer

func (*Bag) Set added in v0.4.1

func (bag *Bag) Set(m int, d, t, th, sth float64, alg, w string)

Set max characters, damping, tolerance, threshold, sentences distance threshold, algorithm, and weighing

func (*Bag) SetCustomAlgorithm added in v0.4.4

func (bag *Bag) SetCustomAlgorithm(f func(e []*Edge) []int)

func (*Bag) SetCustomWeighing added in v0.4.4

func (bag *Bag) SetCustomWeighing(f func(src, dst []int) float64)

func (*Bag) SetDictionary added in v0.4.4

func (bag *Bag) SetDictionary(dict map[string]int)

Useful if you already have your own dictionary (example: from your database) Dictionary is a map[string]int where the key is the word and int is the position in vector, starting from 1

func (*Bag) SetWordTokenizer added in v0.5.0

func (bag *Bag) SetWordTokenizer(f func(string) []string)

func (*Bag) String added in v0.7.0

func (b *Bag) String() string

func (*Bag) Summarize

func (bag *Bag) Summarize(text string, num int) ([]string, error)

Summarize the text to num sentences

type ByScore added in v0.4.4

type ByScore []*Rank

func (ByScore) Len added in v0.4.4

func (b ByScore) Len() int

func (ByScore) Less added in v0.4.4

func (b ByScore) Less(i, j int) bool

func (ByScore) Swap added in v0.4.4

func (b ByScore) Swap(i, j int)

type ByWeight added in v0.4.4

type ByWeight []*Edge

func (ByWeight) Len added in v0.4.4

func (b ByWeight) Len() int

func (ByWeight) Less added in v0.4.4

func (b ByWeight) Less(i, j int) bool

func (ByWeight) Swap added in v0.4.4

func (b ByWeight) Swap(i, j int)

type Edge

type Edge struct {
	// contains filtered or unexported fields
}

type Node

type Node struct {
	// contains filtered or unexported fields
}

type Rank

type Rank struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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