PerSent (Persian Sentiment Analyzer)
Introduction
PerSent is a practical Python library designed for Persian sentiment analysis. The name stands for "Persian Sentiment Analyzer". Currently in its early testing phase, the library provides basic functionality and is available on PyPI. Install it using:
pip install PerSent
Current capabilities include:
-
Sentiment analysis of opinions/comments
-
Emotion analysis of texts (happiness, sadness, anger, surprise, fear, disgust, calmness)
-
Analysis of product/service reviews (recommended/not recommended/no idea)
-
Both single-text and batch CSV processing
-
Output displayed in terminal or saved to CSV with summary statistics
Initial repository that evolved into this library: Click here
We welcome user testing and feedback to improve the library. If you encounter bugs or have suggestions, please: Contribute
For installation issues due to dependency conflicts (especially with mingw-w64), consider using online platforms like DeepNote.com.
Structure
Comment Analysis Functions
$$train(train_csv, test_size=0.2, vector_size=100, window=5)$$
Trains the model using a CSV file with columns:
1- body (text content)
2- recommendation_status (must be one of:)
-
no_idea
-
recommended
-
not_recommended
Null/NaN values are converted to no_idea, affecting model accuracy. Optional parameters:
-
test_size: Test data proportion (default 0.2)
-
vector_size: Word vector dimensions (default 100)
-
window: Context window size (default 5)
Returns test accuracy score.
$$predict(text)$$
The core function that analyzes a text and returns one of: "not_recommended", "recommended", or "no_idea".
$$save_model()$$
$$load_model()$$
Model persistence functions. Models are saved in the model directory.
$$csvPredict(input_csv, output_path, summary_path=None, text_column=0)$$
Batch processes comments from a CSV file. For single-column files, text_column isn't needed. Otherwise specify column name/index (0-based, negative indices supported). Output contains:
1- Original text
2- Recommendation status Optional summary_path generates statistics:
-
Total count
-
Recommended count
-
Not recommended count
-
No idea count
-
Model accuracy (not implemented in current version)
Returns a DataFrame and saves results.
Emotion Analysis Functions
$$load_weighted_lexicon(csv_file, word_col=0, emotion_col=1, weight_col=2)$$
Loads a CSV with three columns:
1- Keywords
2- Emotion (happiness, sadness, anger, fear, disgust, calmness)
3- Emotion weight (defaults to 1 if unspecified, affecting accuracy)
Column indices are optional.
$$train_from_csv(train_csv, text_col='text', emotion_col='sentiment', weight_col='weight')$$
Trains the emotion model using a CSV with specified column names (optional).
$$save_model(model_name='weighted_sentiment_model')$$
$$load_model(model_name='weighted_sentiment_model')$$
Model persistence functions (saved in model directory).
$$analyze_text(text)$$
Analyzes a single text, returning percentage scores for each emotion.
$$analyze_csv(input_csv, output_csv, text_col='text', output_col='sentiment_analysis')$$
Batch processes texts from CSV. Returns True on success. Requires:
-
input_csv path
-
output_csv path Optional column names.
Installation
Install via pip:
pip install PerSent
For specific versions:
pip install PerSent==<VERSION_NUMBER>
Usage
- Comment Analysis
Basic single-text analysis:
from PerSent import CommentAnalyzer
analyzer = CommentAnalyzer()
'''
Training (if you have data):
Requires CSV with comments and recommendation status columns
Status must be: recommended/not_recommended/no_idea
'''
analyzer.train("train.csv")
# Load pre-trained model
analyzer.load_model()
# Predict
text = "کیفیت عالی داشت" # "Excellent quality"
result = analyzer.predict(text)
print(f"Sentiment: {result}") # Output: Sentiment: recommended
The included pre-trained model has ~70% accuracy. For better results, you can train with larger datasets. I've prepared a split dataset (due to size):
Batch CSV processing:
from PerSent import CommentAnalyzer
analyzer = CommentAnalyzer()
analyzer.load_model()
# Basic usage (single-column CSV)
analyzer.csvPredict(
input_csv="comments.csv",
output_path="results.csv"
)
# Alternative usage patterns:
# 1. Using column index (0-based)
analyzer.csvPredict("comments.csv", "results.csv", None, 0)
# 2. Negative indices (count from end)
analyzer.csvPredict("comments.csv", "results.csv", None, -1)
# 3. Column name
analyzer.csvPredict("comments.csv", "results.csv", None, "نظرات") # "Comments" column
# 4. With summary (single-column)
analyzer.csvPredict("comments.csv", "results.csv", "summary.csv")
# 5. With summary and column specification
analyzer.csvPredict("comments.csv", "results.csv", "summary.csv", 2)
- Emotion Analysis
Single text analysis with pre-trained model:
from PerSent import SentimentAnalyzer
analyzer = SentimentAnalyzer()
analyzer.load_model()
sample_text = "امتحانم رو خراب کردم. احساس میکنم یک شکست خوردهی تمام عیارم."
# "I failed my exam. I feel like a complete failure."
result = analyzer.analyze_text(sample_text)
for emotion, score in sorted(result.items(), key=lambda x: x[1], reverse=True):
print(f"{emotion}: {score:.2f}%")
output :
غم: 36.00% #Sadness
عصبانیت: 36.00% #anger
ترس: 28.00% #fear
شادی: 0.00% #happiness
تنفر: 0.00% #disgust
شگفتی: 0.00% #surprise
آرامش: 0.00% #calmness
To train your own model:
analyzer.train_from_csv('emotion_dataset.csv')
Required CSV columns:
1- Keywords
2- Emotion (happiness, sadness, anger, disgust, fear, calmness)
3- Emotion weight
Model persistence:
analyzer.save_model("custom_model_name")
analyzer.load_model("custom_model_name")
Batch CSV processing:
analyzer.analyze_csv("input.csv", "output.csv")
Contribution
As mentioned, this library needs community collaboration. Please share suggestions, bugs, or feedback via:
-
Email:
RezaAsadiProgrammer@gmail.com -
Telegram:
@RezaGooner
Metadata
Release files for PerSent 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| persent-1.1.0.tar.gz | 27.2 MB | Without provenance | Details for persent-1.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| persent-1.1.0-py3-none-any.whl | Python 3 | none | any | Details for persent-1.1.0-py3-none-any.whl |
Total release size: 54.4 MB
Release files / persent-1.1.0.tar.gz
| Download URL | persent-1.1.0.tar.gz |
|---|---|
| Size | 27.2 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a30489b264930bb106e2984f2eeb03f1fe5252fa1978ca4b590e051d597f11e3
|
|
BLAKE2b-256 checksum How to use checksums |
ff84b3a21cf8b96ac45a89775b559cd27958c2957274654dcded720628b3da07
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Release files / persent-1.1.0-py3-none-any.whl
| Download URL | persent-1.1.0-py3-none-any.whl |
|---|---|
| Size | 27.2 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ce778359cb1b70d973b3298e1f31b9d4b217a069f326cc2764b8cc118c403ef1
|
|
BLAKE2b-256 checksum How to use checksums |
12f585b13e816861067203f13c03668db77724e78918f2ea9558f96f171bd6b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.2
|
Security for PerSent 1.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.