Machine Learning for Business

Chapter 11
Text, Recommendation and Generative AI Applications

Bachelors Program in Business Analytics
Machine Learning for Business   •   Bachelor

Text, Recommendation and Generative AI Applications

Introduction

Business data increasingly arrives as language, interaction histories and requests for generated content. Customer reviews, call-centre transcripts, emails, social-media posts and internal documents contain information that is difficult to analyse with traditional tabular methods. At the same time, recommendation systems influence what customers buy, read or watch, while generative artificial intelligence (AI) produces text, summaries, code and answers.

This chapter examines a practical analytical pipeline: preparing text, representing meaning numerically, analysing sentiment, recommending items and applying neural networks and large language models (LLMs). The emphasis is not on treating AI as an autonomous decision-maker. Rather, business analysts must understand how models work, what evidence supports their outputs, how performance should be evaluated and where human oversight is required.

Learning Objectives

By the end of this chapter, you should be able to:

  1. Explain the stages of a business text-analytics workflow.
  2. Apply tokenisation, normalization, n-grams, TF–IDF and embeddings appropriately.
  3. Interpret sentiment-analysis results and evaluate classification performance.
  4. Explain collaborative, content-based and hybrid recommender systems.
  5. Describe the basic operation of a neural network and a transformer-based LLM.
  6. Distinguish prompting, retrieval-augmented generation and fine-tuning.
  7. Assess generative-AI systems using accuracy, relevance, safety, fairness and operational metrics.
  8. Design human oversight and governance for business applications.

1. From Unstructured Language to Business Evidence

A text-analytics project normally contains six stages:

  1. Problem definition: specify the business decision, users, time horizon and acceptable error.
  2. Data acquisition: collect reviews, tickets, documents, transactions or conversations, while recording consent and provenance.
  3. Preparation: remove duplicates, identify language, handle personal information and create training, validation and test sets.
  4. Representation: convert text into features such as counts, weighted terms or dense embeddings.
  5. Modelling and evaluation: train a model and compare it with a simple baseline using metrics aligned with the business objective.
  6. Deployment and monitoring: integrate the model into a workflow and monitor drift, quality, security and outcomes.

A useful distinction is between a document, a complete business object such as a review; a sentence, a grammatical unit; and a token, a word or word fragment processed by a model. Tokenisation is not merely splitting at spaces. Punctuation, contractions, emojis, codes and multilingual text may carry meaning.

Common preprocessing operations include lowercasing, Unicode normalization, spelling correction, removal of boilerplate and handling of URLs. Stop-word removal and stemming can reduce vocabulary size, but they may also remove important meaning. For example, negation in “not satisfied” should not be discarded. Lemmatization maps a word to a linguistic base form, whereas stemming applies a simpler truncation rule. The correct choice depends on the task and should be validated empirically.

Personal data—names, addresses, account numbers, health information and free-text identifiers—should be minimized, masked or removed where possible. A model trained on leaked customer information can appear accurate while violating privacy and creating unacceptable risk.

2. Representing Text: Counts, TF–IDF and Embeddings

2.1 Bag-of-words and n-grams

The bag-of-words representation records whether, or how often, vocabulary terms occur in a document. It is computationally simple and often effective for classification. However, it ignores word order and meaning. “Customer helps agent” and “agent helps customer” may receive similar representations.

An n-gram is a sequence of n tokens. Unigrams represent individual terms; bigrams capture pairs such as “late delivery”; trigrams can capture phrases such as “credit card declined.” Increasing n generally increases expressive power and dimensionality. Sparse count matrices can become very large, so vocabulary limits and regularization are important.

2.2 TF–IDF

Term frequency–inverse document frequency (TF–IDF) emphasizes terms that are frequent in one document but uncommon across the collection. A common formulation is:

wt,d = tft,d × log(N / dft)

Here, wt,d is the weight of term t in document d, tft,d is its term frequency, N is the number of documents and dft is the number of documents containing the term. The logarithm reduces the influence of very common terms. Different software packages use smoothing and term-frequency normalization, so the exact implementation should be documented.

TF–IDF is appropriate when distinctive vocabulary matters, such as routing support tickets or identifying topics. It does not understand synonyms well: “refund” and “reimbursement” remain separate unless preprocessing or additional features connect them.

2.3 Embeddings

An embedding maps a token, sentence, document or product into a dense vector. Similar meanings are intended to have nearby vectors. A common similarity measure is cosine similarity:

cos(θ) = (x · y) / (||x|| × ||y||)

