ghal

package
v0.0.0-...-363a0e4 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2018 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ExclamationMark = MakeWord(".", "!")
View Source
var Period = MakeWord(".", ".")
View Source
var QuestionMark = MakeWord(".", "?")

Functions

func SetDebugLog

func SetDebugLog(w io.Writer, prefix string)

SetDebugLog enables debug logging for this package, writing information to the given writer about how sentence construction is proceeding, etc.

The exact format of this debug information is not part of the package interface and is subject to change in future releases.

Types

type Brain

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

Brain is the main type in this package, containing all of the state for a single instance of the chatbot.

func LoadBrain

func LoadBrain(r io.Reader) (*Brain, error)

LoadBrain reads a serialized brain from the given reader, which must be in the format created by Brain.Save.

func LoadBrainFile

func LoadBrainFile(filename string) (*Brain, error)

LoadBrainFile is like LoadBrain but it first opens the given filename and then reads data from it.

func NewBrain

func NewBrain() *Brain

NewBrain allocates and returns a new, empty brain, devoid of knowledge and ready to learn.

func (*Brain) AddSentence

func (b *Brain) AddSentence(s Sentence)

AddSentence teaches the brain about the given sentence, allowing parts of it to be used in constructing replies.

func (*Brain) AddSentences

func (b *Brain) AddSentences(ss []Sentence)

AddSentences teaches the brain about all of the given sentences. This is like AddSentence but perhaps more convenient when loading training data.

func (*Brain) MakeQuestion

func (b *Brain) MakeQuestion() Sentence

MakeQuestion constructs a random question sentence using all of the question-sentence-terminals the brain has learned. This could be used to try to change the subject if normal reply behavior fails.

This method can itself return a nil sentence if the brain hasn't yet seen any sentences that terminate with a question mark.

func (*Brain) MakeReason

func (b *Brain) MakeReason() Sentence

MakeReason constructs a random constructs a response question starting with the word "because".

This method can itself return a nil sentence if the brain hasn't yet seen any sentences that begin with the word.

func (*Brain) MakeReply

func (b *Brain) MakeReply(ss ...Sentence) Sentence

MakeReply takes one or more sentences and constructs a sentence in reply to them. This method constructs a number of candidate sentences using keywords from the given sentence and then assigns each a relevance score based on matching keywords from the given sentence. It returns one of the sentences with the highest relevance score.

It is possible that there will be no reply at all if the brain doesn't know anything about the words in the given sentence. This is particularly likely for smaller brains. In that case, the return value is a nil Sentence.

func (*Brain) MakeSentenceStartingKeyword

func (b *Brain) MakeSentenceStartingKeyword(w Word) Sentence

MakeSentenceStartingKeyword is like MakeSentenceWithKeyword but the given keyword must begin the sentence.

func (*Brain) MakeSentenceWithKeyword

func (b *Brain) MakeSentenceWithKeyword(w Word) Sentence

MakeSentenceWithKeyword constructs a new sentence containing the given keyword.

Will return nil if no sentence can be constructed for the given keyword.

func (*Brain) Save

func (b *Brain) Save(w io.Writer) error

Save writes a snapshot of the receiving brain's contents into the given writer in a binary format that can be reloaded later with LoadBrain.

func (*Brain) SaveFile

func (b *Brain) SaveFile(filename string) error

SaveFile is like Save but it creates a file with the given filename and then writes the data to it.

type Sentence

type Sentence []Word

func ParseText

func ParseText(text string) ([]Sentence, error)

func (Sentence) Nouns

func (s Sentence) Nouns() WordSet

Nouns returns a set of all of the distinct nouns in the sentence.

func (Sentence) ProperNouns

func (s Sentence) ProperNouns() WordSet

ProperNouns returns a set of all of the distinct proper nouns in the sentence.

func (Sentence) String

func (s Sentence) String() string

func (Sentence) StringTagged

func (s Sentence) StringTagged() string

StringTagged is a variant of String that includes the parts-of-speech tag information, using the common word/TAG notation.

func (Sentence) TrimPeriod

func (s Sentence) TrimPeriod() Sentence

TrimPeriod tests whether the final "word" in the receiver is a period and if so returns a new slice with the same backing array that does not include that trailing period. Otherwise, returns the receiver verbatim.

This method does not trim any other sort of sentence-terminating punctuation, such as question marks. It is intended to emulate common casual chatroom writing style where periods are usually elided at the ends of sentences.

This can either be used prior to adding a new sentence to a brain (to cause the brain to learn sentences without trailing periods) or on a sentence constructed by a brain (to cosmetically remove trailing periods even though the brain itself considers them part of a sentence).

func (Sentence) Words

func (s Sentence) Words() WordSet

Words returns a set of all of the distinct words in the sentence.

type Word

type Word struct {
	Tag  string
	Text string
}

func MakeWord

func MakeWord(tag, text string) Word

func (Word) GoString

func (w Word) GoString() string

func (Word) IsAtMention

func (w Word) IsAtMention() bool

func (Word) IsHashtag

func (w Word) IsHashtag() bool

func (Word) IsNoun

func (w Word) IsNoun() bool

func (Word) IsProperNoun

func (w Word) IsProperNoun() bool

func (Word) MarshalJSON

func (w Word) MarshalJSON() ([]byte, error)

func (*Word) UnmarshalJSON

func (w *Word) UnmarshalJSON(src []byte) error

type WordSet

type WordSet map[Word]struct{}

func (WordSet) Add

func (s WordSet) Add(k Word)

func (WordSet) ChooseOneRandom

func (s WordSet) ChooseOneRandom() Word

ChooseOneRandom is like ChooseRandom but returns only a single chain. Will panic if called on an empty set.

func (WordSet) ChooseRandom

func (s WordSet) ChooseRandom(n int) []Word

ChooseRandom will choose up to n words pseudo-randomly from the receiving set, returning a slice with n or fewer elements.

func (WordSet) ChooseRandomInto

func (s WordSet) ChooseRandomInto(into []Word) []Word

ChooseRandomInto is like ChooseRandom but allows the caller to provide the target buffer. The length of the given slice decides the maximum number to choose, and the result is a slice with the same backing array that may be shorter if there were not enough items in the set to fill it.

func (WordSet) Has

func (s WordSet) Has(k Word) bool

func (WordSet) Nouns

func (s WordSet) Nouns() WordSet

Nouns returns the set of words within the receiver that are nouns.

func (WordSet) ProperNouns

func (s WordSet) ProperNouns() WordSet

ProperNouns returns the set of words within the receiver that are proper nouns.

func (WordSet) Union

func (s WordSet) Union(others ...WordSet) WordSet

Union returns the union of the receiver and all of the other given sets.

Jump to

Keyboard shortcuts

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