Month: October 2020

Find the emotions in text for any twitter account, Trump, Biden and Obama case study

By Using this code, you can calculate the emotions from the text.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import itertools
import collections
import twitter
import seaborn as sns
import tweepy as tw
import nltk
from nltk.corpus import stopwords
import warnings
import re
import text2emotion as te

# 
warnings.filterwarnings("ignore")
sns.set(font_scale=1.5)
sns.set_style("whitegrid")

consumer_key= 'Enter your consumer_key'
consumer_secret= 'Enter your consumer_secret'
access_token= 'Enter your access_token'
access_token_secret= 'Enter your access_token_secret'


auth = tw.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tw.API(auth, wait_on_rate_limit=True)


api = twitter.Api(consumer_key=consumer_key,
  consumer_secret=consumer_secret,
  access_token_key=access_token,
  access_token_secret=access_token_secret)


#write  the twitter account name
Account_name  = "@BarackObama"  
 
keepres = api.GetUserTimeline(screen_name = Account_name, count=200)

# create lists to save the emotions type for each tweets 
Happylst = []
Angrylst = []
Surprist= []
Sadlst = []
Fearlst= []
keep_Happyx = 0
keep_Angryx = 0
keep_Surprisex = 0
keep_Sadx = 0
keep_Fearx = 0


all_tweets = []
tweets = [i.AsDict() for i in keepres]
for t in tweets:
    all_tweets.append(t['text'])

 
# remove links, clean the tweets
def remove_url(txt):
    return " ".join(re.sub("([^0-9A-Za-z \t])|(\w+:\/\/\S+)", "", txt).split())


all_tweets_no_urls = [remove_url(tweet) for tweet in all_tweets]

 1
for t in all_tweets_no_urls:
    keepemo = te.get_emotion(t)
    Happylst.append(keepemo['Happy'])
    Angrylst.append(keepemo['Angry'])
    Surprist.append(keepemo['Surprise'])
    Sadlst.append(keepemo['Sad'])
    Fearlst.append(keepemo['Fear'])
Result:
# find the maximum feeling in each tweet
 
def find_maxemotion(Happyx, Angryx,  Surprisex, Sadx, Fearx ):
    res_max = ''
    res_max_value = 0
    
    if Happyx > Angryx and Happyx > Surprisex and Happyx > Sadx and Happyx > Fearx:
        res_max = 'Happy'
        #res_max_value = Happyx
    
    if Angryx > Happyx and Angryx > Surprisex and Angryx > Sadx and Angryx > Fearx:
        res_max = 'Angry'
        #res_max_value = Angryx
        
        
    if Surprisex > Happyx and Surprisex > Angryx and Surprisex > Sadx and Surprisex > Fearx:
        res_max = 'Surprise'
        #res_max_value = Surprisex
        
    if Sadx > Happyx and Sadx > Angryx and Sadx > Surprisex and Sadx > Fearx:
        res_max = 'Sad'
        #res_max_value = Sadx
   
    if Fearx > Happyx and Fearx > Angryx and Fearx > Surprisex and Fearx > Sadx:
        res_max = 'Fear'
        #res_max_value = Fearx
    
    return res_max



for t in all_tweets_no_urls:
    keepemo = te.get_emotion(t)
    emres = find_maxemotion(keepemo['Happy'], keepemo['Angry'], keepemo['Surprise'], keepemo['Sad'], keepemo['Fear'])
    Happylst.append(keepemo['Happy'])
    Angrylst.append(keepemo['Angry'])
    Surprist.append(keepemo['Surprise'])
    Sadlst.append(keepemo['Sad'])
    Fearlst.append(keepemo['Fear'])
    if emres == 'Happy':
        keep_Happyx += 1

    if emres == 'Angry':
        keep_Angryx += 1
        
    if emres == 'Surprise':
        keep_Surprisex += 1
        
    if emres == 'Sad':
        keep_Sadx += 1

    if emres == 'Fear':
        keep_Fearx += 1

print('Finish reading')
# print number of each emotion
print('Happy', keep_Happyx)
print('Angry', keep_Angryx)
print('Surprise', keep_Surprisex)
print('Sad', keep_Sadx)
print('Fear', keep_Fearx)
# print the first 4 tweets 
all_tweets_no_urls[4]
Result:
['Banking Marketplace Making a Wise Pivot Banking is necessary banks are not',
 'The aim is to enable people to discover whether the doctors and hospitals they visit may have motives other than pa',
 'Civilized people respect others Civilized people know the limits of their freedom of speech Your freedom ends w',
 'PayPal to allow cryptocurrency buying selling and shopping on its network']
