Statistics for Corpus Linguists
  • Overview
  • Introduction to R
    • 1.1 First steps
    • 1.2 Exploring R Studio
    • 1.3 Vectors
    • 1.4 Data frames
    • 1.5 Libraries
    • 1.6 Importing/Exporting
  • NLP
    • 2.1 Concordancing
    • 2.2 Regular expressions
    • 2.3 The CQP interface
    • 2.4 Data annotation
  • Statistics
    • 3.1 Data, variables, samples
    • 3.2 Probability theory
    • 3.3 Descriptive statistics
    • 3.4 Hypothesis testing
    • 3.5 Chi-squared test
    • 3.6 t-test
    • 3.7 ANOVA
    • 3.8 Case study
  • Models
    • 4.1 Linear regression
    • 4.2 Logistic regression
    • 4.3 Poisson regression
  • Machine Learning
    • 5.1 Tree-based methods
    • 5.2 Gradient boosting
  1. 2. NLP with R
  2. 2.4 Data annotation
  • 2. NLP with R
    • 2.1 Concordancing
    • 2.2 Regular expressions
    • 2.3 The CQP interface
    • 2.4 Data annotation

On this page

  • Recommended reading
  • Sample study
    • Theoretical background
    • Obtaining data
  • Data annotation
    • Dealing with problematic cases
    • Getting the data back into R
    • Adding a case list
  • Adding text metadata
  • Where do I go from here?

Other Formats

  • PDF
  1. 2. NLP with R
  2. 2.4 Data annotation

2.4 Data annotation

Author
Affiliation

Vladimir Buskin

Catholic University of Eichstätt-Ingolstadt

Abstract
This handout exemplifies the workflow from a corpus query to an annotated dataset, covering the retrieval of concordance lines with quanteda, manual annotation in a spreadsheet, the handling of false hits, re-importing the data into R and extracting register information from the ICE text categories.

Recommended reading

S. T. Gries (2013): Chapter 1.3.3

Sample study

Theoretical background

Suppose we are interested in the object realisation patterns of the verb eat in the British ICE component. A quick review of the literature tells us that …

  • … argument realisation may be related to the aspectual structure of a verbal action (cf. Goldberg 2001), but …

  • … there seems to be a stronger emphasis on lexical aspect (telicity/atelicity; i.e., logical endpoints of actions) than on grammatical aspect (i.e., perfective/progressive).

Since grammatical aspect is also concerned with the temporal construal of actions, it raises the question of whether or not it can also influence argument realisation. To investigate the relationship between aspect and object realisation, we will perform an exemplary analysis on the verb lemma EAT.

Obtaining data

We load all necessary libraries, query the ICE-GB corpus and run a KWIC search using the regular expression ^(eat(s|ing|en)?|ate)$, which finds all inflectional forms of EAT (see 2.2 Regular expressions). We then store the results in a spreadsheet file kwic_eat.xlsx.

# Load libraries and corpus
library(quanteda)
library(writexl)
library(readxl)
library(tidyverse)

ICE_GB <- readRDS("ICE_GB.RDS")

# Perform query
kwic_eat <- kwic(ICE_GB,
                 "^(eat(s|ing|en)?|ate)$",
                 valuetype = "regex",
                 window = 15)

# Store results
write_xlsx(kwic_eat, "kwic_eat.xlsx")

When you open kwic_eat.xlsx in a spreadsheet software, the file will contain 7 columns by default (docname, from, to, pre, keyword, post, pattern). Each row corresponds to a match of your search expression in the corpus, which amounts to 110 here. This is your raw output.

Why not use word boundaries?

Word boundaries \\b are often used to delimit words in a regular expression. Since kwic() matches each token separately, however, \\b also allows matches within hyphenated tokens: The pattern \\b(eat(s|ing|en)?|ate)\\b finds eating in the compounds egg-eating and bone-eating, too. The anchors ^ and $, by contrast, require the entire token to match.

Data annotation

Whenever you decide to work on your corpus results, it is good practice to duplicate your file and append the current date to the filename. Re-save it as, for instance, kwic_eat_2024-09-09.xlsx and open it again. This way you are performing basic version control, which will allow you to return to previous stages of your analysis with ease. Writing the date in the format year-month-day ensures that your files are also sorted chronologically in your file browser.

In your spreadsheet software, you can now add your variables of interest to the empty columns next to your output data. For our specific example, we will need one that captures object realisation and one the type of verb aspect. Let’s simply call them object_realisation and verb_aspect.

Naming variables

