# jumanppをRから使う関数
# 以下の条件を満たすマシンでのみ動作する
# 1) reticulateパッケージを導入済み
# 2) juman++を導入済み
# 3) pythonを導入済み
# 4) pythonでrohknpを導入済み
# 一度に分析できる文字列の上限は1,500字くらい？

# メイン関数
jumany <- function(target, executable = "C:\\jumanpp\\jumanpp_V2.exe", conf = "C:\\jumanpp\\jumandic.conf"){
    knppy <- reticulate::import(module = "rhoknp")
    jumanpp <- knppy$Jumanpp(executable = executable, options = c("--config", conf))
    res <- jumanpp$apply(target)
    analyzedmat <- do.call("rbind", lapply(res$morphemes, function(x) getMrphl(x)))
    analyzeddat <- as.data.frame(analyzedmat)
    return(analyzeddat)
}


# jumanppの出力から形態素情報を取り出す関数
getMrphl <- function(res_part){
    smlist <- res_part$semantics
    morphv <- c("surface_form" = res_part$surf, 
            "reading_form" = res_part$reading, 
#            "text_form" = res_part$text, 
            "lemma" = res_part$lemma, 
            "part_of_speech" = res_part$pos, 
            "pos_division" = res_part$subpos, 
            "conjtype" = res_part$conjtype, 
            "conjform" = res_part$conjform, 
            "semantics" = paste(names(smlist), smlist, collapse = "; "), 
            "canon" = replace(res_part$canon, is.null(res_part$canon), NA))
    return(morphv)
}
