These slides are best viewed on a modern browser like Google Chrome on a desktop or laptop. You can navigate the slides using the left and right arrows.
What is text analysis?
Process of extracting meaningful information from unstructured text
Applications can include:
Sentiment analysis
Topic modelling
Named entity recognition
Text classification
Text summarisation
Common preprocessing steps for text data
Clean text by removing special characters, extra white space, and punctuations
Converting text to lowercase
Stopword removal
Stemming and lemmatisation
Spelling correction
Tokenisation
Cleaning text with R
Helps to know how to use regular expressions!
library(stringr)text <-c("This is a sample text with some numbers 12345 and special characters !@#$%^&*()","Another example with extra spaces and punctuation...!!!")text |>str_to_lower() |>str_remove_all("[^[:alnum:][:space:]]") |>str_squish()
[1] "this is a sample text with some numbers 12345 and special characters"
[2] "another example with extra spaces and punctuation"
Stemming and lemmatisation
Stemming reduces words to their root form by often removing sufixes and prefixes
E.g., “running”, “runner”, “runs” to “run”
Stemming is typically fast but can produce non-words or inaccurate roots
Lemmatisation reduces words to their base or dictionary form
E.g. “running”, “ran” to “run” and “better” to “good”
Lemmatisation is more accurate but slower as it requires a dictionary or vocabulary
Process of splitting text into smaller units (tokens)
Tokens can be words, sentences or n-grams
library(tidytext)library(tidyverse)words <-data.frame(text =c("Once upon a time, in the Kingdom of Probabilia, lived Princess Percentia.","She wasn't like the other princesses, who dreamed of dancing and dashing princes.","Percentia adored numbers. While her sisters embroidered samplers, Percentia meticulously collected data on the local butterfly population, categorized cloud formations, and charted the daily rainfall." )) |>unnest_tokens(word, text, token ="words") words
word
1 once
2 upon
3 a
4 time
5 in
6 the
7 kingdom
8 of
9 probabilia
10 lived
11 princess
12 percentia
13 she
14 wasn't
15 like
16 the
17 other
18 princesses
19 who
20 dreamed
21 of
22 dancing
23 and
24 dashing
25 princes
26 percentia
27 adored
28 numbers
29 while
30 her
31 sisters
32 embroidered
33 samplers
34 percentia
35 meticulously
36 collected
37 data
38 on
39 the
40 local
41 butterfly
42 population
43 categorized
44 cloud
45 formations
46 and
47 charted
48 the
49 daily
50 rainfall
Word frequency analysis
words |>count(word, sort =TRUE)
word n
1 the 4
2 percentia 3
3 and 2
4 of 2
5 a 1
6 adored 1
7 butterfly 1
8 categorized 1
9 charted 1
10 cloud 1
11 collected 1
12 daily 1
13 dancing 1
14 dashing 1
15 data 1
16 dreamed 1
17 embroidered 1
18 formations 1
19 her 1
20 in 1
21 kingdom 1
22 like 1
23 lived 1
24 local 1
25 meticulously 1
26 numbers 1
27 on 1
28 once 1
29 other 1
30 population 1
31 princes 1
32 princess 1
33 princesses 1
34 probabilia 1
35 rainfall 1
36 samplers 1
37 she 1
38 sisters 1
39 time 1
40 upon 1
41 wasn't 1
42 while 1
43 who 1