# The result in chart
import matplotlib.pyplot as plt
names = ['Happy', 'Angry', 'Surprise', 'Sad','Fear']
values_1 = []
values_1.append(keep_Happyx)
values_1.append(keep_Angryx)
values_1.append(keep_Surprisex)
values_1.append(keep_Sadx)
values_1.append(keep_Fearx)
   
x =[]
for xc in range(len(y1)):
    x.append(xc)
    
plt.figure(figsize=(30, 9))
plt.subplot(131)
plt.bar(names, values_1)
plt.title(Account_name + " Emotions ")


plt.subplot(132)
plt.title( Account_name  + " Emotions - Happy (green), Fear (red)" )
plt.plot(x, Happylst,  marker='o', color="green",  markersize=2, linewidth=2 , label="Happy")
plt.plot(x, Fearlst,  marker='o', color="red",  markersize=2, linewidth=2 , label="Fear")

Biden tweets:

# the last 4 Tweetes
'RT BarackObama Dont boo vote Happy Halloween everybody',
 'President Obama and I left President Trump a playbook on how to deal with pandemics He flatout ignored it And we',
 'If youre planning to vote early inperson today is your last day to do so in many states Head to',
 'RT TeamJoe Voting Update Early vote hours have been extended in Douglas County NE to 500 PM today Find your early voting location a']

Trump tweets:

#the last 4 tweets:
['Biden & Obama owe a massive apology to the People of Flint The water was poisoned on their watch Not only did the',
 'Bidens plan to Abolish American Energy is an economic DEATH SENTENCE for Pennsylvania A vote for Biden is a vote',
 'Our ECONOMY is now surging back faster better bigger and stronger than any nation on earth We just had the best',
 'I ran for office 4 years ago because I could not sit by amp watch any longer as a small group of Washington Insiders']

Obama tweets:

#the last 4 tweets:
['Dont boo vote Happy Halloween everybody',
 'Three days Michigan Three days until the most important election of our lifetimes Join me and JoeBiden for our',
 'You could be the difference between someone making it out to the polls or staying home And many states could be de',
 'Always great talking to KingJames and MavCarter about everything from this unique NBA season to the importance of']
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-4073386782957450"
     data-ad-slot="3414177424"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

{ Add a Comment }

Convert Arabic text to Buckwalter transliteration and vice versa

this code will convert Arabic text to buckwalter and vice versa by using python3.
araletters = {u"\u0627":'A',
u"\u0628":'b', u"\u062A":'t', u"\u062B":'v', u"\u062C":'j',
u"\u062D":'H', u"\u062E":'x', u"\u062F":'d', u"\u0630":'*', u"\u0631":'r',
u"\u0632":'z', u"\u0633":'s', u"\u0634":'$', u"\u0635":'S', u"\u0636":'D',
u"\u0637":'T', u"\u0638":'Z', u"\u0639":'E', u"\u063A":'g', u"\u0641":'f',
u"\u0642":'q', u"\u0643":'k', u"\u0644":'l', u"\u0645":'m', u"\u0646":'n',
u"\u0647":'h', u"\u0648":'w', u"\u0649":'y', u"\u064A":'y',
u"\u0622":'|', u"\u064E":'a', u"\u064F":'u', u"\u0650":'i',
u"\u0651":'~', u"\u0652":'o', u"\u064B":'F', u"\u064C":'N',
u"\u064D":'K', u"\u0621":'\'', u"\u0623":'>', u"\u0625":'<',
u"\u0624":'&', u"\u0626":'}', u"\u0629":'p', " ":' '
}
def split(word): 
    return [char for char in word]  
      

def get_key(v): 
    for key, value in araletters.items(): 
         if v == value: 
             return key 

    return ""
 

def get_val(k): 
    for key, value in araletters.items(): 
         if k == key: 
             return value 

    return ""
 
   
def convert_bw_to_ara(xsent):
    res = ''
    splstr = split(xsent)
    for x in splstr:
        cara = get_key(x)
        if cara != '':
            res += cara
        else:
            res += x

    return res


def convert_ara_to_bw(xsent):
    res = ''
    splstr = split(xsent)
    for x in splstr:
        cara = get_val(x)
        if cara != '':
            res += cara
        else:
            res += x

    return res
res = convert_ara_to_bw('الحساسية من بعض الأطعمة قد يكون لها عواقب وخيمة، فعندما يتناول شخص كميات ضئيلة من أحد الأطعمة التي يعاني من حساسية إزاءها، قد يحدث رد فعل مناعي طفيف يتضمن الحكة والتورم وألم المعدة.')
print(res)

Result:  
AlHsAsyp mn bED Al>TEmp qd ykwn lhA EwAqb wxymp، fEndmA ytnAwl $xS kmyAt D}ylp mn >Hd Al>TEmp Alty yEAny mn HsAsyp <zA'hA، qd yHdv rd fEl mnAEy Tfyf ytDmn AlHkp wAltwrm w>lm AlmEdp.
res = convert_bw_to_ara("AlHsAsyp mn bED Al>TEmp qd ykwn lhA EwAqb wxymp، fEndmA ytnAwl $xS kmyAt D}ylp mn >Hd Al>TEmp Alty yEAny mn HsAsyp <zA'hA، qd yHdv rd fEl mnAEy Tfyf ytDmn AlHkp wAltwrm w>lm AlmEdp.")
print(res)

Result: 
الحساسية من بعض الأطعمة قد يكون لها عواقب وخيمة، فعندما يتناول شخص كميات ضئيلة من أحد الأطعمة التي يعاني من حساسية إزاءها، قد يحدث رد فعل مناعي طفيف يتضمن الحكة والتورم وألم المعدة.

{ Add a Comment }

Find similar Sentences in holy Quran, Python Example

# -*- coding: utf-8 -*-
from nltk.corpus import stopwords 
from nltk.tokenize import word_tokenize 
import pandas
# Arabic Stop words
arastopword = stopwords.words('arabic')  

names = ['aya']
data = pandas.read_csv('data/qr_with/quran.txt',  names=names)
print("All Ayat: ", len(data ))
print(data)

# Enter terms
Searchfor = 'هارون وزير فرعون'

# similarity degree
simdegree = 0.2

Result = [] 
def findsimilarity(X, Y):
    # tokenization 
    X_list = word_tokenize(X)  
    Y_list = word_tokenize(Y) 
      
    # sw contains the list of stopwords 
    #sw = stopwords.words('arabic')  
    l1 =[];l2 =[] 
      
    # remove stop words from string 
    X_set = {w for w in X_list if not w in arastopword}  
    Y_set = {w for w in Y_list if not w in arastopword} 
      
    # form a set containing keywords of both strings  
    rvector = X_set.union(Y_set)  
    for w in rvector: 
        #print(w)
        if w in X_set: l1.append(1) # create a vector 
        else: l1.append(0) 
        if w in Y_set: l2.append(1) 
        else: l2.append(0) 
    c = 0

  
    # cosine formula  
    for i in range(len(rvector)): 
        c+= l1[i]*l2[i] 
    cosine = c / float((sum(l1)*sum(l2))**0.5) 
    
    #print("similarity: "  , cosine) 

    if cosine > simdegree:      
        Result.append([Y, round(cosine, 4)])


for d in data['aya']:
    findsimilarity(Searchfor, d)

def takeSecond(elem):
    return elem[1]

# sort the result
Result.sort(key=takeSecond)

# print the final result
for j in Result:
    print(j[0])
    print(j[1])
    print('')
    
وأضل فرعون قومه وما هدى
0.25

قال فرعون وما رب العالمين
0.25

فأرسل فرعون في المدائن حاشرين
0.25

من فرعون إنه كان عاليا من المسرفين
0.25

اذهب إلى فرعون إنه طغى
0.2887

اذهبا إلى فرعون إنه طغى
0.2887

قوم فرعون ألا يتقون
0.2887

اذهب إلى فرعون إنه طغى
0.2887

هارون أخي
0.3536

فرعون وثمود
0.3536

{ Add a Comment }

Holy Quran Word Clouds by word2vec algorithm, python example

طريقة رسم الكلمات المتشابهه من القران الكريم على شكل word cloud باستخدام الفكتور مودلword2vec لحساب الكلمات المتشابهة، يمكنك تحميل المودل واتباع خطوات البرنامج للحصول على نفس النتائج.

