| Title: | Analyse Political Texts |
|---|---|
| Description: | Wrangle and annotate different types of political texts. It also introduces Urgency Analysis, a new method for the analysis of urgency in political texts. |
| Authors: | Henrique Sposito [cre, aut, ctb], James Hollway [ctb], Jael Tan [ctb] |
| Maintainer: | Henrique Sposito <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.0 |
| Built: | 2026-10-18 11:10:41 UTC |
| Source: | https://github.com/henriquesposito/poldis |
This function relies on '{spacyr}' NLP parsing to annotate texts.
annotate_text(v, level = "words")annotate_text(v, level = "words")
v |
Text vector |
level |
At which level would you like to parse the text? Options include "words" or "sentences". Defaults to "words". |
A data frame with syntax information by words or sentences in text.
#annotate_text(US_inaugural_addresses_1993_2025$text[2])#annotate_text(US_inaugural_addresses_1993_2025$text[2])
A function for getting string matches and the context in which they occur.
extract_context(match, v, level = "sentences", n = 1)extract_context(match, v, level = "sentences", n = 1)
match |
Character string to be matched. For multiple strings, please use "|" as a separator. |
v |
Text vector or annotated data frame. |
level |
At which text level do you want matches to be returned? Defaults to "sentences". Options are sentences, words, and paragraph. |
n |
Number of sentences or words matched before and after string match. Defaults to 1. That is, one word or one sentence before, and after, string match. For paragraphs, n is always set to one. |
A list of string matches and their context.
extract_context(match = " war ", v = US_inaugural_addresses_1993_2025$text[1], level = "sentences", n = 2)extract_context(match = " war ", v = US_inaugural_addresses_1993_2025$text[1], level = "sentences", n = 2)
A lot of information is contained in the first sentence of a text. In political texts, for example, dates and locations are often contained in the first sentence of the text.
extract_first_sentence(v)extract_first_sentence(v)
v |
Text vector. |
A list of the first sentences in text.
extract_first_sentence("This is the first sentence. This is the second sentence.")extract_first_sentence("This is the first sentence. This is the second sentence.")
Extract locations from strings
extract_locations(v)extract_locations(v)
v |
Text vector. |
The function relies on geographical entity detection from NLP models.
A data frame of locations and the number of times they appear.
#extract_locations(c("This is the United States", "This is Sao Paulo", #"I was in Rio de Janeiro and Sao Paulo, then back to the United States"))#extract_locations(c("This is the United States", "This is Sao Paulo", #"I was in Rio de Janeiro and Sao Paulo, then back to the United States"))
Get texts in which certain "matches" occur.
extract_match(v, match, invert = FALSE, ignore.case = TRUE)extract_match(v, match, invert = FALSE, ignore.case = TRUE)
v |
Text vector or annotated data frame. |
match |
A regex match for a word(s) or expression. For multiple words, please use "|" to divide them. |
invert |
Do you want texts without certain matches to be returned? By default FALSE. |
ignore.case |
Should case be ignored? By default, TRUE. |
A list the same length as text variable.
extract_match(c("This function was created on the 29 September 2021", "Today is October 12, 2021"), "October")extract_match(c("This function was created on the 29 September 2021", "Today is October 12, 2021"), "October")
Extract a list of possible names of individuals in texts
extract_names(v)extract_names(v)
v |
A text vector. |
The function relies on named entity recognition from NLP models.
A data frame of individual names and the number of times they appear.
#extract_names("This package was created by Jael, James, and I.")#extract_names("This package was created by Jael, James, and I.")
Extract similarities and differences in texts/segments
extract_text_similarities(v, comparison = "similarities", method)extract_text_similarities(v, comparison = "similarities", method)
v |
Text vector or annotated data frame. |
comparison |
How would you like to compare texts? Options are "similarities", for comparing similarities, or "differences", for comparing differences. Defaults to "similarities". |
method |
A method for checking similarities or differences between texts. For similarities, defaults to "correlation" method. Other methods for similarities include "cosine", "jaccard", "ejaccard", "dice", "edice", "simple matching", and "hamann". For differences, defaults to "euclidean". Other methods for differences include "manhattan", "maximum", "canberra", and "minkowski". For more information on each of these methods and what are the implications in selecting a method, please see '?quanteda.textstats::textstat_simil()'. |
A matrix of similarity scores between texts.
#extract_text_similarities(US_inaugural_addresses_1993_2025$text)#extract_text_similarities(US_inaugural_addresses_1993_2025$text)
Gather topic from political discourses
gather_topics(v, dictionary = "CAP")gather_topics(v, dictionary = "CAP")
v |
Text vector or annotated data frame. If missing, opens the political topics codebook. |
dictionary |
The dictionary of 20 major political topics from the Comparative Agendas Project (Jones et al., 2023) is used by default. Users can also declare a custom dictionary as a vector or a list. If users declare a vector, each element is treated as a independent topic. If users declare a list of subjects and related terms, function understands names as topic and words as terms. For more information on how the CAP topics were adapted, please run 'gather_topics()' to access the political topics codebook. |
A list of topics present in each text separated by comma.
summary(gather_topics(US_inaugural_addresses_1993_2025$text))summary(gather_topics(US_inaugural_addresses_1993_2025$text))
Urgency Analysis
get_urgency(v, summarise = "sum")get_urgency(v, summarise = "sum")
v |
Text vector or annotated data frame. |
summarise |
How to handle multiple matches for the same dictionary in the same text observation? By default, multiple matches are added together and their "sum" per text observation is returned. Users can, instead, choose the "mean", which returns the average score per dictionary per text observation, the "max" or the "min", when there are multiple matches. |
Urgency in political discourses is an expression of how necessary and/or how soon an action should be undertaken or completed. This is measured along four dictionaries, two related to necessity (e.g., degree of intensity and of commitment) and two related to timing (e.g., frequency and timing of action). The dictionaries include terms for each of these dimensions of urgency. Their scores were developed through an online survey that took place in June 2026. The survey results were recorded as counts of the number of participants who selected an urgency-related word as more urgent than its pair. To analyze the survey results, we employed Bradley-Terry models for paired comparisons. We use raw log odds coefficients to calculate urgency. Urgency scores for each disctionary are returned. The aggregated urgency scores is calculated by adding these scores and then divided by the coefficient for the word "must", to ensure interpretability (i.e., an aggregated score of 1 in equivalent to "we must do"). Users are welcome to change how urgency scores are aggregated and transform these scores into probabilities or odds ratios as necessary.
A scored data frame for each dimension of urgency.
get_urgency(US_inaugural_addresses_1993_2025$text) #a = get_urgency(select_priorities(US_inaugural_addresses_1993_2025$text)) #summary(get_urgency(US_inaugural_addresses_1993_2025$text)) #plot(get_urgency(US_inaugural_addresses_1993_2025$text))get_urgency(US_inaugural_addresses_1993_2025$text) #a = get_urgency(select_priorities(US_inaugural_addresses_1993_2025$text)) #summary(get_urgency(US_inaugural_addresses_1993_2025$text)) #plot(get_urgency(US_inaugural_addresses_1993_2025$text))
Read text from PDFs
read_pdf(path)read_pdf(path)
path |
The path to a PDF file or a folder containing multiple PDFs. |
A list of texts.
Political priorities are statements in which actors express their intent or commitment to take political action in the future.
select_priorities(v, na.rm = TRUE)select_priorities(v, na.rm = TRUE)
v |
Text vector or annotated data frame. |
na.rm |
Would you like political statements that do not contain a political action to be removed? By default, TRUE. |
A data frame with syntax information by sentences and a variable identifying which of these sentences are priorities.
#select_priorities(US_inaugural_addresses_1993_2025$text)#select_priorities(US_inaugural_addresses_1993_2025$text)
Split texts into structured lists of lists according to a split sign.
split_text(v, splitsign = "\\.")split_text(v, splitsign = "\\.")
v |
Text vector or annotated data frame. |
splitsign |
Where do you want to split? By default sentences ("."). This can also be words, signals or other markers you want. For special characters, please use escape sign before (i.e. "\"). |
A list of lists the same length as vector.
split_text("This is the first sentence. This is the second sentence.")split_text("This is the first sentence. This is the second sentence.")
A dataset containing nine inaugural addresses since 1993 for five different United States presidents: Clinton, Bush, Obama, Trump, and Biden.
data(US_inaugural_addresses_1993_2025)data(US_inaugural_addresses_1993_2025)
A data frame with 9 rows and 3 variables: the president, the date, and the full text of the inaugural address.
The dataset was gathered from the American Presidency Project website (https://www.presidency.ucsb.edu/).