Skip to main content
QUICK REVIEW

[Paper Review] How to use LLMs for Text Analysis

Petter Törnberg|arXiv (Cornell University)|Jul 24, 2023
Natural Language Processing TechniquesComputer Science17 citations
TL;DR

This guide shows how to use LLMs for text analysis in social sciences, detailing a step-by-step Python workflow, prompts, validation, and an illustrative populism task.

ABSTRACT

This guide introduces Large Language Models (LLM) as a highly versatile text analysis method within the social sciences. As LLMs are easy-to-use, cheap, fast, and applicable on a broad range of text analysis tasks, ranging from text annotation and classification to sentiment analysis and critical discourse analysis, many scholars believe that LLMs will transform how we do text analysis. This how-to guide is aimed at students and researchers with limited programming experience, and offers a simple introduction to how LLMs can be used for text analysis in your own research project, as well as advice on best practices. We will go through each of the steps of analyzing textual data with LLMs using Python: installing the software, setting up the API, loading the data, developing an analysis prompt, analyzing the text, and validating the results. As an illustrative example, we will use the challenging task of identifying populism in political texts, and show how LLMs move beyond the existing state-of-the-art.

Motivation & Objective

  • Demonstrate a practical, low-code workflow for applying LLMs to text analysis in social sciences.
  • Show how to set up API access, load data, and prepare texts for LLM processing without heavy preprocessing.
  • Provide guidance on prompt engineering and iterative validation to ensure reliable results.
  • Illustrate the approach with a populism-detection case to compare LLM results with human coding.

Proposed method

  • Provide a step-by-step Python-based workflow for using LLMs in text analysis (signing up for API access, loading data, prompt engineering, calling the LLM, parsing results).
  • Use a populism measuring task to show how LLMs can capture context-dependent meaning across languages and contexts.
  • Describe how to split texts to fit context windows and how to code outputs in a machine-readable format.
  • Detail validation via Krippendorff’s alpha and comparison with human coders.
  • Advocate iterative concept and prompt development based on disagreement analyses.
  • Offer supplementary material including a Jupyter Notebook and example code.

Experimental results

Research questions

  • RQ1Can LLMs perform text analysis tasks that are difficult for traditional computational methods, such as interpreting sarcasm or contextual meanings in political texts?
  • RQ2How can prompts be designed and validated to produce reliable, reproducible coding outputs from LLMs?
  • RQ3To what extent do LLM-generated analyses align with human coders, across languages and contexts, in measuring populism?
  • RQ4What is the practical workflow (data loading, prompt design, API usage) for social scientists with limited programming expertise?

Key findings

  • LLMs show potential to perform a wide range of text analysis tasks that challenge traditional methods, including contextual interpretation and sarcasm.
  • An illustrative populism task demonstrates LLMs can produce outputs with substantial agreement with human coders, though validation is essential.
  • Krippendorff’s alpha for the LLM-based validation in the example is 0.635, indicating relatively high correspondence given differing instructions.
  • Human coders in the original dataset achieved Krippendorff’s alpha of 0.827, reflecting higher inter-coder agreement when working together.
  • Prompt design and iterative development are crucial to improve accuracy and reliability beyond initial results.
  • The guide emphasizes transparency about model biases, API data handling, speed/cost considerations, and ethical implications.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.