anno

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2019 License: MIT Imports: 2 Imported by: 4

README

anno GoDoc

Go package for text annotation. Read the article covering anno

  • Simple interface
  • Very extensible
  • Looks nice in JSON (good for APIs)

Usage

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:

expander := anno.Expander{
	"url": func(b string) string {
		return fmt.Sprintf(`<a href="%[1]s">%[1]s</a>`, b)
	},
	"mention": func(b string) string {
		return fmt.Sprintf(`<a href="https://downlist.io/%[1]s">%[1]s</a>`, 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 <a href="https://downlist.io/@mat">@mat</a> containing links to <a href="https://downlist.io/">https://downlist.io/</a>.

Emoji

Emoji support lives in its own package due to the external dependency.

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.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Emails = FieldFunc("email", func(s []byte) (bool, []byte) {
	trimmedS := TrimPunctuation(s)
	if !bytes.Contains(s, []byte("@")) {
		return false, s
	}
	for _, tld := range tlds {
		if bytes.HasSuffix(s, tld) {
			return true, trimmedS
		}
	}
	return false, s
})

Emails finds email addresses.

View Source
var Hashtags = FieldFunc("hashtag", func(s []byte) (bool, []byte) {
	trimmedS := TrimPunctuation(s)
	return bytes.HasPrefix(trimmedS, []byte("#")), trimmedS
})

Hashtags finds #hashtags.

View Source
var Mentions = FieldFunc("mention", func(s []byte) (bool, []byte) {
	trimmedS := TrimPunctuation(s)
	return bytes.HasPrefix(trimmedS, []byte("@")), trimmedS
})

Mentions finds @twitter style mentions.

View Source
var Punctuation = `.?!'",;`

Punctuation is a string of common punctuation and quotation characters.

View Source
var URLs = FieldFunc("url", func(s []byte) (bool, []byte) {
	trimmedS := TrimPunctuation(s)
	if bytes.Contains(s, []byte("@")) {
		return false, s
	}
	if bytes.HasPrefix(trimmedS, []byte("http")) || bytes.HasPrefix(trimmedS, []byte("www")) {
		return true, trimmedS
	}
	for _, tld := range tlds {
		if bytes.HasSuffix(trimmedS, tld) {
			return true, trimmedS
		}
	}
	return false, s
})

URL finds web addresses.

Functions

func TrimPunctuation

func TrimPunctuation(s []byte) []byte

TrimPunctuation trims off Punctuation characters.

Types

type ErrNoMatch

type ErrNoMatch []byte

ErrNoMatch is returned when a match was expected but can not be found. Can be returned from FieldFunc.

func (ErrNoMatch) Error

func (e ErrNoMatch) Error() string

type Expander

type Expander map[string]func(b string) string

Expander holds a map of note kinds to expander functions that will be used to replace the text in the original string.

func (Expander) Expand

func (e Expander) Expand(s string, notes Notes) string

Expand generates a new string by calling the expander function for each note depending on its kind.

type Finder

type Finder interface {
	Find(s []byte) (Notes, error)
}

Finder represents types capable of finding notes.

type FinderFunc

type FinderFunc func(s []byte) (Notes, error)

FinderFunc represents a function capable of finding notes.

func FieldFunc

func FieldFunc(kind string, fn func(b []byte) (bool, []byte)) FinderFunc

FieldFunc returns a FinderFunc that finds notes on a per field basis. The fn returns true if it's a match, and optionally a subset of the the match. The returned []byte must be contained in the source string, otherwise ErrNoMatch will be returned.

func (FinderFunc) Find

func (fn FinderFunc) Find(s []byte) (Notes, error)

Find calls the FinderFunc.

type Note

type Note struct {
	Val   []byte `json:"val"`
	Start int    `json:"start"`
	Kind  string `json:"kind"`
}

Note represents something interesting within text.

func (*Note) End

func (n *Note) End() int

End calculates the end position of this note.

func (*Note) String

func (n *Note) String() string

type Notes

type Notes []*Note

Notes is a sortable slice of Note objects.

func FindMany

func FindMany(src []byte, finders ...Finder) (Notes, error)

FindMany runs all finders against the source and returns a slice of notes or an error.

func FindManyString

func FindManyString(src string, finders ...Finder) (Notes, error)

FindManyString runs all finders against the source and returns a slice of notes or an error.

func FindString

func FindString(finder Finder, s string) (Notes, error)

FindString uses the Finder to find notes within the specified string.

func (Notes) Len

func (n Notes) Len() int

func (Notes) Less

func (n Notes) Less(i, j int) bool

func (Notes) Swap

func (n Notes) Swap(i, j int)

Directories

Path Synopsis
Package emoji provides finders and expanders for anno.
Package emoji provides finders and expanders for anno.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL