Improve longest word (punctuation)

This commit is contained in:
Meutel 2017-07-07 16:55:55 +02:00
parent 59e20d36c1
commit 0433c6344c

View File

@ -7,6 +7,8 @@ import "os"
import "bufio" import "bufio"
import "strings" import "strings"
var PUNCTUATION = []string{".", ",", "\"", "—", "-", "/"}
func longest(r io.Reader) string { func longest(r io.Reader) string {
lw := "" lw := ""
s := bufio.NewScanner(r) s := bufio.NewScanner(r)
@ -14,14 +16,12 @@ func longest(r io.Reader) string {
for s.Scan() { for s.Scan() {
word := s.Text() word := s.Text()
word = strings.ToLower(word) word = strings.ToLower(word)
word = strings.Replace(word, ".", "", -1) for _, c := range PUNCTUATION {
word = strings.Replace(word, ",", "", -1) word = strings.Replace(word, c, " ", -1)
word = strings.Replace(word, "\"", "", -1) }
for _, word := range strings.Split(word, "—") { for _, word := range strings.Fields(word) {
if len(word) > len(lw) { if len(word) > len(lw) {
if !strings.HasPrefix(word, "http://") { lw = word
lw = word
}
} }
} }
} }