Rで学ぶ自然言語処理入門
Kasey Jones
Research Data Scientist
NLP:
扱うトピック:
words <- c("DW-40", "Mike's Oil", "5w30", "Joe's Gas", "Unleaded", "Plus-89")
# 数字を探す
grep("\\d", words, value = TRUE)
[1] 1 3 6
# アポストロフィを探す
grep("\\'", words, value = TRUE)
[1] "Mike's Oil" "Joe's Gasoline"
| パターン | 一致するテキスト | R の例 | 例文 |
|---|---|---|---|
| \w | 英数字1文字 | gregexpr(pattern ='\w', <text>) | a |
| \d | 数字1文字 | gregexpr(pattern ='\d', text) | 1 |
| \w+ | 任意長の英数字 | gregexpr(pattern ='\w+', text) | word |
| \d+ | 任意長の数字 | gregexpr(pattern ='\d+', text) | 1234 |
| \s | 空白 | gregexpr(pattern ='\s', text) | ' ' |
| \S | 非空白1文字 | gregexpr(pattern ='\S', text) | word |
| 関数 | 目的 | 構文 |
|---|---|---|
| grep | ベクター内でパターン一致を検索 | grep(pattern ='\w', x = <vector>, value = F) |
| gsub | 文字列/ベクター内の全一致を置換 | gsub(pattern ='\d+', replacement = "", x = <vector>) |
Rで学ぶ自然言語処理入門