Total Pageviews

Monday, August 31, 2026

Use Natural Language Processing features and an Artificial Neural Network to determine whether a review is Positive or Negative.

🧠 ANN + NLP Example: Sentiment Analysis

Use Natural Language Processing features and an Artificial Neural Network to determine whether a review is Positive or Negative.

1. What is NLP?

Natural Language Processing (NLP) is a branch of Artificial Intelligence that enables computers to process and understand human language.

In sentiment analysis, NLP converts text into numerical features that a machine-learning model can process.

Text Tokenization Features ANN Sentiment

2. Example Customer Review

Review:

"The movie was excellent and amazing. I really enjoyed it."

The NLP system analyzes the sentence and converts the text into numerical features.

3. Convert Text into Numerical Features

NLP Feature Value Meaning
Positive Word Score 0.90 Strong positive words detected
Negative Word Score 0.10 Small negative contribution
Review Length Score 0.50 Normalized text length
Important:

NLP can use many different representations, such as Bag-of-Words, TF-IDF, word embeddings, or contextual embeddings. For this simple classroom example, we use three manually defined numerical features so the ANN mathematics is easy to follow.

4. ANN Architecture for Sentiment Analysis

NLP INPUT FEATURES
Positive Score
X₁ = 0.90
Negative Score
X₂ = 0.10
Length Score
X₃ = 0.50
OUTPUT
Positive
Probability

The text is first converted into numerical features. These features are then processed by the ANN.

5. Step-by-Step ANN Mathematics

Click the buttons below to see how the review is classified.

1 NLP Converts Text into Numbers

The review:

"The movie was excellent and amazing. I really enjoyed it."

For our simplified example, NLP produces:

X₁ = Positive Word Score = 0.90
X₂ = Negative Word Score = 0.10
X₃ = Review Length Score = 0.50

These numerical values become the inputs to the ANN.

2 Assign Weights and Bias

Suppose the hidden neuron H₁ has learned:

W₁ = 0.60
W₂ = -0.30
W₃ = 0.20
Bias = 0.10

The negative weight for the negative-score feature means that, in this simplified model, a higher negative score reduces the neuron's activation.

3 Calculate the Weighted Sum

The neuron uses:

Z = X₁W₁ + X₂W₂ + X₃W₃ + b

Substitute the values:

Z = (0.90 × 0.60) + (0.10 × -0.30) + (0.50 × 0.20) + 0.10

Calculate each term:

0.90 × 0.60 = 0.540
0.10 × -0.30 = -0.030
0.50 × 0.20 = 0.100

Now add them:

Z = 0.540 - 0.030 + 0.100 + 0.100

Z = 0.710

Therefore: Weighted Sum = 0.710.

4 Apply Sigmoid Activation

We use the sigmoid activation function:

Sigmoid(z) = 1 / (1 + e-z)

Since:

Z = 0.710

Substitute:

H₁ = 1 / (1 + e-0.710)

Approximately:

e-0.710 ≈ 0.492

H₁ ≈ 1 / (1 + 0.492)

H₁ ≈ 0.670

Therefore: H₁ ≈ 0.670.

5 Calculate the Output

Suppose the output neuron has:

Output Weight = 1.50
Output Bias = -0.40

Calculate the output weighted sum:

Zout = (H₁ × Output Weight) + Output Bias

Substitute:

Zout = (0.670 × 1.50) - 0.40

Calculate:

0.670 × 1.50 = 1.005

Zout = 1.005 - 0.40

Zout = 0.605

Apply sigmoid to obtain the positive sentiment probability:

P(Positive) = 1 / (1 + e-0.605)

P(Positive) ≈ 0.647
Positive Sentiment Probability ≈ 64.7%

6 Make the Final Sentiment Prediction

Suppose the classification threshold is:

Threshold = 50%

The ANN predicted:

Positive Probability = 64.7%

Compare:

64.7% ≥ 50%
😊 PREDICTION: POSITIVE SENTIMENT

Therefore, the ANN classifies the review as Positive.

6. Complete NLP → ANN Flow

TEXT

"The movie was excellent and amazing..."


NLP FEATURE EXTRACTION
Positive Score = 0.90
Negative Score = 0.10
Length Score = 0.50


ANN INPUT
X₁ = 0.90
X₂ = 0.10
X₃ = 0.50


WEIGHTED SUM
Z = (0.90 × 0.60) + (0.10 × -0.30) + (0.50 × 0.20) + 0.10
Z = 0.710


SIGMOID
H₁ ≈ 0.670


OUTPUT
Zout = (0.670 × 1.50) - 0.40
Zout = 0.605


FINAL SIGMOID
P(Positive) ≈ 0.647


64.7% → POSITIVE SENTIMENT 😊

7. Important NLP + ANN Terms

NLP Text Tokenization Feature Extraction Positive Score Negative Score Neuron Weight Bias Weighted Sum Activation Function Sigmoid Sentiment Analysis Classification

Key idea: NLP converts human language into numerical representations. The ANN then processes those numerical inputs and produces a prediction.

No comments:

Post a Comment