Created
December 29, 2016 07:54
-
-
Save cipepser/cbc1ee78191bf3f77d7a49df56d45a19 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"strings" | |
) | |
func makeCharngram(s []rune, n int) [][]rune { | |
t := make([][]rune, len(s) - n + 1) | |
for i := 0; i < len(s) -n + 1; i++ { | |
t[i] = s[i:i+n] | |
} | |
return t | |
} | |
func makeWordngram(s [][]rune, n int) [][][]rune { | |
t := make([][][]rune, len(s) - n + 1) | |
for i := 0; i < len(s) -n + 1; i++ { | |
t[i] = s[i:i+n] | |
} | |
return t | |
} | |
func main() { | |
s := "I am an NLPer" | |
fmt.Println("------文字bi-gram------") | |
t := makeCharngram([]rune(s), 2) | |
for _, v := range t { | |
fmt.Println(string(v)) | |
} | |
fmt.Println("------単語bi-gram------") | |
words := strings.Split(s, " ") | |
r_words := make([][]rune, len(words)) | |
for i, v := range words { | |
r_words[i] = []rune(v) | |
} | |
wbg := makeWordngram(r_words, 2) | |
for _, w := range wbg { | |
for _, x := range w { | |
fmt.Printf(string(x) + " ") | |
} | |
fmt.Println("") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment