-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from rabinadk1/word_cloud
Word cloud for Backend
- Loading branch information
Showing
11 changed files
with
644 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const WordCloud = () => { | ||
return <div>WordCloud</div>; | ||
}; | ||
|
||
export default WordCloud; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from fastapi import APIRouter | ||
|
||
router = APIRouter(prefix="/tweets_commons", tags=["tweets_commons"]) | ||
|
||
# get all the stopwords | ||
with open("stopwords.txt") as fp: | ||
STOP_WORDS = set(fp.read().splitlines()) | ||
|
||
# Import all routes | ||
from . import routes # noqa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from datetime import date | ||
from typing import List, Optional | ||
|
||
from fastapi import Depends, Query | ||
from nltk import FreqDist | ||
from sqlmodel import Session, select, union_all | ||
|
||
from ..database import get_session | ||
from . import router | ||
from .helper_functions import get_filtered_selection | ||
from .models import PseudoTweet, Topics, Tweet | ||
from .types import Month | ||
from .word_cloud_helper import word_tokenize_nepali | ||
|
||
|
||
@router.get("/") | ||
def get_word_cloud( | ||
topics: Optional[List[Topics]] = Query(None), | ||
day: Optional[date] = None, | ||
month: Optional[Month] = None, | ||
session: Session = Depends(get_session), | ||
): | ||
""" | ||
Get the word-count distribution within the given time range | ||
""" | ||
fields = ("text",) | ||
|
||
tweet_selection = get_filtered_selection(topics, Tweet, day, month, fields) | ||
pseudo_tweet_selection = get_filtered_selection( | ||
topics, PseudoTweet, day, month, fields | ||
) | ||
|
||
combined_model = union_all(tweet_selection, pseudo_tweet_selection).subquery().c | ||
|
||
# Manually selected the text here, need to change if needed | ||
combined_tweets = session.exec(select(combined_model.text)).all() | ||
|
||
# It is a generator of tuples | ||
two_dimensional_tokens = map(word_tokenize_nepali, combined_tweets) | ||
|
||
flat_tokens: List[str] = [] | ||
|
||
for token in two_dimensional_tokens: | ||
flat_tokens.extend(token) | ||
|
||
word_freq = FreqDist(flat_tokens) | ||
|
||
return word_freq.most_common(100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import re | ||
from typing import AnyStr | ||
|
||
from nltk.tokenize import word_tokenize | ||
|
||
from . import STOP_WORDS | ||
|
||
emoj_regex = re.compile( | ||
"[" | ||
"\U0001F600-\U0001F64F" # emoticons | ||
"\U0001F300-\U0001F5FF" # symbols & pictographs | ||
"\U0001F680-\U0001F6FF" # transport & map symbols | ||
"\U0001F1E0-\U0001F1FF" # flags (iOS) | ||
"\U00002500-\U00002BEF" # chinese char | ||
"\U00002702-\U000027B0" | ||
"\U00002702-\U000027B0" | ||
"\U000024C2-\U0001F251" | ||
"\U0001f926-\U0001f937" | ||
"\U00010000-\U0010ffff" | ||
"\u2640-\u2642" | ||
"\u2600-\u2B55" | ||
"\u200d" | ||
"\u23cf" | ||
"\u23e9" | ||
"\u231a" | ||
"\ufe0f" # dingbats | ||
"\u3030" | ||
"]+", | ||
re.UNICODE, | ||
) | ||
|
||
|
||
def remove_emojis(data: AnyStr) -> AnyStr: | ||
return re.sub(emoj_regex, "", data) | ||
|
||
|
||
def word_tokenize_nepali(text: str): | ||
text = remove_emojis(text) | ||
text = re.sub(r"\d+", " ", text) # remove any digits | ||
text = re.sub(r"[,)({}[\]\.:;`_–\-``!‘’''“”?\-।/—%\|]+", " ", text) | ||
text = re.sub( | ||
r"\s+", " ", text | ||
) # replace multiple whitespaces with single whitespace | ||
text = text.replace("#", "").replace( | ||
"_", " " | ||
) # remove #, and break words containing underscore | ||
return tuple(token for token in word_tokenize(text) if token not in STOP_WORDS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.