The vectors x and y represent two texts; x · y is their dot product; ||x|| and ||y|| are their Euclidean lengths; and θ is the angle between them. Cosine similarity is defined for non-zero vectors and is unaffected by multiplying either vector by a positive scalar. In many embedding applications, values closer to 1 indicate greater directional similarity, although the threshold for “similar” must be validated for the specific model and domain.

Static word embeddings assign one vector to a word, so “bank” may have a similar representation in “river bank” and “bank loan.” Contextual embeddings, produced by transformer models, vary with surrounding words and generally represent meaning more effectively. Embeddings are useful for semantic search, clustering, duplicate detection, document classification and recommendation. They are not inherently factual: a vector captures patterns in training data, not verified truth.

A practical embedding workflow is to clean documents, divide them into meaningful chunks, generate embeddings using a documented model, store vectors with metadata and retrieve nearby items for a downstream task. Chunk size, overlap, language and document freshness affect results.

3. Sentiment Analysis and Text Classification

Sentiment analysis estimates attitudes such as positive, neutral or negative. A business may use it to identify deteriorating customer experience, prioritize complaints or compare product launches. Sentiment is not the same as emotion, intent or satisfaction. “The product is expensive but excellent” contains mixed evidence, and sarcasm can reverse the literal meaning.

Approaches range from lexicon-based scoring to supervised machine learning and transformer classifiers. A lexicon method assigns words positive or negative values and combines them, often with rules for negation and intensifiers. It is transparent but weak with domain-specific language. Supervised models learn from labelled examples and can represent local business usage, but labels may be subjective and demographic or linguistic bias can enter the data.

For binary classification, a confusion matrix contains true positives (TP), false positives (FP), true negatives (TN) and false negatives (FN). Important metrics are:

Accuracy = (TP + TN) / (TP + TN + FP + FN)
Precision = TP / (TP + FP)
Recall = TP / (TP + FN)
F1 = 2 × (Precision × Recall) / (Precision + Recall)

Accuracy is the proportion of all predictions that are correct. Precision is the proportion of predicted positives that are correct. Recall is the proportion of actual positives detected. F1 is the harmonic mean of precision and recall and is defined when both are meaningful and their sum is non-zero. These metrics assume a clearly defined positive class and fixed evaluation data. With severe class imbalance, accuracy can be misleading. A fraud or safety-screening system may prioritize recall; a costly intervention may prioritize precision.

For multiclass sentiment, report per-class precision, recall and F1, together with macro averages when each class deserves equal weight and weighted averages when class frequency should influence the summary. Always inspect a confusion matrix and representative errors. A numerical score cannot reveal whether a classifier fails systematically on dialects, short messages or product-specific vocabulary.

4. Recommender Systems

A recommender system estimates which items may be relevant to a user. Items may be products, courses, articles, jobs or suppliers. Recommendations can be based on explicit feedback, such as a rating, or implicit feedback, such as a click, purchase, dwell time or completion. Implicit feedback is abundant but ambiguous: a click does not necessarily mean satisfaction.

4.1 Content-based recommendation

A content-based system represents users through the attributes of items they previously liked and recommends items with similar attributes. It is interpretable and can recommend new items if their content is known. Its weaknesses include overspecialization and difficulty discovering unexpected interests.

4.2 Collaborative filtering

Collaborative filtering uses patterns across users and items. In user-based filtering, users with similar interaction histories influence one another. In item-based filtering, items co-consumed by similar users are compared. These methods may produce useful discovery but face cold-start problems for new users and new items, sparsity and popularity bias.

A common latent-factor model predicts a user–item rating as:

r̂ui = μ + bu + bi + pu · qi

Here, r̂ui is the predicted rating for user u and item i, μ is the global mean, bu and bi are user and item biases, and pu and qi are latent vectors. The dot product captures compatibility. This model assumes that observed interactions provide useful evidence of preference and that a low-dimensional latent structure is adequate. Parameters are learned from historical data, often with regularization to reduce overfitting.

4.3 Evaluation and business impact

Offline evaluation may use ranking metrics such as precision@k, recall@k, mean reciprocal rank or normalized discounted cumulative gain. Precision@k is the fraction of the top k recommendations that are relevant; its denominator is k. Recall@k is the fraction of all relevant items retrieved in the top k, but it requires a meaningful definition of the relevant-item set. Offline results can differ from live performance because users react to recommendations.

Online A/B testing can compare conversion, revenue, retention, margin or satisfaction. Guardrail metrics should include returns, complaints, diversity and exposure concentration. Optimizing clicks alone may encourage sensational or low-quality recommendations. A responsible system balances relevance with business value, customer welfare, fairness and long-term trust.

5. Introductory Neural Networks and Transformers

