Rで解析:wordleであそびましょっ!!「wordler」パッケージ

5文字の単語のアルファベットを推測するゲーム「wordle」をRのコンソールで遊ぶことができるパッケージの紹介です。「wordle」は最大で6回の実行で、アルファベットの入力のたびに出される「緑色はアルファベットと位置が正解」、「黄色はアルファベットが正解、位置が不正解」、「色なしはアルファベットと位置が不正解」のヒントから正解の単語を推測するゲームです。

紹介では「wordle」の開始コマンドとpokemon GOが楽しいのでポケモンWiki「http://wiki.xn--rckteqa2e.com/」からポケモン名を取得して英語のポケモン名のwordleゲームを実行するコマンドを紹介します。

なお、コマンド例内でも記載していますが、5文字以上のポケモン名は先頭5文字を切り出し、5文字未満は「X」を追加して5文字の長さに調整しています。そのため、ポケモン名が一部重複しています。

パッケージのバージョンは0.3.1。実行コマンドはwindows 11のR version 4.1.2で確認しています。

パッケージのインストール

下記コマンドを実行してください。

#パッケージのインストール
install.packages("wordler")

実行コマンド

詳細はコメント、パッケージヘルプを確認してください。

#パッケージの読み込み
library("wordler")

#Wordleの開始:play_wordlerコマンド
#単語を最大で6回で推測する
#緑色はアルファベットと位置が正解
#黄色はアルファベットが正解,位置が不正解
#色なしはアルファベットと位置が不正解
play_wordler()

##############################
###参考:ポケモンのwordleゲーム#####
#データの作成
#rvestパッケージがなければインストール
#htmlやxmlデータの編集に便利です
if(!require("rvest", quietly = TRUE)){
  install.packages("rvest");require("rvest")
}

#tidyverseパッケージがなければインストール
if(!require("tidyverse", quietly = TRUE)){
  install.packages("tidyverse");require("tidyverse")
}

#文字化け防止のおまじない
Sys.setlocale("LC_ALL", "English")

#ポケモンWikiからデータを取得
#http://wiki.xn--rckteqa2e.com/
Pokehtml <- read_html("https://wiki.xn--rckteqa2e.com/wiki/%E3%83%9D%E3%82%B1%E3%83%A2%E3%83%B3%E3%81%AE%E5%A4%96%E5%9B%BD%E8%AA%9E%E5%90%8D%E4%B8%80%E8%A6%A7")

#&#21462;&#24471;&#12487;&#12540;&#12479;&#12398;&#21152;&#24037;
Pokehtml %>% 
  #table&#12494;&#12540;&#12489;&#12434;&#25277;&#20986;
  html_nodes("table") %>% 
  #table&#12494;&#12540;&#12489;&#12398;&#12486;&#12540;&#12502;&#12523;&#21270;
  html_table() %>%
  #&#20840;&#12487;&#12540;&#12479;&#12434;&#32080;&#21512;
  bind_rows() %>%
  #&#21015;&#21517;&#12434;&#22793;&#26356;
  rename(!!!setNames(names(.), c("No", "Japanese", "English", "German",
                                 "French", "Korean", "Chinese (Simplified)",
                                 "Chinese (Traditional)"))) %>%
  #English&#12398;&#20808;&#38957;&#23567;&#25991;&#23383;&#12391;&#20999;&#12426;&#20986;&#12375;
  mutate(Judge = str_trunc(English, width = 5,
                             side = "right", ellipsis = "")) %>%
  #Judge&#12391;5&#25991;&#23383;&#20197;&#19979;&#12434;&#26411;&#23614;&#12395;&#31354;&#30333;&#12434;&#36275;&#12375;&#12390;5&#25991;&#23383;&#12395;&#12377;&#12427;
  mutate(Judge = str_pad(Judge, width = 5, pad = "X", side = "right")) %>%
  ###&#12362;&#12414;&#12369;#####
  #English&#12391;&#25991;&#23383;&#25968;&#12364;5&#12434;TRUE
  #mutate(Judge = ifelse(str_length(English) == 5,
  #                      TRUE, FALSE)) %>%
  #Judg&#12487;&#12540;&#12479;&#12398;TRUE&#12434;&#25277;&#20986;
  #filter(Judge == TRUE) %>%
  #####
  #Judge&#12487;&#12540;&#12479;&#12398;&#22823;&#25991;&#23383;&#21270;
  mutate(Judge = str_to_upper(Judge)) -> PokeDATA

#&#12509;&#12465;&#12514;&#12531;_wordle&#12398;&#38283;&#22987;
play_wordler(target_words = PokeDATA[["Judge"]],
             allowed_words = PokeDATA[["Judge"]])

出力例

・ポケモン_wordleの例


少しでも、あなたの解析が楽になりますように!!

Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.
タイトルとURLをコピーしました