Created
September 28, 2017 18:23
-
-
Save lmas/e47f5cbb04d3284d3a546b0309908d1d to your computer and use it in GitHub Desktop.
Summarizes text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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