Of course, you could also opt for a different column name, as long as it has no spaces or special characters (e.g., !?%#). You could name the first column object_realisation or, even more plainly, object, but not direct object or object realisation with spaces. Otherwise you are bound to encounter a surge of cryptic error messages in your R console.

Now, you are ready to annotate your data! An easy coding scheme would involve classifying rows where eat occurs with an object as yes. Conversely, rows where the direct object is not realised syntactically are assigned the column value no. In the aspect column, verbal aspect will be coded as either perfective, progressive or neutral, following Gries (2014: 118).

Dealing with problematic cases

However, things are not always that clear-cut. What if you encounter a false positive, i.e., an erroneous hit in your dataset? In the following concordance line, the keyword ate is actually part of the preceding word, inappropriate, which has been split across two lines in the corpus file.

docname pre keyword post
ICE_GB/W2F-019.txt : 1 > Too much colour on her face would be inappropri < l > ate , she feels , but she wears a light foundation . < ICE-GB:W2F-019 #28 :
What do I do with false hits?

Short answer: Do not delete irrelevant rows or columns. Essentially, from the moment you’ve obtained your corpus output, you should withstand the temptation to delete anything from it. Instead, adopt the practice of indicating missing values or irrelevant rows by an NA in a separate column. In later analyses, these can be easily filtered out!

This also minimises the risk of accidentally getting rid of data that could have proven important at a later point in time.

Getting the data back into R

Import the Excel file via

# Read file contents into the variable "kwic_data"
kwic_data <- read_xlsx("kwic_eat_2024-09-09.xlsx")
# Print the first six lines of "kwic_data"
print(head(kwic_data))
# A tibble: 6 × 9
  docname  from    to pre   keyword post  pattern object_realisation verb_aspect
  <chr>   <dbl> <dbl> <chr> <chr>   <chr> <chr>   <chr>              <chr>      
1 ICE_GB…   458   458 had … eaten   anyw… "\\b(e… no                 perfective 
2 ICE_GB…   478   478 : 1 … eating  will… "\\b(e… no                 progressive
3 ICE_GB…   785   785 > Ye… eat     befo… "\\b(e… no                 neutral    
4 ICE_GB…  1198  1198 the … eat     them… "\\b(e… yes                neutral    
5 ICE_GB…  4529  4529 > Ye… ate     in t… "\\b(e… no                 neutral    
6 ICE_GB…   958   958 know… eat     it f… "\\b(e… yes                neutral    

Adding a case list

S. T. Gries (2013) recommends setting up the first column of the data frame such that it “numbers all \(n\) cases from \(1\) to \(n\) so that every row can be uniquely identified and so that you always restore one particular ordering (e.g., the original one)” (S. T. Gries 2013: 26). This is very easy to do: We specify a numeric vector ranging from 1 to the total number of rows in the data frame.

# Create a new Case column (which, by default, is added at the very end of the data frame)
kwic_data$Case <- 1:nrow(kwic_data)

# Move the Case column to the front of the data frame
kwic_data <- relocate(kwic_data, Case)

# Print reordered data frame
print(head(kwic_data))
# A tibble: 6 × 10
   Case docname        from    to pre   keyword post  pattern object_realisation
  <int> <chr>         <dbl> <dbl> <chr> <chr>   <chr> <chr>   <chr>             
1     1 ICE_GB/S1A-0…   458   458 had … eaten   anyw… "\\b(e… no                
2     2 ICE_GB/S1A-0…   478   478 : 1 … eating  will… "\\b(e… no                
3     3 ICE_GB/S1A-0…   785   785 > Ye… eat     befo… "\\b(e… no                
4     4 ICE_GB/S1A-0…  1198  1198 the … eat     them… "\\b(e… yes               
5     5 ICE_GB/S1A-0…  4529  4529 > Ye… ate     in t… "\\b(e… no                
6     6 ICE_GB/S1A-0…   958   958 know… eat     it f… "\\b(e… yes               
# ℹ 1 more variable: verb_aspect <chr>

Adding text metadata

Many research questions involve register, e.g., whether a construction is more frequent in speech than in writing. In the ICE corpora, information on the text category of each hit is stored in the docname column:

head(kwic_data$docname)
[1] "ICE_GB/S1A-006.txt" "ICE_GB/S1A-006.txt" "ICE_GB/S1A-006.txt"
[4] "ICE_GB/S1A-009.txt" "ICE_GB/S1A-009.txt" "ICE_GB/S1A-010.txt"

Each file name contains a text category label (e.g., S1A) followed by a file number (e.g., 001). Depending on the version of the corpus files, the names may also contain a prefix (e.g., ICE_GB/) or the file extension .txt. Instead of splitting the names at fixed positions, it is therefore more robust to extract the relevant parts with regular expressions (see 2.2 Regular expressions):

kwic_data <- kwic_data %>%
  mutate(
    # S or W, followed by 1 or 2, followed by a letter from A to F
    text_category = str_extract(docname, "[SW][12][A-F]"),
    # three digits in a row
    file_number = str_extract(docname, "\\d{3}")
  )

kwic_data %>%
  select(docname, text_category, file_number) %>%
  head()
# A tibble: 6 × 3
  docname            text_category file_number
  <chr>              <chr>         <chr>      
1 ICE_GB/S1A-006.txt S1A           006        
2 ICE_GB/S1A-006.txt S1A           006        
3 ICE_GB/S1A-006.txt S1A           006        
4 ICE_GB/S1A-009.txt S1A           009        
5 ICE_GB/S1A-009.txt S1A           009        
6 ICE_GB/S1A-010.txt S1A           010        
Summary of text categories in the ICE corpora
Category Subcategory Type Code Range
SPOKEN (S) DIALOGUE (S1) PRIVATE (S1A)
Direct Conversations S1A-001 to S1A-090
Telephone Calls S1A-091 to S1A-100
PUBLIC (S1B)
Class Lessons S1B-001 to S1B-020
Broadcast Discussions S1B-021 to S1B-040
Broadcast Interviews S1B-041 to S1B-050
Parliamentary Debates S1B-051 to S1B-060
Legal Cross-examinations S1B-061 to S1B-070
Business Transactions S1B-071 to S1B-080
MONOLOGUE (S2) UNSCRIPTED (S2A)
Spontaneous Commentaries S2A-001 to S2A-020
Unscripted Speeches S2A-021 to S2A-050
Demonstrations S2A-051 to S2A-060
Legal Presentations S2A-061 to S2A-070
SCRIPTED (S2B)
Broadcast News S2B-001 to S2B-020
Broadcast Talks S2B-021 to S2B-040
Non-broadcast Talks S2B-041 to S2B-050
WRITTEN (W) NON-PRINTED (W1) NON-PROFESSIONAL WRITING (W1A)
Student Essays W1A-001 to W1A-010
Examination Scripts W1A-011 to W1A-020
CORRESPONDENCE (W1B)
Social Letters W1B-001 to W1B-015
Business Letters W1B-016 to W1B-030
PRINTED (W2) ACADEMIC WRITING (W2A)
Humanities W2A-001 to W2A-010
Social Sciences W2A-011 to W2A-020
Natural Sciences W2A-021 to W2A-030
Technology W2A-031 to W2A-040
NON-ACADEMIC WRITING (W2B)
Humanities W2B-001 to W2B-010
Social Sciences W2B-011 to W2B-020
Natural Sciences W2B-021 to W2B-030
Technology W2B-031 to W2B-040
REPORTAGE (W2C)
Press News Reports W2C-001 to W2C-020
INSTRUCTIONAL WRITING (W2D)
Administrative Writing W2D-001 to W2D-010
Skills & Hobbies W2D-011 to W2D-020
PERSUASIVE WRITING (W2E)
Press Editorials W2E-001 to W2E-010
CREATIVE WRITING (W2F)
Novels & Stories W2F-001 to W2F-020

If the fine-grained text categories are not needed, we can group them into macro-categories instead, such as spoken and written data. All spoken text categories begin with the letter S, all written ones with W.

1kwic_data <- kwic_data %>%
2  mutate(medium = if_else(
3    str_starts(text_category, "S"),
4    "spoken",
5    "written"
  ))

kwic_data %>%
  select(text_category, medium) %>%
  head()
1
Take the data frame kwic_data and pass it on to the next function via the pipe operator %>%. Store the result in kwic_data again.
2
Create a new column with mutate() and call it medium. The column values are assigned conditionally with if_else():
3
If text_category starts with the letter S, …
4
… classify medium as "spoken", …
5
… otherwise as "written".
# A tibble: 6 × 2
  text_category medium
  <chr>         <chr> 
1 S1A           spoken
2 S1A           spoken
3 S1A           spoken
4 S1A           spoken
5 S1A           spoken
6 S1A           spoken

To examine only a subset of your data (e.g., spoken texts), use filter() to select the relevant rows:

kwic_data_spoken <- filter(kwic_data, medium == "spoken")

nrow(kwic_data_spoken)
[1] 71

Where do I go from here?

As soon as you’ve fully annotated your dataset and reviewed it for potential coding errors, the next step involves analysing your data statistically to uncover potential patterns that are not visible to the naked eye. These patterns can include (subtle to major) differences in frequency of occurrence, relationships with other linguistically relevant features (e.g., register/genre) or probabilities (e.g., the probability that an object is omitted), among many others. Therefore, the following units will offer an introduction to statistical methods as they are typically applied in corpus linguistic research. A complete analysis of the eat data, from the research question to the write-up, is presented in 3.8 Case study.

References

Goldberg, Adele E. 2001. “Patient Arguments of Causative Verbs Can Be Omitted: The Role of Information Structure in Argument Distribution.” Language Sciences 23 (4): 503–24.
Gries, Stefan Thomas. 2013. Statistics for Linguistics with r: A Practical Introduction. 2nd rev. ed. Berlin; Boston: De Gruyter Mouton.
Gries, Stefan, and Sandra Deshors. 2014. “Using Regressions to Explore Deviations Between Interlanguage and Native Language.” Corpora 9: 109–36.
2.3 The CQP interface