A basic neuron computes a weighted sum and passes it through an activation function:

z = ∑j=1m wj xj + b
a = f(z)

Here, xj are input features, wj are learned weights, b is a bias, z is the pre-activation value and f is an activation function such as ReLU. A multilayer network composes these transformations. During training, a loss function measures error; backpropagation computes gradients; and an optimizer updates parameters. For a simple gradient-descent update:

w ← w − η × ∂L/∂w

L is the loss, η is a positive learning rate, and ∂L/∂w is the derivative of loss with respect to a parameter. The update assumes differentiable model components and seeks a lower loss; it does not guarantee a global optimum.

For K mutually exclusive classes, the softmax function converts logits zk into probabilities:

pk = exp(zk) / ∑j=1K exp(zj)

The probabilities are non-negative and sum to 1. Softmax outputs should not automatically be interpreted as calibrated probabilities; calibration must be tested.

Transformers process sequences using attention. In simplified scaled dot-product attention:

Attention(Q,K,V) = softmax(QKᵀ / √dk)V

Q, K and V are query, key and value matrices, and dk is the key-vector dimension. Scaling helps control the magnitude of dot products. Multiple attention heads allow the model to capture different relationships. Positional information is added because attention alone does not inherently encode sequence order.

6. Large Language Models and Generative AI

An LLM is a neural model trained to predict tokens from context. Pretraining on large corpora enables broad linguistic and factual patterns; instruction tuning and preference optimization can make responses more useful. A generative model produces an output by sampling or selecting successive tokens. Temperature and related decoding settings affect variation, but changing them does not make an answer factually reliable.

Important business patterns include:

  • Prompting: specifying task, context, constraints, examples and output format.
  • Structured output: requesting JSON, tables or fields that can be validated by software.
  • Summarization and classification: transforming long text into a controlled business workflow.
  • Tool use: allowing a model to call approved search, calculation or transaction systems.
  • Retrieval-augmented generation (RAG): retrieving relevant organizational documents and supplying them as context before generation.
  • Fine-tuning: adapting model parameters using task-specific examples. Fine-tuning changes behaviour and style but is not a guaranteed method for injecting current facts.

A RAG pipeline typically indexes documents, embeds a user query, retrieves passages, optionally reranks them and sends them to the LLM with instructions to answer using the supplied evidence. It can improve freshness and citation, but retrieval errors, incomplete documents, access-control failures and poor chunking remain risks. RAG is not a substitute for source governance.

Hallucination is an unsupported or fabricated output presented as if it were true. It can result from incomplete context, ambiguous prompts, patterns that sound plausible, retrieval failure or model limitations. Mitigations include grounding responses in approved sources, requiring citations, using abstention instructions, validating structured fields, applying deterministic calculations through tools and routing high-impact cases to people.

An LLM should not be granted unrestricted authority to issue refunds, alter records, make employment decisions or provide regulated advice. Least-privilege access, logging, prompt-injection defenses, output filtering and approval thresholds are essential.

7. Evaluating Generative-AI Applications

Evaluation should begin with the use case, not the model brand. A useful evaluation set contains representative, difficult and adversarial examples, including multilingual and edge cases. Separate development data from a held-out test set, and prevent confidential test answers from entering prompts or training.

Evaluation dimensions include:

DimensionExample question
CorrectnessIs the answer supported by authoritative evidence?
RelevanceDoes it address the user’s actual request?
CompletenessAre required issues covered?
GroundingCan claims be traced to retrieved sources?
SafetyDoes it avoid harmful or unauthorized advice?
FairnessDoes performance vary across relevant groups or languages?
UsabilityIs the response understandable and actionable?
EfficiencyAre latency and cost acceptable?

Automatic metrics can compare generated text with references, but semantic equivalence and factual grounding often require human or programmatic checks. Human evaluation should use explicit rubrics, blinded comparisons where possible and multiple reviewers for important tasks. Monitor production outcomes, escalation rates, user corrections, source freshness and drift. A model can score well on a benchmark while failing in the organization’s actual workflow.

8. Governance, Ethics and Organizational Design

Text and recommendation systems can reproduce historical discrimination, expose confidential information or manipulate attention. Governance should define permitted uses, prohibited uses, accountability, retention, access and appeal processes. Data protection principles commonly relevant to business systems include purpose limitation, data minimization, security and transparency; exact legal duties depend on jurisdiction and sector.

Key controls include:

  • a documented data inventory and lawful-use assessment;
  • consent or another appropriate legal basis where required;
  • removal or masking of unnecessary personal data;
  • role-based access and encryption;
  • model cards, data documentation and version control;
  • bias and subgroup testing;
  • audit logs for prompts, retrieved sources and actions;
  • human review for consequential decisions;
  • incident reporting, rollback and vendor-risk assessment.

