Skip to content

Instantly share code, notes, and snippets.

@djhworld
Created April 11, 2014 21:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djhworld/10503051 to your computer and use it in GitHub Desktop.
Save djhworld/10503051 to your computer and use it in GitHub Desktop.
Ngrams algorithm
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
func main() {
if len(os.Args) < 3 {
fmt.Println("Please provide a sentence and an N-Gram ordering .e.g.")
fmt.Println(" \"I have a sentence\" 3")
return
}
var sentence string = os.Args[1]
ngramOrdering, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Println("Please provide a valid ngram ordering")
return
}
result := getNgrams(sentence, ngramOrdering)
for _, r := range result {
fmt.Println(r)
}
}
func getNgrams(input string, ngrams int) []string {
tokens := strings.Fields(input)
var result []string = make([]string, 0)
for i := 0; i < len(tokens); i++ {
if i+ngrams <= len(tokens) {
result = append(result, selectNgramWindow(i, ngrams, tokens))
}
}
return result
}
func selectNgramWindow(index, ngram int, tokens []string) string {
var result string = ""
for i := 0; i < ngram; i++ {
if index+i < len(tokens) {
result += tokens[index+i] + " "
}
}
return strings.TrimSpace(result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment