An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# anno [![GoDoc](https://godoc.org/github.com/matryer/anno?status.svg)](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.