始める無料で始める

さまざまな手法を試してみる

素晴らしいです。すでに複数の文字列距離の計算手法を学びましたね。どの手法を使うかは状況によって変わるため、各手法やそのパラメータを少しずつ試して慣れておくのが有効です。この演習では、検索語として誤記の "Marya Carey"(本来は "Mariah Carey")を使います。異なる文字列距離の手法では、この誤記は本来の名前とどの程度「近い」のでしょうか?

目標は、上記2つの名前の距離を小さくしつつ、検索対象ではない他の名前との距離は大きく保てるようなパラメータを見つけることです。

この演習はコースの一部です

Rで学ぶ中級正規表現

コースを見る

演習の手順

  • 部分文字列の長さを 12 にして q-gram を生成します。
  • q-gram 手法を使い、部分文字列の長さを 12 として searchnames の文字列距離を計算します。
  • "osa" 手法を使って searchnames の文字列距離を計算します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

search <- "Mariah Carey"
names <- c("M. Carey", "Mick Jagger", "Michael Jackson")

# Pass the values 1 and 2 as "q" and inspect the qgrams
qgrams("Mariah Carey", "M. Carey", q = ___)
qgrams("Mariah Carey", "M. Carey", q = ___)

# Try the qgram method on the variables search and names
stringdist(___, ___, method = "___", q = 1)
stringdist(___, ___, method = "___", q = 2)

# Try the default method (osa) on the same input and compare
stringdist(___, ___, method = "___")
コードを編集して実行