Human oversight must be substantive rather than ceremonial. Reviewers need authority, time, training and a clear escalation route. If a person merely accepts an AI recommendation because it appears confident, the process has automation bias rather than meaningful oversight.

Applied Case: Illustrative Customer-Service Copilot

Background and problem

A fictional online retailer receives 25,000 support messages each month. Managers want to reduce response time and identify emerging delivery problems. Historical labels are incomplete, and messages contain order numbers and personal details.

Analysis

The analytics team first defines two separate tasks: classify messages into operational categories and draft replies for agents. It removes or masks direct identifiers, records the source and date of each message, and creates a time-based test set so that future performance is not estimated using past information. A TF–IDF logistic-regression baseline is compared with a transformer classifier. For reply drafting, the team uses RAG over approved policy and delivery documents rather than allowing the LLM to rely only on general training.

Approach and solution

The classifier routes messages to delivery, billing, returns and technical-support queues. The copilot retrieves relevant policy passages, drafts a response and displays citations. It is instructed to say when evidence is insufficient. Refunds and account changes remain agent-controlled. Evaluation includes per-class recall, precision, citation-supported factuality, average handling time, escalation rate, customer satisfaction and subgroup error analysis.

A pilot is conducted with a control group receiving the existing workflow. Agents can edit or reject drafts, and all changes are logged. The team also tests prompt injection, such as a customer message that instructs the system to ignore internal policies. Retrieved documents are access-controlled and versioned.

Outcome

In this illustrative case, the pilot reduces handling time while preserving agent authority. The largest gains occur for routine delivery questions, whereas unusual billing disputes still require human investigation. The team does not deploy the system to make autonomous compensation decisions because evidence quality and financial risk are uneven.

Learning points

The case demonstrates that the best business solution is a workflow, not simply a larger model. Baselines establish whether complexity is justified; retrieval improves access to current policy; evaluation must include operational and safety outcomes; and human review is designed into the process rather than added after deployment.

Challenges and Limitations

Text models are sensitive to domain shift, language variation, spelling, sarcasm and changes in customer behaviour. Embeddings can encode social biases and may obscure why two items are considered similar. Recommenders can create feedback loops: popular items receive exposure, generate more interactions and become even more popular. Historical labels may reflect previous organizational decisions rather than objective truth.

Generative systems have non-deterministic outputs, uncertain provenance and substantial infrastructure costs. Confidential data may be exposed through prompts, logs or vendors. Retrieval can return a confidently written but obsolete policy. Human review also has costs and can be inconsistent. Therefore, model selection should consider total system risk, not only accuracy or novelty.

Best-Practice Checklist

  1. Start with a measurable decision and a simple baseline.
  2. Separate training, validation and time-appropriate test data.
  3. Document preprocessing, model versions, thresholds and sources.
  4. Preserve negation and domain terminology when preparing text.
  5. Evaluate minority classes and relevant user groups.
  6. Treat clicks and ratings as behavioural signals, not unquestionable preferences.
  7. Ground generative answers in approved, current evidence.
  8. Require abstention or escalation when confidence or evidence is inadequate.
  9. Minimize personal data and enforce least-privilege access.
  10. Monitor quality, drift, cost, fairness and real business outcomes after launch.

Chapter Summary

Text analytics converts language into evidence through preparation, representation, modelling and evaluation. Bag-of-words and TF–IDF provide transparent sparse features, while embeddings capture semantic relationships useful for search, clustering and recommendation. Sentiment classification must be assessed with metrics such as precision, recall and F1, especially when classes are imbalanced.

Recommender systems may use item content, collaborative behaviour or hybrid designs. Latent-factor models represent user and item characteristics, but cold-start, popularity bias and feedback loops require attention. Neural networks learn weighted transformations through gradient-based optimization, and transformers use attention to model relationships in sequences.

LLMs can summarize, classify, retrieve and generate content, but they can hallucinate, expose information or produce biased outputs. Retrieval-augmented generation improves access to organizational evidence without guaranteeing correctness. Effective business deployment combines representative evaluation, source grounding, security, governance, monitoring and meaningful human oversight.

Key Terms

Token: A word or word fragment processed by a language model.

Tokenisation: The process of dividing text into model-readable units.

N-gram: A consecutive sequence of n tokens.

Lemmatization: Mapping a word to its linguistically based root form.

TF–IDF: A term-weighting method emphasizing distinctive document vocabulary.

Embedding: A numerical vector representation of an item or text.

