https://github.com/matryer/anno
Go package for text annotation.
https://github.com/matryer/anno
golang hashtags mentions text-annotation
Last synced: 6 months ago
JSON representation
Go package for text annotation.
- Host: GitHub
- URL: https://github.com/matryer/anno
- Owner: matryer
- License: mit
- Created: 2015-05-31T12:02:38.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-02-04T16:39:13.000Z (over 5 years ago)
- Last Synced: 2025-03-24T15:11:15.568Z (7 months ago)
- Topics: golang, hashtags, mentions, text-annotation
- Language: Go
- Size: 27.3 KB
- Stars: 71
- Watchers: 2
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# anno [](https://godoc.org/github.com/matryer/anno)
Go package for text annotation. [Read the article covering anno](https://medium.com/@matryer/annotating-text-in-go-and-package-design-ae43f9dccc6d)
* Simple interface
* Very extensible
* Looks nice in JSON (good for APIs)## Usage
```go
s := "Find http://www.websites.com/ and #hashtags and @mentions easily"
notes, err := anno.FindManyString(s, anno.Emails, anno.URLs, anno.Mentions, anno.Hashtags)
if err != nil {
log.Fatalln(err)
}
for _, note := range notes {
log.Printf("Found a %s at [%d:%d]: \"%s\"", note.Kind, note.Start, note.End, note.Val)
}
```Will output:
```
Found a url at [5:29]: "http://www.websites.com/"
Found a mention at [48:57]: "@mentions"
Found a hashtag at [34:43]: "#hashtags"
```You can expand (replace) occurances of the notes using `anno.Expander`:
```go
expander := anno.Expander{
"url": func(b string) string {
return fmt.Sprintf(`%[1]s`, b)
},
"mention": func(b string) string {
return fmt.Sprintf(`%[1]s`, b)
},
}
src := "This is a #long string written by @mat containing links to https://downlist.io/."
notes, err := anno.FindManyString(src, anno.URLs, anno.Mentions, anno.Hashtags)
if err != nil {
log.Fatalln(err)
}log.Println(expander.Expand(src, notes))
```Will output:
```
This is a #long string written by @mat containing links to https://downlist.io/.
```## Emoji
Emoji support lives in its own package due to the external dependency.
```go
src := "You make me want to :smile: you know!"
notes, err := anno.FindManyString(src, emoji.Find)
expander := anno.Expander{
"emoji": emoji.Expand,
}log.Println(expander.Expand(src, notes))
```Will replace `:smile:` with the appropriate emoji character.