https://github.com/zsxoff/ngrams
Create sequence of N items from a given string
https://github.com/zsxoff/ngrams
Last synced: 3 months ago
JSON representation
Create sequence of N items from a given string
- Host: GitHub
- URL: https://github.com/zsxoff/ngrams
- Owner: zsxoff
- License: unlicense
- Created: 2022-02-12T12:19:36.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-02-12T16:35:11.000Z (over 4 years ago)
- Last Synced: 2025-03-05T22:03:55.023Z (over 1 year ago)
- Language: Go
- Size: 7.81 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ngrams
[](https://pkg.go.dev/github.com/zsxoff/ngrams) [](https://goreportcard.com/report/github.com/zsxoff/ngrams)
Create sequence of N items from a given string.
## Installation
```bash
go get github.com/zsxoff/ngrams@latest
```
## Usage
```go
import (
"fmt"
"github.com/zsxoff/ngrams"
)
func main() {
s := "Hello!"
for i, ngram := range ngrams.Ngrams(s, 2) {
fmt.Println(i, ngram)
}
}
// Output:
// 0 He
// 1 el
// 2 ll
// 3 lo
// 4 o!
```
Also you can use `Unigrams(line string)`, `Bigrams(line string)` and `Trigrams(line string)` like
```go
s := "Hello!"
for i, ngram := range ngrams.Trigrams(s) {
fmt.Println(i, ngram)
}
// Output:
// 0 Hel
// 1 ell
// 2 llo
// 3 lo!
```
## License
[](https://unlicense.org/)
This project is licensed under the terms of the [Unlicense](https://unlicense.org/) (see [LICENSE]() file).