afinngo

package module
v0.0.0-...-86f3116 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2018 License: MIT Imports: 9 Imported by: 0

README

AfinnGo

An AFINN-165-powered sentiment analysis tool written in Go. This is essentially a port of the npm sentiment module, but in Go for a small side-project I am working on.

Use

To access the default English AFINN-165 analyzer, you can use the following:

import "github.com/joshuaslate/afinngo"

func isSentencePositive(sentence string) bool {
  analyzer := afinngo.NewDefaultSentimentAnalyzer()
  output := analyzer.Analyze("There is nothing quite like a warm, sunny day at the beach!")

  return output.Comparative > 0
}

The output of the Analyzer func is:

type SentimentResult struct {
	Score       float64
	Comparative float64
	Tokens      []string
	Words       []string
	Positive    []string
	Negative    []string
}

Customization

You can also use your own analyzation strategy (dictionaries for different languages, handling scoring differently, etc.) by satisfying the ScoringStrategy interface.

type ScoringStrategy interface {
	getScore(tokens []string, currentTokenIndex int, currentTokenScore float64) float64
	getTokens() map[string]float64
	getNegators() map[string]float64
}

// Where customStrategy satisfies the ScoringStrategy interface
myAnalyzer := afinngo.NewSentimentAnalyzerFromStrategy(customStrategy)

Contributions

Contributions are welcome. Please feel empowered to fork this repository, make changes, and open a pull request to send your changes back upstream.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EnglishStrategy

type EnglishStrategy struct {
	Tokens   map[string]float64
	Negators map[string]float64
}

EnglishStrategy is the default scoring strategy (implements ScoringStrategy interface)

func NewEnglishStrategy

func NewEnglishStrategy() *EnglishStrategy

NewEnglishStrategy builds the default sentiment scoring strategy for the English language

type ScoringStrategy

type ScoringStrategy interface {
	// contains filtered or unexported methods
}

ScoringStrategy is an interface that holds a function to determine a scoring strategy

type SentimentAnalyzer

type SentimentAnalyzer struct {
	Strategy ScoringStrategy
	Tokens   map[string]float64
	Negators map[string]float64
}

SentimentAnalyzer is loaded with a dictionary and contains the method to provide sentiment analysis

func NewDefaultSentimentAnalyzer

func NewDefaultSentimentAnalyzer() *SentimentAnalyzer

NewDefaultSentimentAnalyzer returns an English AFINN165 sentiment analyzer

func NewSentimentAnalyzerFromStrategy

func NewSentimentAnalyzerFromStrategy(strategy ScoringStrategy) *SentimentAnalyzer

NewSentimentAnalyzerFromStrategy loads a dictionary and returns a new instance of a SentimentAnalyzer, given a strategy

func (*SentimentAnalyzer) Analyze

func (a *SentimentAnalyzer) Analyze(phrase string) SentimentResult

Analyze a phrase for its sentiment (positive or negative)

type SentimentResult

type SentimentResult struct {
	Score       float64
	Comparative float64
	Tokens      []string
	Words       []string
	Positive    []string
	Negative    []string
}

SentimentResult contains the results of a sentiment analysis

Jump to

Keyboard shortcuts

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