T witter-Demographer: A Flow-based Tool to Enrich T witter Data (2022)

Abstract: Twitter data have become essential to Natural Language Processing (NLP) and social science research, driving various scientific discoveries in recent years. However, the textual data alone are often not enough to conduct studies: especially, social scientists need more variables to perform their analysis and control for various factors. How we augment this information, such as users’ location, age, or tweet sentiment, has ramifications for anonymity and reproducibility, and requires dedicated effort. This paper describes Twitter-Demographer, a simple, flow-based tool to enrich Twitter data with additional information about tweets and users. \tool is aimed at NLP practitioners, psycho-linguists, and (computational) social scientists who want to enrich their datasets with aggregated information, facilitating reproducibility, and providing algorithmic privacy-by-design measures for pseudo-anonymity. We discuss our design choices, inspired by the flow-based programming paradigm, to use black-box components that can easily be chained together and extended. We also analyze the ethical issues related to the use of this tool, and the built-in measures to facilitate pseudo-anonymity.

## introduction twitter data are at the heart of nlp and social science research @xcite , used to study policy and decision-making, and understand public opinion's consequences better. its accessibility and the variety and abundance of the data make twitter one of the most fruitful sources to experiment with new nlp methods, and to generate insights into societal behavior @xcite . given that 199 million people communicate on twitter daily, foot_0 it becomes fundamental to find ways to interpret this information better. however, researchers often need more than pure text data to control for the effects of various covariates, stratify the data into sensible subgroups, and assess their reliability. social sciences typically require a recourse to external variables like age or location to control for confounds. in addition, nlp research has shown that integrating sociodemographic information can improve a wide range of classification tasks @xcite @xcite @xcite . by default, this information is not available, and a wide range of nlp tools have been developed to infer measures from the text (i.e., sentiment, syntactic structure: @xcite and user @xcite . here, we introduce twitter-demographer, a tool that provides a simple and extensible interface for nlp and social science researchers. starting from tweet ids (the common way to share twitter data), the tool hydrates the original text and can enrich it with additional information like the sentiment of the tweets, topics, or estimated demographic information of the author, using existing tools. twitter-demographer builds on previous research @xcite @xcite , but puts all these efforts together in one simple tool that can be used with little effort. twitter-demographer can be applied to extract information from different languages, as its default components are either multi-lingual or language-independent. 2 twitter-demographer has a simple api that can be used to add user-defined components quickly and effectively. one of our goals is to provide and enforce the generation of reproducible data enrichment pipelines (i.e., they can be shared and produce the same results if components are kept the same). with data enrichment we mean the process of extending a dataset, e.g., adding new inferred properties, or disambiguating its content @xcite . our flow-based infrastructure makes it easy to produce and share pipelines with other researchers to reconstruct the extended datasets. most importantly, inferring user-related attributes poses a privacy issue, even for research purposes. we implement several algorithmic privacy-by-design solutions to facilitate pseudoanonymity of the users, and to reduce the chance that their personal data or identifiers can be used to identify natural persons. we believe that twitter-demographer can help (computational) social scientists wanting to analyze the properties of their datasets in more depth, and provide nlp practitioners with a unified way to enrich and share data. ## the flow-based paradigm the flow-based paradigm is a programming paradigm that uses a data processing factory metaphor for designing applications as networks of black-box processes @xcite . the paradigm is helpful for data handling because it allows users to easily combine different blackbox components in many ways, fitting different requirements time by time. each component implements a specific task, takes some inputs, and returns some outputs. many solutions employ this kind of paradigm (e.g., apache nifi 3 ). these solutions are directed at experts like data engineers because they require some knowledge about the low-level details (e.g., how to handle data sources, data streams, and event-based executions). the advantage of the flow-based paradigm is that users do not have to know the intrinsic logic of each block (hence black-box). they only have to focus on combining blocks to ensure the proper mapping between inputs/outputs of consecutive blocks. indeed, the main disadvantages of manually building these pipelines are that (i) they require massive effort to be defined; (ii) they are sensitive to various hurdles, e.g., what happens if we cannot find one tweet or its location is unavailable? (iii) they are error-prone, with minor errors possibly tearing down entire pipelines, e.g., what happens if a web service changes its exchange data format, or is no longer available? twitter-demographer has been imagined as a low coupled set of components that operates on a dataset in tabular format (e.g., a pandas dataframe). each component takes the dataset as input, applies some operations on it (e.g., adding columns), and returns the modified dataset. components can be integrated into pipelines: we aim for high cohesion and low coupling principles to reduce possible errors at the component level. each component exposes a set of required inputs (i.e., columns that must be contained in the input dataset) and a set of generated outputs (i.e., names of the new columns added to the dataset). using this information, we can chain different components together to introduce dependencies (e.g., to run the sentiment analysis classifier, we need first to query twitter and create a new column containing the text of tweets). exposing the input and the outputs allows for the consistency between different components to be checked beforehand to avoid compatibility issues. the flow-based setup makes it possible to replace any component with another one implementing the same task with a different logic, as long as the new component respects the communication interface (i.e., expected inputs and generated outputs). it is worth noting that the paradigm does not force a specific absolute order between components: a component requiring some columns as input (e.g., col x , col y ) must be placed in any position after the components generating such columns. the goal of twitter-demographer is two-fold: 1) providing an easy-to-use interface for data enrichment and 2) providing a system that allows more expert users to re-use and modify existing components that are already implemented in twitter-demographer easily. while it is true that twitter-demographer is mainly based on the composition of existing tools, these are wrapped into off-the-shelf components that simplify the usage of the proposed analytics methodologies. ## twitter-demographer we show the class diagram of twitter-demographer in figure 1 . while listing 1 shows an example application of the tool. line 2 instantiates the demographer object, which is responsible for handling the entire pipeline (i.e., it also performs compatibility checks on components). lines 4-6 show the instantiation of the different data augmentation components that will be used in the pipeline (a rehydration component to collect additional information from the tweets, a location decoder based on nominatim, and a sentiment classifier). lines 9-11 add the components to the demographer object, creating the enrichment pipeline. finally, line 14 runs the entire pipeline on the data, generating the enriched dataset. we anyway guarantee the flexibility to allow new components to be implemented. a component (listing 2) is a simple abstract class that can be easily inherited and implemented: introducing a custom classification pipeline requires only adding a custom classifier to the pipeline, which inherits this class and implements the methods that handle inputs, outputs and the method to run the inference on the data. inputs and outputs are exploited by demographer to handle control over the chain of possible components that can be added. a component cannot be added to a pipeline if it requires inputs that twitter-demographer saves the intermediate computation steps, right after each component has been executed, to handle down-streaming unexpected errors (e.g., lost internet connection). in those situations, the computation can be restarted from checkpoints. ## components twitter-demographer is a container of components and can be extended as they are provided by the community. some components come with an automatic caching logic, especially when the component relies on external services with a limited request rate (e.g., public api accessed with free accounts with limited requests). for example, the localization component implements a caching mech- demographer @xmath0: the uml class diagram of the current twitter-demographer setup. demographer is the main class that handles the execution of the different components. component is an abstract class that defines required inputs and produced outputs, as well as an abstract infer() method that has to be implemented by its subclasses. many of the available implementations of the component class are reported in the uml diagram; the others have been removed to keep the image self-contained. class userclassifier(component): def __init__(self, model): super().__init__() self.@xmath1: return ["sentiment"] def inputs(self): return ["text"] def infer(self, data): return {"sentiment" : self.m.predict(data["text"])} listing 3: a user-defined component for sentiment classification of tweets. users add their own classifiers to the pipeline by wrapping them inside the component abstraction. rehydrate many datasets are released as lists of tweet ids. this is a requirement that has to be met to release data without violation of twitter policies. our rehydrate component is based on twitter api v2. this component allows reconstructing tweets in batches of 100 per request. it automatically waits when twitter api rate limits are reached and starts again when those limits have been reset. 4 this component handles the retrieval of all the information that can be collected on twitter from the single tweet id. it requires a twitter api key. ## conclusions we are constantly improving this library to support more use cases and models. for example, we are working on making the geolocation independent of third-party apis like nominatim, trying to support the download of the nominatim index instead to query (thus improving speed and mitigating rate limits). we are introducing multiple methods for topic modeling and additional components for text-clustering @xcite and hyperparameter optimization tools to find the optimal values for these. we aim to provide a simple interface to address different user needs. while the tool is momentarily focused on twitter, most of the components that we have defined have a broader usage (e.g., the localization component).