Download the model:

from gensim.models import KeyedVectors
from bidi.algorithm import get_display
import arabic_reshaper
import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud
from typing import List, Dict
# function to plot the word cloud 
def plot_word_cloud(word_list: List[str], word_frequency: Dict[str, float]):
    full_string = ' '.join(word_list)
    reshaped_text = arabic_reshaper.reshape(full_string)
    translated_text = get_display(reshaped_text)   
    # Build the Arabic word cloud
    wordc = WordCloud(font_path='tahoma',background_color='white', width=800, height=300).generate(translated_text)
    wordc.fit_words(word_frequency)
        
    # Draw the word cloud
    plt.imshow(wordc)
    plt.axis("off")
    plt.tight_layout(pad = 0)
    
    plt.show()
# load the model
model = KeyedVectors.load('model/quran_w7_m15.bin') 
print("Model loaded")

#check the model size
print ('Number of all words: ', len(model.wv.vocab))
# Enter the word you want to search
Word_to_plot = 'النهار' 

#result size 
retsize = 200
  
temp_tuple = model.most_similar(positive=[Word_to_plot], negative=[], topn = retsize)

similar_words=[i[0] for i in temp_tuple]
        
word_frequency = {}
for word_tuple in temp_tuple:
    reshaped_word = arabic_reshaper.reshape(word_tuple[0])
    key = get_display(reshaped_word)
    word_frequency[key] = word_tuple[1]     
    
 
plot_word_cloud(similar_words, word_frequency)

Result:

# Enter the word you want to search
Word_to_plot = 'كريم' 

#result size 
retsize = 200
# Enter the word you want to search
Word_to_plot = 'النار' 

#result size 
retsize = 200

{ Add a Comment }

Emotion from the text (sadness , joy , fear , anger ) python Example

under editing

download the sample date:

Date and time: Sat Feb 22 04:14:23 +0000 2020

text: RT What a coincidence that Russia always happens to support whoever the Democrat party elites are trying to destroy at any given…

sadness: 0.15790210664272308

joy :0.20329616963863373

fear: 0.47116225957870483

anger: 0.2204379141330719

from nltk.tokenize import word_tokenize
import pandas as pd  
import matplotlib.pyplot as plt

I used Trump tweets :


{ Add a Comment }

Translate text by google translate

Install:

$ pip install googletrans

Check supported languages :

import googletrans
print(googletrans.LANGUAGES)

