mention

package
v0.96.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2023 License: Apache-2.0, MIT Imports: 4 Imported by: 0

README

mention Build Status Coverage Status GoDoc

mention parses twitter like mentions and hashtags like @gernest and #Tanzania from text input.

Installation

go get github.com/gernest/mention

Usage

mention is flexible, meaning it is not only limited to @ and # tags. You can choose whatever tag you like and mention will take it from there.

twitter like mentions

For instance you have the following message

hello @gernesti I would like to follow you on twitter

And you want to know who was mentioned in the text.

package main

import (
	"fmt"
	"strings"

	"github.com/gernest/mention"
)

func main() {
	message := "hello @gernest I would like to follow you on twitter"

	tags := mention.GetTags('@', message)
	tagStrings := mention.GetTagsAsUniqueStrings('@', message)

	fmt.Println(tags)
	fmt.Println(tagStrings)
}

If you run the above example it will print [gernest] is the stdout.

twitter like hashtags

For instance you have the following message

how does it feel to be rejected? #loner

And you want to know the hashtags

package main

import (
	"fmt"
	"strings"

	"github.com/gernest/mention"
)

func main() {
	message := "how does it feel to be rejected? #loner"

	tags := mention.GetTags('#', message)

	fmt.Println(tags)
}

If you run the above example it will print [loner] in the stdout.

The API

mention exposes only one function GetTags(char rune, src string) []string

The first argument char is the prefix for your tag, this can be @ or # or whatever unicode character you prefer. Don't be worried by its type rune it is just your normal characters but in single quotes. See the examples for more information.

The second argument is the source of the input which can be from texts.

Contributing

Start with clicking the star button to make the author and his neighbors happy. Then fork the repository and submit a pull request for whatever change you want to be added to this project.

If you have any questions, just open an issue.

Author

Geofrey Ernest Twitter : @gernesti

Chad Barraford Github : @cbarraford

Licence

This project is released under the MIT licence. See LICENCE for more details.

Documentation

Overview

Package mention provides function for parsing twitter like mentions and hashtags

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeUTF16ToString

func DecodeUTF16ToString(s []uint16) string

func EncodeStringToUTF16

func EncodeStringToUTF16(s string) []uint16

func EncodeStringToUTF16Index

func EncodeStringToUTF16Index(s string) []int

Encode returns the UTF-16 encoding of the Unicode code point sequence s.

func GetTagsAsUniqueStrings

func GetTagsAsUniqueStrings(prefix rune, str string, terminator ...rune) (strs []string)

GetTagsAsUniqueStrings gets all tags as a slice of unique strings. This is here to have a means of being somewhat backwards compatible with previous versions of mention

func GetTagsAsUniqueUTF16Strings

func GetTagsAsUniqueUTF16Strings(prefix rune, str []uint16, terminator ...rune) (strs [][]uint16)

GetTagsAsUniqueStrings gets all tags as a slice of unique strings. This is here to have a means of being somewhat backwards compatible with previous versions of mention

Types

type Tag

type Tag struct {

	// The character used to mark the beginning of the tag.
	Char rune

	// Tag non space string that follows after the tag character mark.
	Tag string

	// Tag non space string that follows after the tag character mark.
	TagUTF16 []uint16

	// Index is the byte position in the source string where the tag was found.
	Index int
}

Tag is string that is prefixed with a marker. Often used to mark users like @genrest.

func GetTags

func GetTags(prefix rune, str string, terminator ...rune) (tags []Tag)

GetTags returns a slice of Tags, that is all characters after rune char up to occurrence of space or another occurrence of rune char. Additionally you can provide a coma separated unicode characters to be used as terminating sequence.

Example (Hashtag)
msg := " viva la #tanzania"
tags := GetTagsAsUniqueStrings('#', msg)
fmt.Println(tags)
Output:

[tanzania]
Example (Mention)
msg := " hello @gernest"
tags := GetTagsAsUniqueStrings('@', msg)
fmt.Println(tags)
Output:

[gernest]

func GetUTF16Tags

func GetUTF16Tags(prefix rune, str []uint16, terminator ...rune) (tags []Tag)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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