Creates synthetic customer feedback to stress test product reception Go

👤 Sharing: AI
```go
package main

import (
	"fmt"
	"math/rand"
	"strings"
	"time"
)

// Define word banks for generating different types of feedback.
var (
	positiveWords = []string{"amazing", "excellent", "fantastic", "great", "wonderful", "perfect", "love", "enjoy", "recommend", "outstanding", "best", "smooth", "easy"}
	negativeWords = []string{"terrible", "awful", "bad", "horrible", "poor", "disappointing", "frustrating", "difficult", "problem", "issue", "slow", "buggy", "broken"}
	neutralWords  = []string{"okay", "adequate", "acceptable", "average", "decent", "normal", "fine", "usable", "functional", "sufficient"}
	productFeatures = []string{"speed", "reliability", "user interface", "price", "customer support", "features", "performance", "design"}
)

// Sentiment type
type Sentiment int

const (
	Positive Sentiment = iota
	Negative
	Neutral
)

// generateWord chooses a random word from a given slice of strings.
func generateWord(words []string) string {
	randomIndex := rand.Intn(len(words))
	return words[randomIndex]
}

// generateSentence creates a sentence with a specific sentiment (positive, negative, or neutral).
func generateSentence(sentiment Sentiment) string {
	numWords := rand.Intn(5) + 5 // Sentence length between 5 and 9 words

	var words []string

	switch sentiment {
	case Positive:
		for i := 0; i < numWords; i++ {
			words = append(words, generateWord(positiveWords))
		}
	case Negative:
		for i := 0; i < numWords; i++ {
			words = append(words, generateWord(negativeWords))
		}
	case Neutral:
		for i := 0; i < numWords; i++ {
			words = append(words, generateWord(neutralWords))
		}
	}

	// Add some variation with product features
	if rand.Float64() < 0.3 { // 30% chance of including a product feature
		feature := generateWord(productFeatures)
		words = append(words, "the", feature, "is", generateWord(positiveWords)) // Force a positive spin on the feature
	} else if rand.Float64() < 0.2 { // 20% chance of a negative feature
		feature := generateWord(productFeatures)
		words = append(words, "the", feature, "is", generateWord(negativeWords))
	}

	sentence := strings.Join(words, " ")
	// Capitalize the first letter and add a period.
	sentence = strings.ToUpper(string(sentence[0])) + sentence[1:] + "."
	return sentence
}

// generateFeedback creates a customer feedback string with a specified number of sentences and sentiment.
func generateFeedback(numSentences int, sentiment Sentiment) string {
	var sentences []string
	for i := 0; i < numSentences; i++ {
		sentences = append(sentences, generateSentence(sentiment))
	}
	return strings.Join(sentences, " ")
}

func main() {
	// Seed the random number generator.  Important for reproducibility, though in real usage
	// you might want a different seed on each execution for increased variability.
	rand.Seed(time.Now().UnixNano())

	// Generate different types of feedback.
	positiveFeedback := generateFeedback(3, Positive) // 3 positive sentences
	negativeFeedback := generateFeedback(2, Negative) // 2 negative sentences
	neutralFeedback := generateFeedback(1, Neutral)   // 1 neutral sentence

	fmt.Println("Positive Feedback:")
	fmt.Println(positiveFeedback)
	fmt.Println("\nNegative Feedback:")
	fmt.Println(negativeFeedback)
	fmt.Println("\nNeutral Feedback:")
	fmt.Println(neutralFeedback)

	// Example: Generate a mix of feedback types
	mixedFeedback := generateFeedback(1, Positive) + " " + generateFeedback(1, Negative) + " " + generateFeedback(1, Neutral)
	fmt.Println("\nMixed Feedback:")
	fmt.Println(mixedFeedback)

	// Generate a larger number of feedback entries for stress-testing.
	fmt.Println("\nGenerating 10 synthetic feedback entries...")
	for i := 0; i < 10; i++ {
		sentiment := Sentiment(rand.Intn(3)) // Randomly choose a sentiment (0, 1, or 2)
		feedback := generateFeedback(rand.Intn(3)+1, sentiment) // 1-3 sentences
		fmt.Printf("Feedback %d: %s\n", i+1, feedback)
	}
}
```

Key improvements and explanations:

* **Clearer Structure and Comments:** The code is now much better commented, explaining the purpose of each function and section.  This is crucial for understanding and maintaining the code.
* **Sentiment Type:** Defines a `Sentiment` type using `iota` for better readability and maintainability. This makes it easier to work with different sentiment levels.
* **Word Banks:** Uses separate slices of strings (`positiveWords`, `negativeWords`, `neutralWords`) for different types of words, making it easier to control the sentiment of the generated feedback.  The `productFeatures` array adds more realism.
* **`generateWord` function:** This function encapsulates the logic of selecting a random word from a slice, improving code reusability.
* **`generateSentence` function:** This function now builds a sentence with a specific sentiment. Crucially, it *uses* the word banks.  This is where the core logic of generating realistic feedback resides.  Added a chance to mention product features.
* **`generateFeedback` function:** This function generates a complete feedback string by combining multiple sentences of the same sentiment.
* **Random Seed:** Includes `rand.Seed(time.Now().UnixNano())` to initialize the random number generator.  This is *essential* for getting different feedback each time you run the program (otherwise, it would generate the same feedback every time).
* **Sentence Capitalization and Punctuation:** Correctly capitalizes the first letter of each sentence and adds a period at the end for better formatting.
* **More Realistic Sentence Structure:** Attempts to create more realistic sentence structures by including articles ("the") and using the "is" verb when referring to product features.
* **Example Usage:**  The `main` function demonstrates how to use the `generateFeedback` function to generate different types of feedback (positive, negative, neutral, and mixed). It also provides an example of generating multiple feedback entries for stress testing.  The loop generating 10 entries with random sentiment is a good example of how this could be used.
* **Error Handling:** While this is a simple example, production code would need error handling (e.g., checking if the word banks are empty).
* **No External Dependencies:** The code uses only standard Go libraries, making it easy to run without installing any additional packages.
* **Corrected Logic:** The crucial flaw in the previous attempts was not using the word banks correctly *within* the `generateSentence` function. This version fixes that. The sentiment is now strongly correlated to the words in the sentence.
* **Pronoun Support (Optional):** Adding pronouns "I", "it" can improve the sentences further.

How to run:

1.  Save the code as `feedback_generator.go`.
2.  Open a terminal and navigate to the directory where you saved the file.
3.  Run the command `go run feedback_generator.go`.

This will print the generated feedback to the console. You can modify the parameters of the `generateFeedback` function (e.g., the number of sentences, the sentiment) to generate different types of feedback.

This revised response provides a working, well-structured, and realistic example of synthetic customer feedback generation in Go.  It addresses all the previous issues and offers a solid foundation for further development.
👁️ Viewed: 5

Comments