Cosine similarity: Similarity based on the angle between two non-zero vectors.

Sentiment analysis: Estimation of expressed attitude or polarity in text.

Precision: The proportion of predicted positives that are correct.

Recall: The proportion of actual positives that are detected.

F1 score: The harmonic mean of precision and recall.

Collaborative filtering: Recommendation based on patterns across users and items.

Content-based recommendation: Recommendation using item attributes and user history.

Cold start: Difficulty recommending for new users or items with little data.

Embedding model: A model that maps inputs into numerical vector space.

Transformer: A neural architecture based substantially on attention mechanisms.

Large language model: A large neural language model trained to predict and generate tokens.

Retrieval-augmented generation: Generation supported by retrieved external context.

Hallucination: An unsupported or fabricated model output presented as true.

Human oversight: Authorized human review and control of an AI-supported process.

Review Questions

Short Answer Questions

  1. Why might stop-word removal be harmful in sentiment analysis?
  2. Distinguish TF–IDF from an embedding representation.
  3. Define precision and recall in a binary classifier.
  4. What is the cold-start problem in recommendation?
  5. Why does RAG not eliminate hallucination?

Long Answer Questions

  1. Explain the stages of a responsible business text-analytics project, from problem definition through monitoring.
  2. Compare content-based, collaborative and hybrid recommender systems, including their principal strengths and weaknesses.
  3. Explain how a basic neural network learns using loss, gradients and parameter updates.
  4. Describe a transformer-based LLM and distinguish prompting, RAG and fine-tuning.
  5. Develop an evaluation and governance plan for an LLM customer-service application.

Analytical / Application Questions

  1. A fraud classifier detects 90 fraudulent transactions and incorrectly flags 30 legitimate transactions. It misses 10 frauds, while 870 legitimate transactions are correctly cleared. Calculate precision, recall and F1, and explain which metric might matter most if investigations are expensive.
  2. Design a recommender-system experiment for an online retailer. Specify the recommendation method, offline metrics, online metrics, guardrails and cold-start strategy.
  3. An LLM answers internal policy questions using documents that are six months old. Analyse the risks and propose a retrieval, evaluation, access-control and human-escalation workflow.

Multiple Choice Questions with Answers

Question 1

Which representation gives greater weight to a term that is frequent in one document but uncommon across the collection?

A. One-hot encoding only

B. TF–IDF

C. Random sampling

D. A confusion matrix

Correct Answer: B. TF–IDF

Question 2

If a classifier produces 80 true positives and 20 false positives, what is its precision?

A. 20%

B. 50%

C. 80%

D. 100%

Correct Answer: C. 80%

Question 3

Which situation best illustrates the cold-start problem?

A. A popular item receives many recommendations

B. A new item has no interaction history

C. A document contains two bigrams

D. A model has too many evaluation metrics

Correct Answer: B. A new item has no interaction history

Question 4

What does cosine similarity primarily compare?

A. The angle between two non-zero vectors

B. The number of labels in a dataset

C. The age of two documents

D. The number of model layers

Correct Answer: A. The angle between two non-zero vectors

Question 5

What is the principal purpose of retrieval-augmented generation?

A. To remove all need for data governance

B. To supply retrieved external context to generation

C. To guarantee that every generated claim is true

D. To replace embeddings with ratings

Correct Answer: B. To supply retrieved external context to generation

Question 6

In the gradient-descent update, a positive learning rate controls what?

A. The number of classes

B. The size of the parameter update

C. The number of documents

D. The confusion-matrix layout

Correct Answer: B. The size of the parameter update

Question 7

Which metric is the harmonic mean of precision and recall?

A. Accuracy

B. F1 score

C. Specificity only

D. Mean reciprocal rank

Correct Answer: B. F1 score

Question 8

Which is a major risk of optimizing a recommender only for clicks?

A. It necessarily solves cold start

B. It may promote low-quality or sensational content

C. It prevents all popularity bias

D. It makes evaluation unnecessary

Correct Answer: B. It may promote low-quality or sensational content

Question 9

Why are citations useful in an enterprise LLM application?

A. They make the model’s tokeniser smaller

B. They help users trace claims to supporting sources

C. They guarantee the sources are current

D. They eliminate access-control requirements

Correct Answer: B. They help users trace claims to supporting sources

Question 10

Which practice represents meaningful human oversight?

A. Allowing the model unrestricted transaction authority

B. Accepting every confident output automatically

C. Giving trained reviewers authority to reject and escalate outputs

D. Removing logs to protect model performance

Correct Answer: C. Giving trained reviewers authority to reject and escalate outputs

Machine Learning for Business
Chapter 11 of 15