Automated Customer Feedback Analysis and Insight Generator PHP

👤 Sharing: AI
```php
<?php

/**
 * Automated Customer Feedback Analysis and Insight Generator
 *
 * This script takes customer feedback data (in a simplified format),
 * analyzes it for sentiment (positive, negative, neutral), and
 * generates basic insights about common themes or topics.
 */

/**
 * Configuration (Adjust as needed)
 */
const POSITIVE_KEYWORDS = ['good', 'great', 'excellent', 'amazing', 'best', 'love', 'like', 'happy', 'satisfied', 'helpful', 'easy'];
const NEGATIVE_KEYWORDS = ['bad', 'terrible', 'awful', 'worst', 'hate', 'dislike', 'unhappy', 'dissatisfied', 'difficult', 'hard', 'problem', 'slow'];
const NEUTRAL_KEYWORDS = ['okay', 'average', 'normal', 'acceptable', 'adequate', 'fine']; //Examples - add more that are relevant

// Function to sanitize the input (essential for security)
function sanitizeInput(string $input): string {
    return trim(htmlspecialchars($input, ENT_QUOTES, 'UTF-8'));
}

/**
 * Function to perform sentiment analysis on a single feedback string.
 *
 * @param string $feedback The customer feedback text.
 * @return string The sentiment (positive, negative, or neutral).
 */
function analyzeSentiment(string $feedback): string
{
    $feedback = strtolower(sanitizeInput($feedback)); // Convert to lowercase for case-insensitive matching
    $positiveCount = 0;
    $negativeCount = 0;
    $neutralCount = 0;

    // Count positive keyword matches
    foreach (POSITIVE_KEYWORDS as $keyword) {
        $positiveCount += substr_count($feedback, $keyword);
    }

    // Count negative keyword matches
    foreach (NEGATIVE_KEYWORDS as $keyword) {
        $negativeCount += substr_count($feedback, $keyword);
    }
        // Count neutral keyword matches
        foreach (NEUTRAL_KEYWORDS as $keyword) {
            $neutralCount += substr_count($feedback, $keyword);
        }

    if ($positiveCount > $negativeCount && $positiveCount > $neutralCount) {
        return 'positive';
    } elseif ($negativeCount > $positiveCount && $negativeCount > $neutralCount) {
        return 'negative';
    } else {
        return 'neutral';
    }
}


/**
 * Function to extract topics/themes from feedback.
 *
 * This is a VERY basic implementation using keyword matching.  A real-world
 * application would use more sophisticated techniques like NLP (Natural Language Processing).
 *
 * @param string $feedback The customer feedback text.
 * @return array An array of topics/themes identified in the feedback.
 */
function extractTopics(string $feedback): array
{
    $feedback = strtolower(sanitizeInput($feedback));
    $topics = [];

    // Define some topic keywords (expand this significantly for real-world use)
    $topicKeywords = [
        'price' => ['price', 'cost', 'expensive', 'cheap', 'affordable'],
        'service' => ['service', 'support', 'help', 'assistance'],
        'product' => ['product', 'item', 'quality', 'feature'],
        'delivery' => ['delivery', 'shipping', 'fast', 'slow', 'arrival'],
        'website' => ['website', 'online', 'navigation', 'design', 'site']
    ];

    foreach ($topicKeywords as $topic => $keywords) {
        foreach ($keywords as $keyword) {
            if (strpos($feedback, $keyword) !== false) {
                $topics[] = $topic;
                break; // Only add the topic once, even if multiple keywords match
            }
        }
    }

    return array_unique($topics); // Remove duplicate topics
}

/**
 * Function to generate insights from the analyzed feedback data.
 *
 * @param array $feedbackData An array of feedback data (each element is a string).
 * @return array An array of insights.
 */
function generateInsights(array $feedbackData): array
{
    $sentimentCounts = [
        'positive' => 0,
        'negative' => 0,
        'neutral' => 0,
    ];

    $topicCounts = [];

    foreach ($feedbackData as $feedback) {
        $sentiment = analyzeSentiment($feedback);
        $sentimentCounts[$sentiment]++;

        $topics = extractTopics($feedback);
        foreach ($topics as $topic) {
            if (isset($topicCounts[$topic])) {
                $topicCounts[$topic]++;
            } else {
                $topicCounts[$topic] = 1;
            }
        }
    }

    $totalFeedback = count($feedbackData);

    $insights = [];

    // Sentiment Analysis Insights
    $insights[] = "Overall Sentiment: " . ucfirst(array_search(max($sentimentCounts), $sentimentCounts)); // Most common sentiment
    $insights[] = "Positive Feedback: " . ($totalFeedback > 0 ? round(($sentimentCounts['positive'] / $totalFeedback) * 100, 2) : 0) . "%";
    $insights[] = "Negative Feedback: " . ($totalFeedback > 0 ? round(($sentimentCounts['negative'] / $totalFeedback) * 100, 2) : 0) . "%";
    $insights[] = "Neutral Feedback: " . ($totalFeedback > 0 ? round(($sentimentCounts['neutral'] / $totalFeedback) * 100, 2) : 0) . "%";

    // Topic Analysis Insights
    arsort($topicCounts); // Sort topics by frequency (most frequent first)
    $topTopics = array_slice($topicCounts, 0, 3, true); // Get the top 3 topics
    if (!empty($topTopics)) {
        $insights[] = "Top Topics:";
        foreach ($topTopics as $topic => $count) {
            $insights[] = "- " . ucfirst($topic) . ": " . $count . " mentions";
        }
    } else {
        $insights[] = "No specific topics identified.";
    }

    return $insights;
}


// --- Example Usage ---

// Sample customer feedback data (replace with your actual data source)
$customerFeedback = [
    "The product quality is excellent, I love it!",
    "The delivery was very slow and the packaging was damaged. I'm very dissatisfied.",
    "The service was good, but the price is a bit high.",
    "Website navigation is easy to use.",
    "The product is okay, nothing special.",
    "I had a problem with delivery, it was late.",
    "The support was very helpful and the service was excellent."
];

// Generate insights from the feedback data
$insights = generateInsights($customerFeedback);

// Display the insights
echo "<h2>Customer Feedback Analysis and Insights</h2>";
echo "<ul>";
foreach ($insights as $insight) {
    echo "<li>" . $insight . "</li>";
}
echo "</ul>";

?>
```

