‘bibファイルからサマリーの和訳まで(1)’ で作ったリストを読み込んで, アブストラクトの和訳付きデータにする。

データの読み込み

library(openxlsx)
eric.psyc.nodp <- read.xlsx("../Data/eric_psyc_nodp.xlsx")

アブストの文字数を数えてみる(50万字を超えると困る)

sum(nchar(eric.psyc.nodp$abstract))
## [1] 479762

有料だな・・・
とりあえず,お試しから。

アブストの和訳のお試し

Deepl APIの情報(非表示)

library(dplyr)
library(tidyverse)
library(deeplr)

動作確認

# available_languages2(auth_key = api)

言語の指定

source_lang <- "EN"
target_lang <- "JA"

関数
- https://note.com/text_tier2718/n/n3451567126a7 に載っている関数を使う

deepL <- function(Sentence, source_lang = "EN", target_lang = "JA", api_key = api_key) {
 a <- system(
   paste0(
     'curl -s https://api-free.deepl.com/v2/translate -d "auth_key=',
     api_key,
     '" -d "text=',
     str_replace_all(Sentence, pattern = '"', replacement = "'"), # 翻訳文中に""があるとpaste0と干渉してエラーを起こすので''に変換
     '" -d source_lang="',
     source_lang,
     '" -d "target_lang=',
     target_lang,
     '"'
   ),
   intern = T
 )
 b <- strsplit(
   strsplit(
     as.character(a),
     '\"text\":\"'
   )[[1]][2],
   '\"}]}'
 )[[1]][1]
 Sys.sleep(1)

 return(b)
}

自動翻訳
お試しデータの作成

otameshi <- eric.psyc.nodp[234:254,]
translation <- map(otameshi$abstract, function(x) {
 deepL(
   Sentence = x,
   source_lang = source_lang,
   target_lang = target_lang,
   api_key = api
 )
})
otameshi.ja <- mutate(otameshi, abstract_ja = translation)

library(DT)
datatable(otameshi.ja, options = list(pageLength = 20))
library(openxlsx)
write.xlsx(otameshi.ja, "../Data/otameshi.ja.xlsx")