R 的自然語言處理入門
Kasey Jones
Research Data Scientist
NLP:
主題:
words <- c("DW-40", "Mike's Oil", "5w30", "Joe's Gas", "Unleaded", "Plus-89")
# Finding Digits
grep("\\d", words, value = TRUE)
[1] 1 3 6
# Finding Apostrophes
grep("\\'", words, value = TRUE)
[1] "Mike's Oil" "Joe's Gasoline"
| Pattern | Text Matches | R Example | Text Example |
|---|---|---|---|
| \w | 任一英數字元 | gregexpr(pattern ='\w', <text>) | a |
| \d | 任一數字 | gregexpr(pattern ='\d', text) | 1 |
| \w+ | 任意長度英數字串 | gregexpr(pattern ='\w+', text) | word |
| \d+ | 任意長度數字 | gregexpr(pattern ='\d+', text) | 1234 |
| \s | 空白字元 | gregexpr(pattern ='\s', text) | ' ' |
| \S | 非空白字元 | gregexpr(pattern ='\S', text) | word |
| Function | Purpose | Syntax |
|---|---|---|
| grep | 在向量中尋找樣式的相符項 | grep(pattern ='\w', x = <vector>, value = F) |
| gsub | 取代字串/向量中所有相符項 | gsub(pattern ='\d+', replacement = "", x = <vector>) |
R 的自然語言處理入門