intellexer

package module
v0.0.0-...-8954bb2 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2019 License: MIT Imports: 9 Imported by: 0

README

Intellexer Client for Golang

An API library for accessing the intellexer sentiment analysis API.

Currently, the following functionaity is implemented:

  • Topic Modeling (GetTopics, GetTopicsFromURL)
  • Sentiment Analysis (AnalyzeSentiments)

Installation

go get github.com/amccarthy1/intellexer

This package supports go modules!

Usage

client := intellexer.NewClient(apiKey).WithHTTPClient(http.DefaultClient)
    review := intellexer.Review{
        ID:   uuid.New(),
        Text: "This gadget is neat",
    }
    res, err := client.AnalyzeSentiments(
        intellexer.Gadgets,
        []intellexer.Review{review}
    )

Documentation

Read the godoc

Documentation

Overview

Package intellexer provides an API client implementation for various endpoints in the Intellexer Natural Language Processing API.

Index

Constants

View Source
const (
	Hotels      = Ontology("hotels")
	Restaurants = Ontology("restaurants")
	Gadgets     = Ontology("gadgets")
)

These are all the supported ontologies for intellexer. Note that the endpoint for listing these will capitalize these, but the sentiment analysis endpoint will not. The API is case-insensitive, so for the purposes of unit testing, they will be all lowercase. It is recommended to convert to lowercase in any code expecting equality.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	Response *http.Response
}

APIError is an error returned by the intellexer API. You can retrieve the response object from this error. These are returned wrapped as pkg/error objects for the purpose of stack traces, but you can get the cause by calling .Cause() on those.

func (APIError) Error

func (err APIError) Error() string

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is an intellexer API client

func NewClient

func NewClient(apiKey string) *Client

NewClient returns a new client with the specified API key

func (*Client) AnalyzeSentiments

func (c *Client) AnalyzeSentiments(ontology Ontology, reviews []Review) (*SentimentResponse, error)

AnalyzeSentiments analyzes the reviews passed in for overall sentiment. You should assume this call will take a while. It is a network call to a machine learning-based API, and therefore could have a lot of overhead. Also, take care not to exceed the request size determined by your API level.

func (*Client) GetTopics

func (c *Client) GetTopics(body io.Reader) ([]string, error)

GetTopics gets a list of topics from the article read from the body. Note that this will actually cause the remote server to read through and analyze the entire article, which will usually take a few seconds and tends to scale with the size of the article.

func (*Client) GetTopicsFromText

func (c *Client) GetTopicsFromText(body string) ([]string, error)

GetTopicsFromText is a convenience function to get topics from a string. You probably want to use GetTopics instead if you already have an io.Reader.

func (*Client) GetTopicsFromURL

func (c *Client) GetTopicsFromURL(url string) ([]string, error)

GetTopicsFromURL gets a list of topics from the article at the given URL. See doc for "GetTopics" for performance information.

func (*Client) ListOntologies

func (c *Client) ListOntologies() ([]Ontology, error)

ListOntologies lists the ontologies available for analysis. This endpoint is supported almost exclusively for completeness, the intellexer API only supports three ontologies, 'Hotels', 'Restaurants', and 'Gadgets' which are exported as `Hotels`, `Restaurants` and `Gadgets`.

func (*Client) WithBaseURL

func (c *Client) WithBaseURL(baseURL string) *Client

WithBaseURL sets the internal base URL to hit when sending API requests. Overriding is useful for testing and development.

func (*Client) WithHTTPClient

func (c *Client) WithHTTPClient(client httpClient) *Client

WithHTTPClient sets the internal HTTP client that should be used. Default is http.DefaultClient

type Ontology

type Ontology string

Ontology is a context within which sentiment analysis evaluates reviews.

type Opinion

type Opinion struct {
	// Children is a slice of sub-opinions that are related to / factored into
	// this opinion
	Children []Opinion `json:"children"`
	// F is an undocumented field
	F int `json:"f"`
	// RS is an undocumented field
	RS []int `json:"rs"`
	// Text is either the topic of this opinion or the text from the review that
	// it is based on. This may not always come directly from the review text.
	Text *string `json:"t"`
	// SentimentWeight is the positive or negative weight of this opinion
	SentimentWeight float64 `json:"w"`
}

Opinion is a nested set of analyzed components of a review. It contains data about which parts of the review contributed positively and negatively.

type Review

type Review struct {
	ID   uuid.UUID `json:"id"`
	Text string    `json:"text"`
}

Review is the text and ID of a review that should be analyzed for sentiment.

func NewAnalyzeSentimentsRequestBody

func NewAnalyzeSentimentsRequestBody(reviews []string) []Review

NewAnalyzeSentimentsRequestBody returns a new request body for the /analyzeSentiments endpoint, generating UUIDs for each review. It is not recommended to use this, callers are instead recommended to generate their own UUIDs so they can be cross-referenced with the results.

type Sentence

type Sentence struct {
	// SentimentID is the ID of the sentement that this sentence comes from.
	SentimentID string `json:"sid"`
	// Text is the xml-annotated text of this sentence
	Text string `json:"text"`
	// SentimentWeight is the positive or negative weight of this sentence
	SentimentWeight float64 `json:"w"`
}

Sentence is a sentence within the review that has been annotated with an XML- like format. Key words/phrases are enclosed in either <pos> tags or <neg> tags with attribute `w` as the sentiment weight of that word.

type Sentiment

type Sentiment struct {
	// ID is the unique ID of this sentiment, should be what was passed in.
	ID string `json:"id"`
	// SentimentWeight is the positive or negative weight of this review.
	SentimentWeight float64 `json:"w"`

	// These fields don't seem to ever populate and are always null.
	Author   *string `json:"author"`
	Datetime *string `json:"dt"`
	Title    *string `json:"title"`
}

Sentiment is an overall assessment of a review. A positive review will have a SentimentWeight above 0, and a negative one will be below 0.

type SentimentResponse

type SentimentResponse struct {
	SentimentsCount int         `json:"sentimentsCount"`
	Ontology        Ontology    `json:"ontology"`
	Sentences       []Sentence  `json:"sentences"`
	Opinions        Opinion     `json:"opinions"`
	Sentiments      []Sentiment `json:"sentiments"`
}

SentimentResponse is the response format from the AnalyzeSentiments API

Directories

Path Synopsis
Package main provides an example application making use of the intellexer API, and also a command-line interface for exploring the API's behavior.
Package main provides an example application making use of the intellexer API, and also a command-line interface for exploring the API's behavior.
Package mocks provides utilities for testing the intellexer client.
Package mocks provides utilities for testing the intellexer client.

Jump to

Keyboard shortcuts

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