Since the advent of its democracy in 1975, the political landscape in Spain had been strongly bi-partisan. The two main parties representing the right and left sides of the political spectrum -in their current incarnations PP and PSOE -have typically received 80 % or more of the votes. The rest of the political representation was made of minority parties, mostly focused on regional interests.
During the world financial crisis, which hit Spain the hardest in 2012, unemployment rose to about 30% of the workforce and to about 60% in the case of workers under 25 years of age [1] . That same year, the government took steps to bail out banks filing for bankruptcy, a decision which, together with new austerity measures on Spain's welfare system and the numerous high-profile corruption cases coming into light, may be responsible for the breaking of the status-quo. The Spanish political scene since then has seen the rise of anti-establishment movements, sometimes in the form of citizen's platforms which coalesced into political parties.
This evolution is reflected in the fragmentation of the political spectrum shown in Fig. 1 . From 2012 on, the composition of the Congress of Deputies became much more colourful. Far-left Podemos and far-right Vox movements, as well as an alternative center party Ciudadanos , emerged as leading minority groups in addition to traditional regional parties. Generally speaking, post-2012 Spanish politics became more diverse, and more focused on identity issues. As regional tensions in the Spanish system -already a strongly federal one-were heightened, we witnessed the political growth of Catalan and Basque separatist groups.
In this study we show how modern data science can help characterizing the tenets represented by political parties, and their evolution. In other words, we will use data to feel the pulse of Spanish politics. Beyond official party manifestos, the real pulse of politics expresses itself be found in media outlets such as Twitter. Indeed, we will show that one can use tweets from political leaders as a proxy for the parties' emotional state.
We will start with a simple frequency analysis to extract each party's ideological bubble, then we move onto a more sophisticated analysis to quantify sentiment evolution, i.e. the level of negativity and positivity as a reaction to events. Finally, we show how, using predictive AI tools, one can learn to identify party ideological adherence with the inspection of individual tweets.
Before we present our results, we describe the procedure to generate the datasets and the pre-processing needed to obtain quantitative results.
Our input data is the content of tweets. We have restricted ourselves to the five main * political parties: Ciudadanos Podemos PP PSOE and Vox . Since we would like to focus on the political situation since the bi-partism was broken, with new parties gaining national relevance, we examine tweets from January 2016.
To obtain the tweets we first enrolled as developers with Twitter (a relatively simple procedure for academic purposes) and then used standard tools to select and collect the tweets. The most common tool is tweepy [2] , the official Twitter API which enables us to obtain tweets from different accounts. There is an issue with this package, though, as it only allows the collection of the last 3200 tweets per account. Maybe this quantity would be huge for the most part of the population, but not for politicians, who display a constant presence in Twitter. To collect a larger tweet datasample we used another tool called
In order to avoid subjective bias in the selection of candidates, we explore the main webs of the political parties and examine the list of members of the internal committees. To obtain similar numbers of tweets for each party, we are forced to select different numbers of members in each case, depending on the activity of the different members of the committees, see Table 1 . In particular, for Podemos we select a larger number of representatives. After acquiring the raw data in the form of a collection of tweets, we pre-process as shown schematically in Fig. 2 . For each tweet, we attach a label with the political party it represents and note the date it was written. The contents of the tweet (string of text) are first tokenised, namely broken down into words, keeping their relative position. For example, the poem line 'So few grains of happiness' becomes a sequence of ('so', 'few', 'grains', 'of', 'happiness'). We clean this sequence further by removing stop words, e.g. 'of'. Then the items of this sequence are lemmatised, i.e. some words are substituted by their lemma, e.g. 'happiness' → 'happy'. Additionally, we remove short tweets with fewer than seven words. At the end of this process of data cleaning we were left with about 108,000 tweets for the analysis.
For the neural network predictive analysis described in Sec. , the tweets are substituted by numerical arrays, with a column for each appearing word, and a value of 0,1,2 . . . depending on how many times a particular word appears in a tweet. This process is called TextVectorization and enables a numerical analysis, with the input for the Neural Network made of a set of arrays, encoding each tweet, and a label for the political party.
Before embarking into a numerical sentiment analysis and predictive Neural Network algorithms, we can gain some initial understanding on possible ideological differences among parties by using Word Clouds. These clouds are graphical representations of the word frequency and are nowadays quite commonplace.
In Fig. 3 we show the Word Clouds for the year 2019. The higher the recurrence of a particular item in the tweets, the larger the word will appear in the cloud. Parties with very repetitive messages will then show large words, whereas parties with more diverse messages (vocabulary-wise) will show a cloud with many words.
The differences among the parties' clouds are obvious. On the far left of the political spectrum, the world cloud of Podemos highlights derecho (legal rights) , gente (people) besides its own name.
PSOE 's word cloud is the most diverse one, although with high recurrence of its own socialist project (proyecto and socialista), the opposition's label derecha (right wing) and the word futuro (future).
On the centre-right spectrum we find Ciudadanos and PP , both quite focused on the governing left party PSOE with words like Sanchez (PSOE's leader) and PSOE high in the recurrence list. Nevertheless, there are some differences, with Ciudadanos quite focused on the Catalonian independence issue (Cataluña), and PP with Madrid, where they lead the local government.
Vox 's cloud also shows high incidence of words related to national identity: Cataluña and Barcelona, related to the Catalonian independence dispute, as well as the word españoles (Spaniards) and Europa (Europe).
For each party, the clouds are very similar each year with the exception of PSOE 's in 2016, which was the leading opposition party and trying to impeach PP 's leader and * Note that we are focusing on Spanish-wide parties. ERC, a separatist Catalonian party, did overtake Ciudadanos in the November 2019 elections.
PSOE Ciudadanos PP Vox @PabloIglesias @sanchezcastejon @InesArrimadas @pablocasado @Santi ABASCAL @MiguelUrban @CristinaNarbona @MarinaBS Cs @JavierMaroto @Jorgebuxade @pilar lima @Adrilastra @CCuadradoCs @ja nietob @vicpiedra @anamarcelloana @abalosmeco @jmespejosaav @AlmeidaPP @rromerovilches @pbustinduy @santicl @joanmesquida62 @sanchezcesar @Pablosaenzd @Alber Canarias @JoseantonioJun @MelisaRguezCs @abeltran ana @Vox Molina @ionebelarra @patxilopez @BalEdmundo @javiermarquezsa @MeerRocio @MayoralRafa @oscar puente @Tonicanto1 @vicentetiradopp @Igarrigavaz @Julio Rodr @gomezdcelis @Lroldansu @DiegoCalvoPouso @jlsteeg @VeraNoelia @ignacioaguado @RuizSolas @IreneMontero @Albert Rivera @pedro fhz @pnique @VicencNavarro president (Mariano Rajoy). The world cloud showed then a high incidence of keywords Rajoy and Cambio ('change').
All parties, from far left to far right exhibit very different word clouds, rough representations of their ideological bubbles.
With Data Science tools we can go beyond this naive analysis and quantify deeper issues such as: 1.) Does the sentiment of a party message change, and is this change a reaction to external events? and 2.) Is each party's message distinctive and can be identified tweet-by-tweet?
We will explain methodologies to answer both questions, with the use of Sentiment Analysis and Artificial Intelligence (AI), respectively.
In this section we explain a procedure to explore the evolution of the sentimental change of political parties along this last four years.
Within the area of Data Science, human language is studied under the umbrella of Natural Language Processing (NLP). With the increasing use of AI, NLP has become a very powerful way to analyse and predict human communication. For example, with NLP tools one can effectively identify fake content [6] , predict the next word/sentence in a conversation [7] , analyse speech patterns [8] , generate new texts [9] among many others.
For this project we focused on the evolution of sentiment, namely how positive or negative tweets from party representatives are. Most words do not carry any universal sentimental value, e.g. 'house' is a neutral word for most people, but other words do carry universal sentimental value. For example 'great' and 'horrible' have clear sentiment associations, as well as smiling or vomiting emojis.
In the area of NLP we have tools to transform a string of text into a sentiment score, with positive/negative numerical values indicating the level of sentiment. For the analysis we will present here, we used a python library called VADER (Valence Aware Dictionary and sEntiment Reasoner) [4] , which is particularly well adapted to social media contexts. This library is only available in English, hence we first translate the tweets to English using a python library translate [5] and perform the analysis on the translated tweets.
We can represent sentiment in a number of ways, and here we choose two: 1.) distribution of sentiment of the political parties, linked to the level of polarization of their messages, and 2.) time-evolution of this sentiment as a consequence of external events.
Firstly, in Fig. 4 we analyse all tweets from 2019 and plot the sentiment distributions. The traditional parties, PSOE and PP (upper panel), show a clear bias towards positive sentiment. We have observed this behaviour in all years we have analyzed, despite the change in government. On the other hand, newer parties (lower panel) send more polarized messages, where the ratio of negative to positive messages is much higher than for the traditional parties. In the case of Podemos this ratio decreased in 2019, coinciding with their joining of PSOE in a coalition government. Acquiring governing responsibilities seem to have lowered their level of polarization.
This separation of behaviour of well-established and newer parties may be due to the level of political experience in their leadership composition. Established parties may not engage in too-extreme messages to the population, trying to display a position of more balanced and professional communication.
Besides overall positivity and negativity distributions, one can quantitatively investigate whether changes in sentiment could be linked to specific events. To do so, in Fig. 5 we display the monthly median of sentiment level.
There are a number of points to discuss in this figure. In the following discussion, when we talk about a party's positivity or negativity, we simply refer to the value in this curve, it is not meant to be an absolute statement about the party.
If one pays attention to the horizontal dashed line, corresponding to zero overall sentiment, the red PSOE and blue PP lines are above that line for the whole period, except that PP drops sharply below during the COVID pandemia. The overall positivity of these two traditional parties has already been noted in Fig. 4 . The jump of PP to negative values as the lockdown evolved seems to be a reaction to the government's ( PSOE and Podemos ) handling of the pandemia, of which PP has been extremely critical.
New parties are markedly more negative, again a reflection of what we already observed in Fig. 4 .
Moreover, there are interesting differences among these new parties. Vox is clearly the most biased towards negative sentiment and it also displays a more variable behaviour. The behaviour of centrist Ciudadanos was closer to the traditional parties during the period preceding the Catalonian independence proclamation, but at that moment it started dropping in sentiment values. This negative trend was broken in March 2020, where a new leader was elected for this party. On the other hand, Podemos shows an interesting evolution: with an overall negative profile, it suddenly starts coupling its behaviour to the other left-wing PSOE after the right-wing President is successfully impeached. Note how the highs and lows of Podemos and PSOE follow the same pattern from that impeachment on.
Vox 's sentiment values after the 2016 election, when this party did not fare particularly well (see Fig. 1 ) evolved towards more negativity. This negative trend is also apparent during the months preceding the Catalonian declaration of independence. As discussed in the ideological bubble section, territorial identity issues are a strong focus of Vox . The independence process was squashed, its political leaders were arrested or fled the country. This failure strengthened the ideological position of Vox , and may be the origin of the unusual level of positivity in the early 2018.
Another positive trend appears in the 2019 general elections, when Vox 's representation increased dramatically, see last graph in Fig. 1 , becoming the third most voted party in Spain. Following the other opposition parties, Vox shows a downward trend following the beginning of the COVID pandemic.
Despite PP 's overall positive sentiment, one can observe a negative correlation with Podemos up to the Catalonian declaration of independence. Up to that point, an upward trend for PP was paired with a downward evolution for Podemos and vice-versa. After that point, we see PP exhibiting downward trends related to negative events for the party, e.g. the President's impeachment, leading to the loss of the governance for PP. We also see a positive peak inbetween the two 2019 general elections (April and November of the same year), when the leading party PSOE was unable to form a coalition government and re-ran the elections.
Between the same two 2019 general elections, Ciudadanos went from 57 representatives to 10. The re-run of the elections was very damaging for this political party but, interestingly, the evolution of the sentiment was not the expected downward trend.
The sentiment values of PSOE went up after a successful impeachment, and each general election in 2019 -when they were the most voted political party. Their sentiment values decreased sharply around the time lockdown was announced by their governing coalition, and also after Catalonia's independence declaration.
In the previous section we showed that trends in sentiment values could be matched to specific events, and that some parties' behaviour do correlate and anti-correlate. Particularly interesting is the case of Podemos , which was anti-correlated with PP 's behaviour until the Catalonian declaration of independence, and correlated with PSOE from the President Rajoy's impeachment on.
In this section we are going to explore another way of looking at the same data. Instead of analysing the sentiment carried by the meaning of words inside tweets, we simply analyse the tweets as strings of word-frequency numbers, as explained in Sec. and schematically represented in Fig. 2 . With these strings as inputs and the political affiliation as label, we perform a supervised Machine Learning analysis based on Neural Networks, see Fig. 6 .
The architecture of our Neural Network consists of an input layer with the vectorised tweets, three hidden layers with 16 neurons each, plus intermediate dropout layers, and a categorical output in the form of one of the following labels: Vox PP Ciudadanos PSOE Podemos . This Neural Network is going to learn to classify tweets as belonging to a party. It will train with an amount of data (80% of the total dataset from the year 2019) and refine an algorithm which provides each individual tweet's probability of belonging to a party. The training in this case is supervised, namely at each iteration of the Neural Network training it gets a feedback on how well it has been able to predict. After many iterations with no increase in accuracy, the values of the algorithm are frozen and used to produce predictions for new, unseen tweets (20% of the dataset). To avoid biases in the training, we balance the amount of tweets for each party, in a process called undersampling.
The algorithm's ability to learn can be expressed as a total accuracy, namely how many tweets were correctly assigned. Since we are dealing with a multi-class problem, it is better to express the results in terms of a confusion matrix, see Fig. 7 . In this figure we show a matrix which contains percentages. The x-axis correspond to true labels, and y-axis is the predicted label. The way to understand this matrix is the following:
The diagonal, deep green, entries show the percentage of tweets that are correctly assigned for each party. For example, a tweet from a leader of Podemos would be correctly identified 75% of the time, and one from Vox 71%. The non-diagonal entries, in light pink colours represent how often tweets from a political party are mis-identified. For example, let us look at the first column, corresponding to Podemos . These numbers show that a true tweet from this party will be mis-identified three times more often as a tweet from their coalition government partners, PSOE , than with any of the right-wing parties.
The algorithm finds a higher mis-identification rate of tweets from Vox to and from Ciudadanos than with PP , despite belonging to the far-right and centre respectively. This may be due to their shared strong interest in the Catalonian territorial issues.
One can find another significant pair of correlations between PP and PSOE , which could be traced back to their shared status of traditional party.
We can also ask the Neural Network a simpler question, whether a tweet is from a left-or right-leaning party. The statistical answer to that question is provided in Fig. 8 . About 90% of the time, the algorithm will get the answer right. 
Data Science is becoming a very powerful tool to investigate human interactions. For example, the development of Natural Language Processing (NLP) techniques allows us to scrape large amounts of language-based interactions and analyse them in ways that a few years back were inconceivable. On top of that, the development of new and more powerful prediction techniques, in particular Neural Networks, opens new possibilities for prediction and search for unknown patterns.
To demonstrate this potentiality, we have chosen a particular set of human interactions, social media outlet via Twitter, and the political arena in our home country, Spain. When trying to check the political pulse, communication by Twitter is subject to lots of noise, but also quite dynamical in response to current events. Data Science is prepared to deal with noisy environments provided enough data is analysed.
We have described several levels of data analysis for the tweets written by Spanish politicians since 2016. We started with a simple analysis of the party ideological bubble by looking at the recurrence of words. From these bubbles we noted a number of salient points: how some parties are more focused on other party's leaders than their own programme, or how some political movements are quite tuned to territorial issues.
Those bubbles do provide some information, but only on broad features. To go a step further we took two different routes:
1.) We analysed levels of positive and negative sentiment in the tweets using new tools adapted to social media. From that study we found a number of very interesting features. We first observed that new and traditional parties formed different sets of sentiment behaviour, with traditional parties more geared towards positive messages. But more complex patterns were revealed when we studied the overall party sentiment as a function of time. We observed correlations of this sentiment with political events, as well as in-between parties' behaviour.
2.) We analysed the content of the tweets (without assigning any sentimental value) and trained a form of Artificial Intelligence, Neural Network, to recognise the political affiliation of a tweet. The AI was able to predict the origin of the tweet with a precision in the range of 71-75%, and the political leaning (left or right) with a precision of around 90%. The high accuracy achieved by the AI implies there is a high level of adherence to specific political views. Apart from overall predictions, we also found interesting features on the mis-indentification rates, indicating some level of proximity between parties' agendas, e.g. positions respect to the Catalonian independence claims.
Our analysis has a number of approximations which could be improved upon. Leaders' tweets capture the political pulse of our representatives, but one could expand this study to include the followers of political leaders, and explore to what level there is more general adherence to political views. The sentiment analysis we performed required an automatic translation from Spanish to English which could have added some level of noise, especially when dealing with the informal language present in tweet exchanges. The Neural Network analysis was based on a very simple architecture which we did not tune to achieve the best possible values, and one could expect a few percent improvement by tuning the hyper-parameters. Moreover, in this part of the study, we could have added mixed inputs including other sources beyond tweets.
This study is meant as a proof-of-concept exploration. Our results show that NLP and AI tools can be useful and reliable when dealing with complex human interaction problems. Despite our focus on the Spanish political system in 2016-2019, we believe the range of possible applications is huge. For example, this study is directly translatable to any country with a democratic system and a wide use of Twitter or other social media outlets.
The database and code (in python) to reproduce this analysis will be made public at Miguel Folgado's GitHub account (Miguelfolgado).