commit fe0a21acec1c73fae6dd75703e5d6b2876900721 Author: Matt Baer Date: Fri Feb 26 13:52:03 2016 -0500 Write code ,.(:.---). diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f1365a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.swp +cmd/cmd diff --git a/README.md b/README.md new file mode 100644 index 0000000..b95357d --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +punctuation +=========== + +Words and symbols in, punctuation out. Inspired by an [article on punctuation in novels](https://medium.com/@neuroecology/punctuation-in-novels-8f316d542ec4). diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..354959a --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + "os" + + "github.com/thebaer/punctuation" +) + +func main() { + o := punctuation.Extract(os.Stdin) + fmt.Println(o) +} diff --git a/punctuation.go b/punctuation.go new file mode 100644 index 0000000..275329d --- /dev/null +++ b/punctuation.go @@ -0,0 +1,25 @@ +// punctuation takes text and gives back only punctuation. +package punctuation + +import ( + "bufio" + "os" + "strings" +) + +const validPunc = ";:'\",!?.-()" + +func Extract(f *os.File) string { + var out string + in := bufio.NewScanner(f) + for in.Scan() { + out += strings.Map(func(r rune) rune { + if strings.IndexRune(validPunc, r) >= 0 { + return r + } + return -1 + }, in.Text()) + } + + return out +}