Spaces:
Runtime error
Runtime error
fixed bug with analyzing sentiment
Browse files- .idea/misc.xml +1 -1
- .idea/politweet.iml +1 -1
- textclassifier/TextClassifier.py +7 -8
.idea/misc.xml
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
<project version="4">
|
3 |
-
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.
|
4 |
</project>
|
|
|
1 |
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
<project version="4">
|
3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (politweet)" project-jdk-type="Python SDK" />
|
4 |
</project>
|
.idea/politweet.iml
CHANGED
@@ -5,7 +5,7 @@
|
|
5 |
<excludeFolder url="file://$MODULE_DIR$/politweet-environment" />
|
6 |
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
7 |
</content>
|
8 |
-
<orderEntry type="
|
9 |
<orderEntry type="sourceFolder" forTests="false" />
|
10 |
</component>
|
11 |
<component name="PyNamespacePackagesService">
|
|
|
5 |
<excludeFolder url="file://$MODULE_DIR$/politweet-environment" />
|
6 |
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
7 |
</content>
|
8 |
+
<orderEntry type="inheritedJdk" />
|
9 |
<orderEntry type="sourceFolder" forTests="false" />
|
10 |
</component>
|
11 |
<component name="PyNamespacePackagesService">
|
textclassifier/TextClassifier.py
CHANGED
@@ -82,7 +82,7 @@ class TextClassifier:
|
|
82 |
Classifies the sentiment of a user's tweets.
|
83 |
:param user_name: string of the user name.
|
84 |
"""
|
85 |
-
df_sentiment = self.df
|
86 |
df_sentiment['sentiment'] = df_sentiment['tweet'].apply(self.classify_sentiment)
|
87 |
self.df = df_sentiment
|
88 |
return self.df
|
@@ -95,7 +95,7 @@ class TextClassifier:
|
|
95 |
:param sentiment:
|
96 |
:return:
|
97 |
"""
|
98 |
-
assert 1 == 2, "Måste fixa prompt innan denna metod körs"
|
99 |
prompt_string = "Who is the TARGET of this "
|
100 |
prompt_string += sentiment
|
101 |
prompt_string += " TWEET?\\nTWEET=\""
|
@@ -113,7 +113,6 @@ class TextClassifier:
|
|
113 |
)
|
114 |
|
115 |
analyzed_sentiment = response.choices[0]['text']
|
116 |
-
print(analyzed_sentiment)
|
117 |
# Remove spaces at the start/end of the response
|
118 |
if analyzed_sentiment.startswith(' '):
|
119 |
analyzed_sentiment = analyzed_sentiment[1:]
|
@@ -163,8 +162,8 @@ class TextClassifier:
|
|
163 |
assert 'sentiment' in self.df.columns, \
|
164 |
"'sentiment' column does not exist. Please run classify_sentiment_of_tweets first."
|
165 |
|
166 |
-
df_analyze = self.df
|
167 |
-
df_analyze['target'] = df_analyze['tweet'].apply(self.analyze_sentiment, args=(df_analyze['sentiment']))
|
168 |
self.df = df_analyze
|
169 |
return self.df
|
170 |
|
@@ -185,12 +184,12 @@ if __name__ == "__main__":
|
|
185 |
warnings.simplefilter(action='ignore', category=FutureWarning)
|
186 |
pd.set_option('display.max_columns', None)
|
187 |
tc = TextClassifier(model_name="text-davinci-002", from_date='2022-07-01',
|
188 |
-
to_date=str(date.today()), user_name='jimmieakesson', num_tweets=
|
189 |
print(tc)
|
190 |
# df = tc.classify_sentiment_of_tweets("jimmieakesson")
|
191 |
# print(df)
|
192 |
df = tc.classify_sentiment_of_tweets()
|
193 |
print(df.head())
|
194 |
-
|
195 |
|
196 |
-
print(df)
|
|
|
82 |
Classifies the sentiment of a user's tweets.
|
83 |
:param user_name: string of the user name.
|
84 |
"""
|
85 |
+
df_sentiment = self.df.copy()
|
86 |
df_sentiment['sentiment'] = df_sentiment['tweet'].apply(self.classify_sentiment)
|
87 |
self.df = df_sentiment
|
88 |
return self.df
|
|
|
95 |
:param sentiment:
|
96 |
:return:
|
97 |
"""
|
98 |
+
# assert 1 == 2, "Måste fixa prompt innan denna metod körs"
|
99 |
prompt_string = "Who is the TARGET of this "
|
100 |
prompt_string += sentiment
|
101 |
prompt_string += " TWEET?\\nTWEET=\""
|
|
|
113 |
)
|
114 |
|
115 |
analyzed_sentiment = response.choices[0]['text']
|
|
|
116 |
# Remove spaces at the start/end of the response
|
117 |
if analyzed_sentiment.startswith(' '):
|
118 |
analyzed_sentiment = analyzed_sentiment[1:]
|
|
|
162 |
assert 'sentiment' in self.df.columns, \
|
163 |
"'sentiment' column does not exist. Please run classify_sentiment_of_tweets first."
|
164 |
|
165 |
+
df_analyze = self.df.copy()
|
166 |
+
df_analyze['target'] = df_analyze['tweet'].apply(self.analyze_sentiment, args=(df_analyze['sentiment'].values))
|
167 |
self.df = df_analyze
|
168 |
return self.df
|
169 |
|
|
|
184 |
warnings.simplefilter(action='ignore', category=FutureWarning)
|
185 |
pd.set_option('display.max_columns', None)
|
186 |
tc = TextClassifier(model_name="text-davinci-002", from_date='2022-07-01',
|
187 |
+
to_date=str(date.today()), user_name='jimmieakesson', num_tweets=15)
|
188 |
print(tc)
|
189 |
# df = tc.classify_sentiment_of_tweets("jimmieakesson")
|
190 |
# print(df)
|
191 |
df = tc.classify_sentiment_of_tweets()
|
192 |
print(df.head())
|
193 |
+
df = tc.analyze_sentiment_of_tweets()
|
194 |
|
195 |
+
print(df.head())
|