PerSent 1.3.1__tar.gz → 1.3.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PerSent
3
- Version: 1.3.1
3
+ Version: 1.3.2
4
4
  Summary: Persian Sentiment Analysis Toolkit
5
5
  Home-page: https://github.com/RezaGooner/PerSent
6
6
  Author: RezaGooner
@@ -9,6 +9,8 @@ from sklearn.linear_model import LogisticRegression
9
9
  from sklearn.model_selection import train_test_split
10
10
  import os
11
11
  import joblib
12
+ import importlib.resources
13
+
12
14
 
13
15
  class CommentAnalyzer:
14
16
  def __init__(self, model_dir='PerSent/model'):
@@ -140,21 +142,35 @@ class CommentAnalyzer:
140
142
  except Exception as e:
141
143
  raise Exception(f"Error while saving model: {str(e)}")
142
144
 
145
+
143
146
  def loadModel(self):
144
- """reload from file"""
147
+ """reload from package asset or model_dir"""
145
148
  try:
146
- classifier_path = os.path.join(self.model_dir, 'classifier.joblib')
147
- vectorizer_path = os.path.join(self.model_dir, 'word2vec.model')
149
+ classifier_filename = 'classifier.joblib'
150
+ vectorizer_filename = 'word2vec.model'
151
+
152
+ classifier_path = os.path.join(self.model_dir, classifier_filename)
153
+ vectorizer_path = os.path.join(self.model_dir, vectorizer_filename)
154
+
155
+ if os.path.exists(classifier_path) and os.path.exists(vectorizer_path):
156
+ self.classifier = joblib.load(classifier_path)
157
+ self.vectorizer = Word2Vec.load(vectorizer_path)
158
+ return
148
159
 
149
- if not os.path.exists(classifier_path) or not os.path.exists(vectorizer_path):
150
- raise FileNotFoundError("Model files not found in the specified directory")
151
-
152
- self.classifier = joblib.load(classifier_path)
153
- self.vectorizer = Word2Vec.load(vectorizer_path)
154
- except FileNotFoundError as e:
155
- raise FileNotFoundError(f"Model files not found: {str(e)}")
160
+ try:
161
+ with importlib.resources.path("PerSent.models", classifier_filename) as clf_p, \
162
+ importlib.resources.path("PerSent.models", vectorizer_filename) as vec_p:
163
+ self.classifier = joblib.load(clf_p)
164
+ self.vectorizer = Word2Vec.load(str(vec_p))
165
+ except FileNotFoundError as e:
166
+ raise FileNotFoundError("Model files not found in model_dir *and* package asset")
167
+ except Exception as e:
168
+ raise Exception(f"Failed to load model from package: {str(e)}")
169
+
156
170
  except Exception as e:
157
171
  raise Exception(f"Failed to load model: {str(e)}")
172
+
173
+
158
174
 
159
175
  def analyzeCSV(self, input_csv, output_path, summary_path=None, text_column=0):
160
176
  """
@@ -11,6 +11,7 @@ from collections import defaultdict
11
11
  from hazm import Stemmer, Lemmatizer, Normalizer, word_tokenize, stopwords_list
12
12
  from tqdm import tqdm
13
13
  import pandas as pd
14
+ import importlib.resources
14
15
 
15
16
  class SentimentAnalyzer:
16
17
  """A Persian sentiment analyzer that supports word weighting"""
@@ -239,20 +240,15 @@ class SentimentAnalyzer:
239
240
  """
240
241
  try:
241
242
  model_path = os.path.join(self.model_dir, f'{model_name}.joblib')
242
-
243
- if not os.path.exists(model_path):
244
- raise FileNotFoundError(f"Model file {model_path} not found")
245
-
246
- data = joblib.load(model_path)
243
+ if os.path.exists(model_path):
244
+ data = joblib.load(model_path)
245
+ else:
246
+ with importlib.resources.path("PerSent.models", f'{model_name}.joblib') as pkg_model_path:
247
+ data = joblib.load(pkg_model_path)
247
248
  self.emotion_map = data['emotion_map']
248
249
  self.keywords = defaultdict(list, data['keywords'])
249
250
  self.word_weights = defaultdict(dict, data['word_weights'])
250
251
  self.model_loaded = True
251
-
252
- except FileNotFoundError as e:
253
- raise FileNotFoundError(f"Model file not found: {str(e)}")
254
- except (KeyError, AttributeError) as e:
255
- raise ValueError(f"Invalid or corrupted model file: {str(e)}")
256
252
  except Exception as e:
257
253
  raise Exception(f"Failed to load model: {str(e)}")
258
254
 
@@ -3,8 +3,8 @@ from .SentimentAnalyzer import SentimentAnalyzer
3
3
  from pathlib import Path
4
4
  import os
5
5
 
6
- MODEL_DIR = Path(__file__).parent / "model"
6
+ MODEL_DIR = Path(__file__).parent / "models"
7
7
  os.makedirs(MODEL_DIR, exist_ok=True)
8
8
 
9
- __version__ = "1.3.1"
9
+ __version__ = "1.3.2"
10
10
  __all__ = ['CommentAnalyzer', 'SentimentAnalyzer', 'MODEL_DIR']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: PerSent
3
- Version: 1.3.1
3
+ Version: 1.3.2
4
4
  Summary: Persian Sentiment Analysis Toolkit
5
5
  Home-page: https://github.com/RezaGooner/PerSent
6
6
  Author: RezaGooner
@@ -10,6 +10,6 @@ PerSent.egg-info/SOURCES.txt
10
10
  PerSent.egg-info/dependency_links.txt
11
11
  PerSent.egg-info/requires.txt
12
12
  PerSent.egg-info/top_level.txt
13
- PerSent/model/classifier.joblib
14
- PerSent/model/weighted_sentiment_model.joblib
15
- PerSent/model/word2vec.model
13
+ PerSent/models/classifier.joblib
14
+ PerSent/models/weighted_sentiment_model.joblib
15
+ PerSent/models/word2vec.model
@@ -5,7 +5,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
5
5
 
6
6
  setup(
7
7
  name="PerSent",
8
- version="1.3.1",
8
+ version="1.3.2",
9
9
  author="RezaGooner",
10
10
  author_email="RezaAsadiProgrammer@Gmail.com",
11
11
  description="Persian Sentiment Analysis Toolkit",
@@ -14,7 +14,7 @@ setup(
14
14
  url="https://github.com/RezaGooner/PerSent",
15
15
  packages=find_packages(),
16
16
  package_data={
17
- 'PerSent': ['CommentAnalyzer.py','SentimentAnalyzer.py' , 'model/*'],
17
+ 'PerSent': ['CommentAnalyzer.py','SentimentAnalyzer.py' , 'models/*'],
18
18
  },
19
19
  include_package_data=True,
20
20
  install_requires=[
File without changes
File without changes
File without changes