def get_subjectivity_score_tr(user): # Gets polarity score for TR tweets # -100 is the negative end, 100 is the positive end tr = Translator() tweets = get_tweets(user) tweets_text = tweets_to_string(tweets) tr_text = tr.translate(tweets_text).text return int(TextBlob(tr_text).sentiment.subjectivity * 100)
def get_gender_pred_tr(user): # We only have male and female for the turkish model model_path = f"{URL_ROOT}models/tr/gender_model_tr.pkl" model = pickle.load(open(model_path, "rb")) tweets = tweets_to_string(get_tweets(user)) gender_pred = model.predict_proba((pd.Series(tweets))) male_pred = gender_pred[:, 0] male_score = int(male_pred * 100) female_score = 100 - male_score return {"male": male_score, "female": female_score}
def get_gender_pred_en(user): model_path = f"{URL_ROOT}models/en/gender_model_en.pkl" model = pickle.load(open(model_path, "rb")) tweets = tweets_to_string(get_tweets(user)) gender_pred = model.predict_proba((pd.Series(tweets))) male_pred = gender_pred[:, 0] brand_pred = gender_pred[:, 2] male_score = int(male_pred * 100) brand_score = int(brand_pred * 100) female_score = 100 - male_score - brand_score return {"male": male_score, "female": female_score, "brand": brand_score}
def create_wordclouds(user): # Returns the path for wordclouds for tweets and mentioned path_tw = f"{URL_ROOT}static/images/{user.username}_tw_wordcloud.png" path_ment = f"{URL_ROOT}static/images/{user.username}_ment_wordcloud.png" path_stopwords_tr = f"{URL_ROOT}models/datasets/stopwords-tr.txt" stopwords_tr = txt_to_list(path_stopwords_tr) \ + ["bi", "var", "yok", "sadece", "bence", "sence", "bi", "evet", "hayır", "peki", "tamam", "başka", "aynı", "lazım", "yav", "lan", "la", "olm"] stopwords_tr_en = list(STOPWORDS) + stopwords_tr if os.path.isfile(path_tw): os.remove(path_tw) if os.path.isfile(path_ment): os.remove(path_ment) tw_str = tweets_to_string(get_tweets(user)) ment_str = tweets_to_string(get_mentioned(user)) cloud_tw = WordCloud(stopwords=set(stopwords_tr_en)) cloud_tw.generate(tw_str) cloud_tw.to_file(path_tw) ment_tw = WordCloud(stopwords=set(stopwords_tr_en)) ment_tw.generate(ment_str) ment_tw.to_file(path_ment) return {"tweet_wordcloud": path_tw, "ment_wordcloud": path_ment}
def get_subjectivity_score_tr(user): # Gets subjectivity score for TR tweets # -100 is the negative end, 100 is the positive end tr = Translator() score = 0 tweets = get_tweets(user) tweets_text = tweets_to_string(tweets) text_list = [] for i in range(0, len(tweets_text), 15000): text_list.append(tweets_text[i:i + 15000]) translations = tr.translate(text_list) for translation in translations: score += int(TextBlob(translation.text).sentiment.subjectivity * 100) score = int(score / len(text_list)) return score
def get_subjectivity_score_en(user): # Gets subjectivity score for EN tweets # 0 is the factual end, 100 is the subjective end tweets = get_tweets(user) tweets_text = tweets_to_string(tweets) return int(TextBlob(tweets_text).sentiment.subjectivity * 100)
def get_polarity_score_en(user): # Gets polarity score (is the tweet negative, positive or neutral?) for EN tweets # -100 is the negative end, 100 is the positive end tweets = get_tweets(user) tweets_text = tweets_to_string(tweets) return int(TextBlob(tweets_text).sentiment.polarity * 100)