rastof9 commited on
Commit
252acde
·
verified ·
1 Parent(s): abbf52b

Create utils/helpers.py

Browse files
Files changed (1) hide show
  1. app/utils/helpers.py +24 -0
app/utils/helpers.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from datetime import datetime
3
+
4
+ def format_date(date_str):
5
+ """Format a date string into a human-readable format."""
6
+ try:
7
+ date_obj = datetime.strptime(date_str, "%Y-%m-%d")
8
+ return date_obj.strftime("%b %d, %Y")
9
+ except ValueError:
10
+ return date_str
11
+
12
+ def extract_hashtags(text):
13
+ """Extract hashtags from a given text."""
14
+ return re.findall(r"#\w+", text)
15
+
16
+ def calculate_sentiment_score(sentiment_data):
17
+ """Calculate an overall sentiment score from sentiment data."""
18
+ if not sentiment_data:
19
+ return 0
20
+ return sum(sentiment_data.values()) / len(sentiment_data)
21
+
22
+ def paginate(query, page, per_page=10):
23
+ """Paginate a SQLAlchemy query."""
24
+ return query.paginate(page=page, per_page=per_page, error_out=False)