Skip to content

Instantly share code, notes, and snippets.

@tetlabo
Created March 5, 2023 09:12
Show Gist options
  • Save tetlabo/ec2f6d884d17cfbad9e6ed274152caea to your computer and use it in GitHub Desktop.
Save tetlabo/ec2f6d884d17cfbad9e6ed274152caea to your computer and use it in GitHub Desktop.
文字列の画数を取得するプログラム
library(stringi)
library(httr)
library(jsonlite)
names <- c("白石麻衣", "生田絵梨花", "齋藤飛鳥")
for (i in 1:length(names)){
chars <- unlist(strsplit(names[i], split = ""))
for (j in 1:length(chars)){
chars_ucs <- stri_escape_unicode(chars[j])
char_ucs_fixed <- gsub("\\\\u", "0x", chars_ucs)
res <- GET(paste0("https://mojikiban.ipa.go.jp/mji/q?UCS=", char_ucs_fixed))
res_json <- content(res, as = "text")
res_df <- fromJSON(res_json)
res_df <- subset(res_df[["results"]], 入管正字コード != "")
final_res <- res_df[["総画数"]]
print(paste(chars[j], char_ucs_fixed, final_res, sep = ": "))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment