Created
April 11, 2014 21:28
-
-
Save djhworld/10503051 to your computer and use it in GitHub Desktop.
Ngrams algorithm
This file contains hidden or 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
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