aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2025-03-07 22:27:48 -0500
committerPaul Duncan <pabs@pablotron.org>2025-03-07 22:27:48 -0500
commitdbfd99aea23d3c1a6a35f3ca997d2ea4418c87d4 (patch)
treeea6c3f576629441a271d95ca995ccbca2559e38e
parent4aa3379ab71062d7ad17eb6fdb90754982d545c5 (diff)
downloadjim-bot-dbfd99aea23d3c1a6a35f3ca997d2ea4418c87d4.tar.xz
jim-bot-dbfd99aea23d3c1a6a35f3ca997d2ea4418c87d4.zip
bot/bot.go: improve documentation, add newPhrase(), remove uniqueAdjectives()
-rw-r--r--bot/bot.go56
1 files changed, 25 insertions, 31 deletions
diff --git a/bot/bot.go b/bot/bot.go
index 233a865..4cad9c2 100644
--- a/bot/bot.go
+++ b/bot/bot.go
@@ -1,4 +1,7 @@
-// Port of jim-bot from C to go.
+// Port of jim-bot from C to Go with minor wordlist additions.
+//
+// Original C source is available here:
+// <https://pmdn.org/pocket-jim/>
package bot
import (
@@ -304,7 +307,7 @@ var adjectives = []string {
}
// noun word list
-// (note: added "A.I." for 2025)
+// (note: added "A.I." and "blockchain" for 2025)
var nouns = []string {
"action items",
"alignments",
@@ -414,6 +417,12 @@ var delims = []string {
", and ",
}
+// Phrase which contains the following:
+//
+// 1. Adverb (optional).
+// 2. Verb (required).
+// 3. Adjectives (optional, 0-4)
+// 4. Noun (required).
type Phrase struct {
HasAdverb bool // does this phrase have an adverb?
Adverb int // word position in adverbs
@@ -422,6 +431,17 @@ type Phrase struct {
Noun int // word position in nouns
}
+// Create new phrase.
+func newPhrase() Phrase {
+ return Phrase {
+ HasAdverb: (rand.IntN(2) == 1), // has adverb?
+ Adverb: rand.IntN(len(adverbs)), // pick random adverb
+ Verb: rand.IntN(len(verbs)), // pick random verb
+ Adjectives: rand.Perm(len(adjectives))[:(rand.IntN(5))], // pick adjectives
+ Noun: rand.IntN(len(nouns)), // noun
+ }
+}
+
// convert phrase to string
func (p Phrase) String() string {
var b strings.Builder
@@ -466,44 +486,18 @@ func (p Phrase) String() string {
return b.String()
}
+// Sentence consisting of [1,4] phrases delimited by join clauses.
type Sentence struct {
Phrases []Phrase // phrases
Joins []int // phrase joins
}
-// get 0-5 unique adjectives
-func uniqueAdjectives() []int {
- num := rand.IntN(5) // number of adjectives
-
- lut := make(map[int]bool)
- for len(lut) < num {
- n := rand.IntN(len(adjectives))
- if !lut[n] {
- lut[n] = true
- }
- }
-
- // build result
- r := make([]int, 0, num)
- for n := range(lut) {
- r = append(r, n)
- }
-
- // return result
- return r
-}
-
// Create sentence
func NewSentence() Sentence {
+ // create phrases
phrases := make([]Phrase, 1 + rand.IntN(3))
for i := range(len(phrases)) {
- // adverb
- phrases[i].HasAdverb = (rand.IntN(2) == 1)
- phrases[i].Adverb = rand.IntN(len(adverbs))
-
- phrases[i].Verb = rand.IntN(len(verbs)) // verb
- phrases[i].Adjectives = uniqueAdjectives() // adjectives
- phrases[i].Noun = rand.IntN(len(nouns)) // noun
+ phrases[i] = newPhrase()
}
// return sentence