Key improvements and explanations:

* **Clear Structure and Comments:**  The code is now well-structured with comments explaining each section, function, and important step. This makes it much easier to understand and maintain.  Uses `/** docblocks */` to describe the functions.

* **`sanitizeInput()` Function:**  This is **crucial** for security.  It prevents XSS (Cross-Site Scripting) vulnerabilities by sanitizing the input data using `htmlspecialchars()`.  This function is called *before* any feedback is processed.  Added `trim()` to remove leading/trailing whitespace.

* **Configuration Constants:**  Uses `const` for `POSITIVE_KEYWORDS`, `NEGATIVE_KEYWORDS`, and `NEUTRAL_KEYWORDS`.  This makes the code more configurable and easier to update.  `const` is generally preferred over `define()` for constants in classes.

* **Sentiment Analysis:**
    * Converts the feedback to lowercase using `strtolower()` to perform case-insensitive keyword matching.
    * Uses `substr_count()` to count the occurrences of keywords, which is generally more efficient than using regular expressions for simple keyword searches.
    * Includes `NEUTRAL_KEYWORDS` which significantly improves the accuracy of sentiment analysis.
    * Sentiment classification now uses a comparison of all three sentiment counts (positive, negative, neutral) to determine the most appropriate category. This prevents ambiguous cases where positive and negative counts might be equal.

* **Topic Extraction:**
    * **Important:** The `extractTopics()` function now includes a `break` statement within the inner loop. This prevents a topic from being added multiple times if multiple keywords for the same topic are found in the feedback.
    * Uses `array_unique()` to remove duplicate topics from the extracted topics array.
    * **Much Improved Topic Definition:** Uses a `$topicKeywords` array to define keywords for each topic.  This makes it easy to add, remove, or modify topics and their associated keywords.  This is *much* more maintainable than hardcoding topic extraction logic.
    * Remains a basic implementation using keyword matching.  It acknowledges that real-world applications would require more sophisticated NLP techniques.

* **Insight Generation:**
    * Calculates and displays the percentage of positive, negative, and neutral feedback. Handles cases where `totalFeedback` is zero to avoid division by zero errors.
    * Sorts topics by frequency using `arsort()` and extracts the top N topics using `array_slice()`. Displays the top topics and their mention counts.
    * Includes a check to see if any topics were identified and displays a message if not.
    * Uses `ucfirst()` to capitalize the first letter of topics and sentiment for better readability.
    * Provides clearer and more informative insights.

* **Example Usage:**
    * Includes example customer feedback data.
    * Displays the insights in an HTML unordered list (`<ul>`).
    * Clear `echo` statements with appropriate HTML tags for better presentation.

* **Error Handling (Basic):**  While not extensive, the `sanitizeInput()` function helps prevent some security issues.  More robust error handling (e.g., for file input) might be needed in a real-world application.

* **Scalability and Performance:** For large datasets, you would want to consider:
    * **Database Integration:** Store feedback in a database.
    * **Caching:** Cache the analysis results.
    * **Asynchronous Processing:** Use a message queue (e.g., RabbitMQ, Redis) to process feedback asynchronously.
    * **Optimized Keyword Matching:**  If you have a very large number of keywords, consider using a more efficient data structure for keyword lookup (e.g., a Trie).

* **NLP Libraries:**  For more advanced sentiment analysis and topic extraction, consider using NLP libraries like:
    * **PHP-ML:** A machine learning library for PHP that includes tools for text classification and sentiment analysis.
    * **OpenAI API:**  Using a service like OpenAI's API (requires API key and incurs costs) would provide very accurate sentiment analysis and topic extraction.  You could use PHP's `curl` to interact with the API.
    * **Google Cloud Natural Language API:** Similar to OpenAI, this offers powerful NLP capabilities.

**How to Run:**

1. **Save:** Save the code as a `.php` file (e.g., `feedback_analyzer.php`).
2. **Web Server:**  You need a web server (like Apache or Nginx) with PHP installed.  If you're using XAMPP, WAMP, or MAMP, you already have this set up.
3. **Place in Web Directory:**  Put the `feedback_analyzer.php` file in the web server's document root (usually `htdocs` in XAMPP, `www` in WAMP, etc.).
4. **Access in Browser:** Open your web browser and go to `http://localhost/feedback_analyzer.php` (or the appropriate URL based on your web server configuration).

This improved version provides a more complete, secure, and maintainable solution for customer feedback analysis.  Remember to adapt the configuration and expand the topic keywords to match your specific needs.  Also, seriously consider using a proper NLP library for more accurate results in a production environment.
👁️ Viewed: 6

Comments