Documentation ¶
Index ¶
- Variables
- func SetDebugLog(w io.Writer, prefix string)
- type Brain
- func (b *Brain) AddSentence(s Sentence)
- func (b *Brain) AddSentences(ss []Sentence)
- func (b *Brain) MakeQuestion() Sentence
- func (b *Brain) MakeReason() Sentence
- func (b *Brain) MakeReply(ss ...Sentence) Sentence
- func (b *Brain) MakeSentenceStartingKeyword(w Word) Sentence
- func (b *Brain) MakeSentenceWithKeyword(w Word) Sentence
- func (b *Brain) Save(w io.Writer) error
- func (b *Brain) SaveFile(filename string) error
- type Sentence
- type Word
- type WordSet
- func (s WordSet) Add(k Word)
- func (s WordSet) ChooseOneRandom() Word
- func (s WordSet) ChooseRandom(n int) []Word
- func (s WordSet) ChooseRandomInto(into []Word) []Word
- func (s WordSet) Has(k Word) bool
- func (s WordSet) Nouns() WordSet
- func (s WordSet) ProperNouns() WordSet
- func (s WordSet) Union(others ...WordSet) WordSet
Constants ¶
This section is empty.
Variables ¶
var ExclamationMark = MakeWord(".", "!")
var Period = MakeWord(".", ".")
var QuestionMark = MakeWord(".", "?")
Functions ¶
func SetDebugLog ¶
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 ¶
LoadBrain reads a serialized brain from the given reader, which must be in the format created by Brain.Save.
func LoadBrainFile ¶
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 ¶
AddSentence teaches the brain about the given sentence, allowing parts of it to be used in constructing replies.
func (*Brain) AddSentences ¶
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 ¶
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 ¶
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 ¶
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 ¶
MakeSentenceStartingKeyword is like MakeSentenceWithKeyword but the given keyword must begin the sentence.
func (*Brain) MakeSentenceWithKeyword ¶
MakeSentenceWithKeyword constructs a new sentence containing the given keyword.
Will return nil if no sentence can be constructed for the given keyword.
type Sentence ¶
type Sentence []Word
func (Sentence) ProperNouns ¶
ProperNouns returns a set of all of the distinct proper nouns in the sentence.
func (Sentence) StringTagged ¶
StringTagged is a variant of String that includes the parts-of-speech tag information, using the common word/TAG notation.
func (Sentence) TrimPeriod ¶
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).
type Word ¶
func (Word) IsAtMention ¶
func (Word) IsProperNoun ¶
func (Word) MarshalJSON ¶
func (*Word) UnmarshalJSON ¶
type WordSet ¶
type WordSet map[Word]struct{}
func (WordSet) ChooseOneRandom ¶
ChooseOneRandom is like ChooseRandom but returns only a single chain. Will panic if called on an empty set.
func (WordSet) ChooseRandom ¶
ChooseRandom will choose up to n words pseudo-randomly from the receiving set, returning a slice with n or fewer elements.
func (WordSet) ChooseRandomInto ¶
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) ProperNouns ¶
ProperNouns returns the set of words within the receiver that are proper nouns.