A sentiment analysis system implemented in Prolog, combining Natural Language Processing techniques with the SentiWordNet lexicon to analyze text sentiment.
Authors: Federico Chiaradia (f.chiaradia5@studenti.uniba.it), Yonas Babulet Abetew (y.babuletabetew@studenti.uniba.it)
Institution: Department of Computer Science, University of Bari
- Introduction
- Features
- System Architecture
- Prerequisites
- Installation
- Usage
- How It Works
- Project Structure
- Examples
- Limitations and Future Improvements
- License
Sentiment analysis, also known as opinion mining, is a computational technique that determines the sentiment or emotional tone expressed in text. This project leverages Prolog, a logic programming language, to implement a rule-based sentiment analysis system.
The system classifies input text into three categories:
- Positive (score > 0.3)
- Negative (score < -0.3)
- Neutral (otherwise)
The implementation combines NLP techniques with data from the SentiWordNet lexicon to compute sentiment scores based on user input sentences.
- Dual Interface: Access via GUI (JavaFX) or console (SWI-Prolog)
- Rule-Based System: Uses declarative Prolog rules for linguistic pattern matching
- SentiWordNet Integration: Leverages the SentiWordNet v3.0.0 lexicon for sentiment scoring
- Advanced NLP Pipeline: Includes stopword removal, intensifier detection, negation handling, and neighbor influence calculation
- Transparent Processing: Shows intermediate steps for educational purposes
- Error Handling: Provides helpful feedback for invalid inputs
The system consists of three main modules:
-
GUI (Main.java): JavaFX-based graphical interface for user interaction
-
Prolog Bridge (Prolog.java): Java-Prolog interface using JPL library
-
Sentiment Analyzer (Sent_Analysis.pl)
: Core Prolog logic with two sub-modules:
sent_modifiers.pl: Dictionary of indicators, intensifiers, and negationssent_wordnet.pl: SentiWordNet lexicon database
- SWI-Prolog: Required for running the Prolog engine
- Java: Required for the GUI version (JDK 8 or higher recommended)
- JavaFX SDK: Included with the launch files for the GUI version
- Clone this repository:
git clone https://github.com/yourusername/prolog-sentiment-analysis.git
cd prolog-sentiment-analysis- Ensure SWI-Prolog is installed on your system
- For the GUI version, verify that Java is installed:
java -version- Navigate to the
GUIfolder - Run the batch file:
PrologSentimentAnalysis.bat- The JavaFX interface will open
- Enter a sentence in the input field and click Submit
- View the analysis results in the modal window
Accessing Documentation: Click on the "Help" menu in the top-left corner to view usage instructions.
Error Handling: If invalid input is entered, an error modal will appear with helpful tips.
- Navigate to the
consolefolder - Open SWI-Prolog and consult the main file:
?- consult('Sent_Analysis.pl').Alternatively, directly open the file with SWI-Prolog.
Sent_Analysis.pl to update the path to cons_doc.txt:
- The main menu will launch automatically. To run manually:
?- main.- Choose an option:
- 1: Analyze a sentence
- 2: View documentation
- 3: Exit
Example Usage:
The system processes input text through the following stages:
- Input Sentence
- Text Preprocessing
- Splitting
- Stopword removal
- Lexical Analysis
- Apply intensifiers
- Calculate neighbor influence
- Retrieve scores from SentiWordNet
- Calculate Scores
- Filter scores
- Add weights
- Apply negations
- Final Score
The input sentence is tokenized into individual words and stored in a list.
Common stopwords (defined in the knowledge base) are removed:
stopwords('so').
stopwords('the').Each word is labeled based on its linguistic function:
- Indicator (
pos/neg): Words indicating positive or negative sentiment - Intensifier (
in-[score]): Words that amplify sentiment (e.g., "very", "extremely") - Negation (
n): Words that negate sentiment (e.g., "not", "never") - Neutral (
neut): Default label if no match is found
Labels are defined in sent_modifiers.pl.
Intensifier scores are merged with adjacent indicators. Non-adjacent intensifiers receive neutral weights.
Intensifier words are removed from the word list while preserving order.
For each word, the system computes influence from neighboring words using their labels. This produces a triple of scores (-1, 0, 1) representing negative, neutral, and positive influence.
Sentiment scores are retrieved from the SentiWordNet v3.0.0 lexicon, represented as:
synset(a, 0.125, 0.375, ['fearful']).
synset(n, 0.875, 0.0, ['honorableness', 'honorableness']).Where:
- First parameter: Part of speech (a=adjective, n=noun, etc.)
- Second parameter: Positive score
- Third parameter: Negative score
- Fourth parameter: List of synonyms
Based on neighbor influence:
- If influence is positive/negative (1 or -1): Use maximum positive/negative score
- If influence is neutral: Use average positive score minus average negative score
Multiply each score by its intensifier weight.
Words preceded by negations have their scores multiplied by -1.
Sum all word scores to get the final sentiment score:
negative if score < -0.3
positive if score > 0.3
neutral otherwise
prolog-sentiment-analysis/
βββ GUI/
β βββ PrologSentimentAnalysis.bat
β βββ Main.java
β βββ Prolog.java
β βββ jpl.jar
β βββ JavaFX SDK files
βββ console/
β βββ Sent_Analysis.pl
β βββ sent_modifiers.pl
β βββ sent_wordnet.pl
β βββ cons_doc.txt
βββ Documentation.pdf
βββ README.md
- "I like to eat apple"
- "he is a very bad person"
- "the reason of his good behaviour is that he is a good person"
- "i like to play abcdef" (contains unknown words)
- "we are not happy today" (contains unaccepted words)
- "me and you love to watch a movie" (grammatical issues)
- String Matching: Substring matching can cause false positives
- Case Sensitivity: All text is converted to lowercase for matching
- Limited Grammar Rules: Uses general linguistic patterns rather than comprehensive English grammar
- Non-exhaustive Dictionary: Limited coverage in
sent_modifiers.pl
- Stemming/Lemmatization: Reduce words to root forms for better matching
- Advanced Intensifier/Negation Handling: Apply these operations multiple times or to wider contexts
- Extended Neighbor Analysis: Consider more neighboring words with additional filters
- Configurable Thresholds: Allow users to adjust positivity/negativity score ranges
- Plural Handling: Better processing of plural forms
- Enhanced Grammar Rules: Incorporate more sophisticated linguistic patterns
This project was developed as part of coursework at the University of Bari, Department of Computer Science.
This is an academic project. If you have suggestions or improvements, feel free to open an issue or submit a pull request.
For questions or feedback:
- Federico Chiaradia: f.chiaradia5@studenti.uniba.it
- Yonas Babulet Abetew: y.babuletabetew@studenti.uniba.it
Department of Computer Science University of Bari










