PerSent 0.2.2__tar.gz → 0.2.4__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.
Potentially problematic release.
This version of PerSent might be problematic. Click here for more details.
- {persent-0.2.2 → persent-0.2.4}/PKG-INFO +1 -1
- {persent-0.2.2 → persent-0.2.4}/PerSent/CommentAnalyzer.py +57 -9
- {persent-0.2.2 → persent-0.2.4}/PerSent/__init__.py +1 -1
- {persent-0.2.2 → persent-0.2.4}/PerSent.egg-info/PKG-INFO +1 -1
- {persent-0.2.2 → persent-0.2.4}/setup.py +1 -1
- {persent-0.2.2 → persent-0.2.4}/PerSent.egg-info/SOURCES.txt +0 -0
- {persent-0.2.2 → persent-0.2.4}/PerSent.egg-info/dependency_links.txt +0 -0
- {persent-0.2.2 → persent-0.2.4}/PerSent.egg-info/requires.txt +0 -0
- {persent-0.2.2 → persent-0.2.4}/PerSent.egg-info/top_level.txt +0 -0
- {persent-0.2.2 → persent-0.2.4}/README.md +0 -0
- {persent-0.2.2 → persent-0.2.4}/setup.cfg +0 -0
|
@@ -112,40 +112,88 @@ class CommentAnalyzer:
|
|
|
112
112
|
self.classifier = joblib.load(os.path.join(self.model_dir, 'classifier.joblib'))
|
|
113
113
|
self.vectorizer = Word2Vec.load(os.path.join(self.model_dir, 'word2vec.model'))
|
|
114
114
|
|
|
115
|
-
def csvPredict(self, input_csv, output_path, text_column=
|
|
115
|
+
def csvPredict(self, input_csv, output_path, summary_path=None, text_column=0):
|
|
116
116
|
"""
|
|
117
117
|
Analyze sentiment for comments in a CSV file and save results
|
|
118
118
|
|
|
119
119
|
Parameters:
|
|
120
120
|
input_csv (str): Path to input CSV file
|
|
121
121
|
output_path (str): Path to save output CSV file
|
|
122
|
-
text_column (str, optional): Name of column containing comments.
|
|
122
|
+
text_column (str/int, optional): Name or index (0-based) of column containing comments.
|
|
123
|
+
Defaults to 0 (first column).
|
|
124
|
+
summary_path (str, optional): Path to save prediction summary report.
|
|
125
|
+
If None, no summary will be saved.
|
|
123
126
|
"""
|
|
124
127
|
try:
|
|
125
128
|
# Read input CSV
|
|
126
129
|
df = pd.read_csv(input_csv)
|
|
127
130
|
|
|
128
|
-
#
|
|
129
|
-
if text_column
|
|
130
|
-
#
|
|
131
|
-
if
|
|
132
|
-
text_column = df.columns
|
|
133
|
-
|
|
131
|
+
# Determine the correct column
|
|
132
|
+
if isinstance(text_column, int):
|
|
133
|
+
# Handle negative indices
|
|
134
|
+
if text_column < 0:
|
|
135
|
+
text_column = len(df.columns) + text_column
|
|
136
|
+
|
|
137
|
+
if text_column >= len(df.columns) or text_column < 0:
|
|
138
|
+
raise ValueError(f"Column index {text_column} is out of range")
|
|
139
|
+
|
|
140
|
+
column_name = df.columns[text_column]
|
|
141
|
+
else:
|
|
142
|
+
if text_column not in df.columns:
|
|
134
143
|
raise ValueError(f"Column '{text_column}' not found in CSV file")
|
|
144
|
+
column_name = text_column
|
|
135
145
|
|
|
136
146
|
# Analyze each comment
|
|
137
147
|
tqdm.pandas(desc="Analyzing comments")
|
|
138
|
-
df['sentiment'] = df[
|
|
148
|
+
df['sentiment'] = df[column_name].progress_apply(self.predict)
|
|
139
149
|
|
|
140
150
|
# Save results
|
|
141
151
|
df.to_csv(output_path, index=False, encoding='utf-8-sig')
|
|
142
152
|
print(f"Results saved to {output_path}")
|
|
143
153
|
|
|
154
|
+
# Generate and save summary if requested
|
|
155
|
+
if summary_path:
|
|
156
|
+
summary = self._generate_summary(df)
|
|
157
|
+
summary.to_csv(summary_path, index=False, encoding='utf-8-sig')
|
|
158
|
+
print(f"Summary report saved to {summary_path}")
|
|
159
|
+
|
|
144
160
|
return df
|
|
145
161
|
|
|
146
162
|
except Exception as e:
|
|
147
163
|
print(f"Error: {str(e)}")
|
|
148
164
|
return None
|
|
149
165
|
|
|
166
|
+
def _generate_summary(self, df):
|
|
167
|
+
"""Generate prediction summary statistics"""
|
|
168
|
+
# Count each sentiment
|
|
169
|
+
counts = df['sentiment'].value_counts().to_dict()
|
|
170
|
+
|
|
171
|
+
# Create summary dataframe
|
|
172
|
+
summary = pd.DataFrame({
|
|
173
|
+
'Category': [
|
|
174
|
+
'Recommended',
|
|
175
|
+
'Not Recommended',
|
|
176
|
+
'No Idea',
|
|
177
|
+
'Total',
|
|
178
|
+
'Model Accuracy'
|
|
179
|
+
],
|
|
180
|
+
'Count': [
|
|
181
|
+
counts.get('recommended', 0),
|
|
182
|
+
counts.get('not_recommended', 0),
|
|
183
|
+
counts.get('no_idea', 0),
|
|
184
|
+
len(df),
|
|
185
|
+
'N/A' # Accuracy needs to be calculated during training
|
|
186
|
+
],
|
|
187
|
+
'Percentage': [
|
|
188
|
+
f"{100 * counts.get('recommended', 0) / len(df):.2f}%",
|
|
189
|
+
f"{100 * counts.get('not_recommended', 0) / len(df):.2f}%",
|
|
190
|
+
f"{100 * counts.get('no_idea', 0) / len(df):.2f}%",
|
|
191
|
+
'100%',
|
|
192
|
+
'N/A'
|
|
193
|
+
]
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
return summary
|
|
197
|
+
|
|
150
198
|
|
|
151
199
|
# Github : RezaGooner
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|