{‘af’: ‘afrikaans’, ‘sq’: ‘albanian’, ‘am’: ‘amharic’, ‘ar’: ‘arabic’, ‘hy’: ‘armenian’, ‘az’: ‘azerbaijani’, ‘eu’: ‘basque’, ‘be’: ‘belarusian’, ‘bn’: ‘bengali’, ‘bs’: ‘bosnian’, ‘bg’: ‘bulgarian’, ‘ca’: ‘catalan’, ‘ceb’: ‘cebuano’, ‘ny’: ‘chichewa’, ‘zh-cn’: ‘chinese (simplified)’, ‘zh-tw’: ‘chinese (traditional)’, ‘co’: ‘corsican’, ‘hr’: ‘croatian’, ‘cs’: ‘czech’, ‘da’: ‘danish’, ‘nl’: ‘dutch’, ‘en’: ‘english’, ‘eo’: ‘esperanto’, ‘et’: ‘estonian’, ‘tl’: ‘filipino’, ‘fi’: ‘finnish’, ‘fr’: ‘french’, ‘fy’: ‘frisian’, ‘gl’: ‘galician’, ‘ka’: ‘georgian’, ‘de’: ‘german’, ‘el’: ‘greek’, ‘gu’: ‘gujarati’, ‘ht’: ‘haitian creole’, ‘ha’: ‘hausa’, ‘haw’: ‘hawaiian’, ‘iw’: ‘hebrew’, ‘he’: ‘hebrew’, ‘hi’: ‘hindi’, ‘hmn’: ‘hmong’, ‘hu’: ‘hungarian’, ‘is’: ‘icelandic’, ‘ig’: ‘igbo’, ‘id’: ‘indonesian’, ‘ga’: ‘irish’, ‘it’: ‘italian’, ‘ja’: ‘japanese’, ‘jw’: ‘javanese’, ‘kn’: ‘kannada’, ‘kk’: ‘kazakh’, ‘km’: ‘khmer’, ‘ko’: ‘korean’, ‘ku’: ‘kurdish (kurmanji)’, ‘ky’: ‘kyrgyz’, ‘lo’: ‘lao’, ‘la’: ‘latin’, ‘lv’: ‘latvian’, ‘lt’: ‘lithuanian’, ‘lb’: ‘luxembourgish’, ‘mk’: ‘macedonian’, ‘mg’: ‘malagasy’, ‘ms’: ‘malay’, ‘ml’: ‘malayalam’, ‘mt’: ‘maltese’, ‘mi’: ‘maori’, ‘mr’: ‘marathi’, ‘mn’: ‘mongolian’, ‘my’: ‘myanmar (burmese)’, ‘ne’: ‘nepali’, ‘no’: ‘norwegian’, ‘or’: ‘odia’, ‘ps’: ‘pashto’, ‘fa’: ‘persian’, ‘pl’: ‘polish’, ‘pt’: ‘portuguese’, ‘pa’: ‘punjabi’, ‘ro’: ‘romanian’, ‘ru’: ‘russian’, ‘sm’: ‘samoan’, ‘gd’: ‘scots gaelic’, ‘sr’: ‘serbian’, ‘st’: ‘sesotho’, ‘sn’: ‘shona’, ‘sd’: ‘sindhi’, ‘si’: ‘sinhala’, ‘sk’: ‘slovak’, ‘sl’: ‘slovenian’, ‘so’: ‘somali’, ‘es’: ‘spanish’, ‘su’: ‘sundanese’, ‘sw’: ‘swahili’, ‘sv’: ‘swedish’, ‘tg’: ‘tajik’, ‘ta’: ‘tamil’, ‘te’: ‘telugu’, ‘th’: ‘thai’, ‘tr’: ‘turkish’, ‘uk’: ‘ukrainian’, ‘ur’: ‘urdu’, ‘ug’: ‘uyghur’, ‘uz’: ‘uzbek’, ‘vi’: ‘vietnamese’, ‘cy’: ‘welsh’, ‘xh’: ‘xhosa’, ‘yi’: ‘yiddish’, ‘yo’: ‘yoruba’, ‘zu’: ‘zulu’}


from googletrans import Translator

def translatetxt(strtxt, fromlang, tolang):
    translator = Translator()
    line = translator.translate(src=fromlang, dest=tolang, text=strtxt)
    return line.text

en = English, ar = Arabic, fr = French

Translate from English to Arabic:

print(translatetxt('i love you' , 'en', 'ar'))
>> انا احبك

Translate from English to French:

print(translatetxt('i love you' , 'en', 'fr'))
>> je t'aime

Translate from English to German:

print(translatetxt('i love you' , 'en', 'de'))
>> ich liebe dich

{ Add a Comment }

Emotion from the text (positive, negative) python Example

Install :

$ pip install textblob 
from textblob import TextBlob

feedbacks = ['I dont like this app ', 
             "The experience was bad as hell", 
             "This app is really helpful and ",
             "Damn the app tastes like shit ",
            'Please don\'t download the app you will regret it ']

positive_feedbacks = []
negative_feedbacks = []

for feedback in feedbacks:
  feedback_polarity = TextBlob(feedback).sentiment.polarity
  if feedback_polarity>0:
    positive_feedbacks.append(feedback)
    continue
  negative_feedbacks.append(feedback)
  
print('Positive_feebacks Count : {}'.format(len(positive_feedbacks)))
print('positive ', positive_feedbacks)

print('')

print('Negative_feedback Count : {}'.format(len(negative_feedbacks)))
print('negative ', negative_feedbacks)


Result:
Positive_feebacks Count : 0
positive  []

Negative_feedback Count : 5
negative  ['I dont_heat you ', 'The experience was bad as hell', 'This app is really helpful and bad', 'Damn the app tastes like shit ', "Please don't download the app you will regret it "]

{ Add a Comment }

Get New common words from Twitter

Install :

pip install twitter
pip install tweepy
pip install python-twitter
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import itertools
import collections
import twitter
import seaborn as sns
import tweepy as tw
import nltk
from nltk.corpus import stopwords
import re
import warnings
import faisal

To get all the code, click GitHub

{ Add a Comment }