RmecabKo is a Korean text-analysis layer on top of the
MeCab morphological analyzer. The heavy lifting - the
native engine and dictionary compilation - comes from
RcppMeCab; RmecabKo adds tokenizers that
follow the tokenizers contract, curated Korean data,
user-dictionary tools, and a handful of analysis helpers. This vignette
walks through a full tidy workflow.
Install the engine and a Korean dictionary once per machine:
text_normalize() is a pure, dependency-light cleanup
step. It composes Unicode (NFC), folds full-width characters, and
squashes repeated characters - all of which help the analyzer and
downstream matching. It needs no backend:
The tokenizers take a character vector and return a list of character
vectors, so they slot directly into
tidytext::unnest_tokens(). The package ships a small
demonstration corpus, demo_ko:
corpus <- tibble(doc = names(demo_ko), text = demo_ko)
tokens <- corpus |>
unnest_tokens(word, text, token = token_nouns)
head(tokens, 8)
#> # A tibble: 8 × 2
#> doc word
#> <chr> <chr>
#> 1 d1 한국어
#> 2 d1 형태소
#> 3 d1 분석
#> 4 d1 텍스트
#> 5 d1 마이닝
#> 6 d1 걸음
#> 7 d2 날씨
#> 8 d2 공원Even without a working backend, the pre-computed tokenization bundled with the package lets us continue:
stopwords_ko is a curated table of Korean function
morphemes. Filter by surface form with an anti_join(), or
strip whole part-of-speech classes at the tag level with
drop_pos:
With a document column in hand, tidytext::bind_tf_idf()
gives per-document keyword weights; keywords_tfidf() offers
the same without the tidy detour:
tidy_tokens |>
count(doc, word) |>
bind_tf_idf(word, doc, n) |>
arrange(desc(tf_idf)) |>
head(6)
#> # A tibble: 6 × 6
#> doc word n tf idf tf_idf
#> <chr> <chr> <int> <dbl> <dbl> <dbl>
#> 1 d2 공원 1 0.5 2.30 1.15
#> 2 d2 날씨 1 0.5 2.30 1.15
#> 3 d9 아이 1 0.5 2.30 1.15
#> 4 d9 운동장 1 0.5 2.30 1.15
#> 5 d10 글 1 0.333 2.30 0.768
#> 6 d10 마음 1 0.333 2.30 0.768keywords_tfidf(demo_ko, div = "nouns", top_n = 2) |> head(6)
#> doc word n tf idf tf_idf
#> 1 d1 걸음 1 0.1666667 2.302585 0.3837642
#> 2 d1 마이닝 1 0.1666667 2.302585 0.3837642
#> 3 d2 공원 1 0.5000000 2.302585 1.1512925
#> 4 d2 날씨 1 0.5000000 2.302585 1.1512925
#> 5 d3 도움 1 0.2000000 2.302585 0.4605170
#> 6 d3 자연어 1 0.2000000 2.302585 0.4605170lexicon_knu() downloads and caches the KNU Korean
sentiment lexicon (polarity from -2 to 2). Joining it against tokens
yields a per-document sentiment score. The lexicon is distributed under
CC BY-NC-SA (Kyungpook National University), so it is fetched on demand
rather than bundled; note the NonCommercial clause and review its terms
before use.
Morpheme n-grams never bridge a removed stopword:
token_ngrams(demo_ko[[1]], n = 2, div = "nouns", simplify = TRUE)
#> [1] "한국어 형태소" "형태소 분석" "분석 텍스트" "텍스트 마이닝"
#> [5] "마이닝 걸음"token_lemma() recovers the dictionary form of
predicates, which keeps inflected verbs and adjectives from scattering
in a frequency count:
kwic() shows a keyword in its morpheme context: