Skip to content

Instantly share code, notes, and snippets.

@lmas
Created September 28, 2017 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lmas/e47f5cbb04d3284d3a546b0309908d1d to your computer and use it in GitHub Desktop.
Save lmas/e47f5cbb04d3284d3a546b0309908d1d to your computer and use it in GitHub Desktop.
Summarizes text
import (
"strings"
"github.com/JesusIslam/tldr"
)
func Summarize(text string) (string, float64, error) {
sum := tldr.New()
tmp, err := sum.Summarize(text, 6)
if err != nil {
return "", 0, err
}
var out string
for _, l := range strings.Split(tmp, "\n") {
l = strings.TrimSpace(l)
if l == "" {
// Ignore empty lines
continue
}
c := strings.Count(l, " ")
if c < 2 {
// Ignore lines with too few words (or in this case,
// spaces between words)
continue
}
out += l + "\n\n"
}
tLen, oLen := float64(len(text)), float64(len(out))
diff := oLen / tLen
return strings.TrimSpace(out) + "\n", diff, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment