Xử lý chuỗi
a <- "learning by doing"
substr (a,
start = 2 ,
stop = 5 )
# lấy ký tự từ trái qua phải
substr (a,
start = 2 ,
stop = 5 )
# lấy từ ký tự vị trí số 2 trở đi
substring (a,
first = 2 )
# lấy từ phải qua trái
library (stringr)
# lấy ký tự từ trái qua phải
stringr::: str_sub (a,
start = 2 ,
end = 5 )
# bắt đầu với 5 ký tự cuối
stringr::: str_sub (a,
start = - 5 )
# lấy toàn bộ ký tự (bỏ 5 ký tự cuối)
stringr::: str_sub (a,
end = - 5 )
# lấy từ ở vị trí thứ 2
stringr::: word (a,
start = 2 )
# lấy từ cuối (bên phải qua)
stringr::: word (a,
start = - 1 )
https://www.epirhandbook.com/en/new_pages/characters_strings.html
Xử lý sở hữu cách
Thực tế là ta sẽ loại bỏ dấu 's.
v <- c ("electricity's" , "it's" , "what's" )
clean <- gsub (pattern = "'s$" , # àm sạch ok
replacement = "" ,
x = v)
clean
[1] "electricity" "it" "what"
v <- c ("electricity's" , "it's" , "what's" )
clean <- gsub (pattern = "'s \\ b" ,
replacement = "" ,
x = v)
clean
[1] "electricity" "it" "what"
text <- c ("electricity's impact on people's life's" ,
"it's is a good idea" ,
"what's" ,
"electricity's" )
clean_text <- gsub (pattern = "'s \\ b" , # cú pháp này mạnh hơn
replacement = "" ,
x = text)
clean_text
[1] "electricity impact on people life" "it is a good idea"
[3] "what" "electricity"
library (stringr)
clean <- stringr::: str_remove (string = v,
pattern = "'s$" )
clean
[1] "electricity" "it" "what"
clean <- stringr::: str_remove (string = v,
pattern = "'s \\ b" )
clean
[1] "electricity" "it" "what"
# lệnh này lọc còn sót chữ [people's], [life's]
clean <- stringr::: str_remove (string = text,
pattern = "'s \\ b" )
clean
[1] "electricity impact on people's life's"
[2] "it is a good idea"
[3] "what"
[4] "electricity"
Quy trình clean text
chuyển về cùng upper hoặc lower case
clean thô: dùng grep, gsub, which để fix từ
clean ký tự unicode
clean dấu quote
rã token
clean stop word (có thể add thêm word cần loại)
clean synonym
gộp theo topic
xử lý miss spelling
Sắp xếp thứ tự vector này theo thứ tự vector kia
a <- c ("ok" , "yes" , "happy" , "well" , "right" , "thanks" )
b <- c ("right" , "thanks" , "ok" , "yes" , "well" , "happy" )
b_sorted_1 <- b[match (a, b)]
b_sorted_2 <- b[order (match (b, a))]
identical (a, b)
Ghi chữ superscript unicode
https://wumbo.net/symbols/superscript-two/
Lưu ý khi xếp thứ tự
v <- c ("Châu Thành" , "Cái Răng" )
# Khi có dấu
"Châu Thành" > "Cái Răng"
"Châu Thành" < "Cái Răng"
# Khi không dấu
"Chau Thanh" > "Cái Răng"
"Chau Thanh" < "Cai Rang"
library (stringi)
f1 <- stri_trans_general (str = "Châu Thành" , id = "Latin-ASCII" )
f1
f2 <- stri_trans_general (str = "Cái Răng" , id = "Latin-ASCII" )
f2
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
df_v <- as.data.frame (v)
df_v
v
1 Châu Thành
2 Cái Răng
df_v |> dplyr::: arrange (v)
v
1 Châu Thành
2 Cái Răng
df_g <- as.data.frame (g)
df_g
g
1 Chau Thanh
2 Cai Rang
df_g |> dplyr::: arrange (g) # function này đảo lại vị trí khi có không còn dấu unicode
g
1 Cai Rang
2 Chau Thanh
Kinh nghiệm rút ra: để sắp xếp alphabet chuẩn, ta loại bỏ dấu của từ, rồi mới arrange.