Usage of COTOHA API client

Getting ready to use client

First of all, you should set COTOHA_ID and COTOHA_SECRET as environment vars. Run Sys.setenv(COTOHA_ID = "Your Client ID") and Sys.setenv(COTOHA_SECRET = "Your Client secret"), then try snippets below.

# Never forget to set `Access Token Publish URL`
PUB_URL <- Sys.getenv("PUB_URL")
my_token <- conifer::getAccessToken(PUB_URL)
client <- conifer::cotoha(my_token)

data(yamanashi)

Parsing dependency structure

res <- client$parse(yamanashi[13])
conifer::pretty_chunks(res)
#> # A tibble: 11 x 10
#>    chunk_id  head dep   chunk_head chunk_func token_id form       pos   features
#>       <int> <int> <chr>      <int>      <int>    <int> <chr>      <chr> <chr>   
#>  1        0     3 D              3          1        0 『         括弧  開括弧  
#>  2        0     3 D              3          1        1 それなら   接続~ <NA>    
#>  3        1     3 D              3          0        2 なぜ       連用~ <NA>    
#>  4        2     3 D              3          1        3 クラムボン 名詞  <NA>    
#>  5        2     3 D              3          1        4 は         連用~ <NA>    
#>  6        3    -1 O             -1          3        5 わら       動詞~ W       
#>  7        3    -1 O             -1          3        6 っ         動詞~ <NA>    
#>  8        3    -1 O             -1          3        7 た         動詞~ 終止    
#>  9        3    -1 O             -1          3        8 の         終助~ <NA>    
#> 10        3    -1 O             -1          3        9 。         句点  <NA>    
#> 11        3    -1 O             -1          3       10 』         括弧  閉括弧  
#> # ... with 1 more variable: attributes <chr>

Named entity extraction

print(yamanashi[68])
#> [1] "『もうねろねろ。遅いぞ、あしたイサドへ連れて行かんぞ。』"
client$ne(yamanashi[68])
#> [[1]]
#> [[1]]$begin_pos
#> [1] 12
#> 
#> [[1]]$end_pos
#> [1] 18
#> 
#> [[1]]$form
#> [1] "あしたイサド"
#> 
#> [[1]]$std_form
#> [1] "あしたイサド"
#> 
#> [[1]]$class
#> [1] "ORG"
#> 
#> [[1]]$extended_class
#> [1] "Organization"
#> 
#> [[1]]$source
#> [1] "basic"

Reference resolution

res <- client$coreference("太郎は友人です。彼は焼き肉を食べた。")
res$coreference
#> [[1]]
#> [[1]]$representative_id
#> [1] 0
#> 
#> [[1]]$referents
#> [[1]]$referents[[1]]
#> [[1]]$referents[[1]]$referent_id
#> [1] 0
#> 
#> [[1]]$referents[[1]]$sentence_id
#> [1] 0
#> 
#> [[1]]$referents[[1]]$token_id_from
#> [1] 0
#> 
#> [[1]]$referents[[1]]$token_id_to
#> [1] 0
#> 
#> [[1]]$referents[[1]]$form
#> [1] "太郎"
#> 
#> 
#> [[1]]$referents[[2]]
#> [[1]]$referents[[2]]$referent_id
#> [1] 1
#> 
#> [[1]]$referents[[2]]$sentence_id
#> [1] 0
#> 
#> [[1]]$referents[[2]]$token_id_from
#> [1] 5
#> 
#> [[1]]$referents[[2]]$token_id_to
#> [1] 5
#> 
#> [[1]]$referents[[2]]$form
#> [1] "彼"

Keyword extraction

print(yamanashi[57])
#> [1] " そのつめたい水の底まで、ラムネの瓶の月光がいっぱいに透とおり天井では波が青じろい火を、燃したり消したりしているよう、あたりはしんとして、ただいかにも遠くからというように、その波の音がひびいて来るだけです。"
res <- client$keyword(yamanashi[57])
res
#> [[1]]
#> [[1]]$form
#> [1] "あたり"
#> 
#> [[1]]$score
#> [1] 20
#> 
#> 
#> [[2]]
#> [[2]]$form
#> [1] "月光"
#> 
#> [[2]]$score
#> [1] 10.7904
#> 
#> 
#> [[3]]
#> [[3]]$form
#> [1] "遠く"
#> 
#> [[3]]$score
#> [1] 10.6362
#> 
#> 
#> [[4]]
#> [[4]]$form
#> [1] "しん"
#> 
#> [[4]]$score
#> [1] 10
#> 
#> 
#> [[5]]
#> [[5]]$form
#> [1] "いっぱい"
#> 
#> [[5]]$score
#> [1] 10

Calculating similality between sentences

print(c(yamanashi[11], yamanashi[12]))
#> [1] "『クラムボンはわらっていたよ。』"    
#> [2] "『クラムボンはかぷかぷわらったよ。』"
client$similarity(yamanashi[11], yamanashi[12])
#> $score
#> [1] 1

Linguistic modality detection

print(c(yamanashi[12], yamanashi[13]))
#> [1] "『クラムボンはかぷかぷわらったよ。』"    
#> [2] "『それならなぜクラムボンはわらったの。』"
client$sentence_type(yamanashi[12])
#> $modality
#> [1] "declarative"
#> 
#> $dialog_act
#> $dialog_act[[1]]
#> [1] "information-providing"
client$sentence_type(yamanashi[13])
#> $modality
#> [1] "interrogative"
#> 
#> $dialog_act
#> $dialog_act[[1]]
#> [1] "information-seeking"

Speaker’s characteristics recognition

print(c(yamanashi[29], yamanashi[40]))
#> [1] "『お魚はなぜああ行ったり来たりするの。』"        
#> [2] "『どうしたい。ぶるぶるふるえているじゃないか。』"
client$user_attribute(yamanashi[29])
#> $age
#> [1] "60歳以上"
#> 
#> $hobby
#> $hobby[[1]]
#> [1] "ANIMAL"
#> 
#> $hobby[[2]]
#> [1] "COOKING"
#> 
#> $hobby[[3]]
#> [1] "FISHING"
#> 
#> $hobby[[4]]
#> [1] "FORTUNE"
#> 
#> $hobby[[5]]
#> [1] "INTERNET"
#> 
#> $hobby[[6]]
#> [1] "TVGAME"
client$user_attribute(yamanashi[40])
#> $age
#> [1] "40-49歳"
#> 
#> $civilstatus
#> [1] "未婚"
#> 
#> $earnings
#> [1] "-1M"
#> 
#> $hobby
#> $hobby[[1]]
#> [1] "INTERNET"
#> 
#> $hobby[[2]]
#> [1] "MOVIE"
#> 
#> 
#> $location
#> [1] "関東"
#> 
#> $moving
#> $moving[[1]]
#> [1] "RAILWAY"
#> 
#> $moving[[2]]
#> [1] "WALKING"
#> 
#> 
#> $occupation
#> [1] "会社員"

Removing fillers of sentence

client$remove_filler("えーーっと、あの、今日の打ち合わせでしたっけ。すみません、ちょっと、急用が入ってしまって。")
#> [[1]]
#> [[1]]$fillers
#> [[1]]$fillers[[1]]
#> [[1]]$fillers[[1]]$begin_pos
#> [1] 0
#> 
#> [[1]]$fillers[[1]]$end_pos
#> [1] 5
#> 
#> [[1]]$fillers[[1]]$form
#> [1] "えーっと、"
#> 
#> 
#> [[1]]$fillers[[2]]
#> [[1]]$fillers[[2]]$begin_pos
#> [1] 5
#> 
#> [[1]]$fillers[[2]]$end_pos
#> [1] 7
#> 
#> [[1]]$fillers[[2]]$form
#> [1] "あの"
#> 
#> 
#> [[1]]$fillers[[3]]
#> [[1]]$fillers[[3]]$begin_pos
#> [1] 28
#> 
#> [[1]]$fillers[[3]]$end_pos
#> [1] 32
#> 
#> [[1]]$fillers[[3]]$form
#> [1] "ちょっと"
#> 
#> 
#> 
#> [[1]]$normalized_sentence
#> [1] "えーっと、あの、今日の打ち合わせでしたっけ。すみません、ちょっと、急用が入ってしまって。"
#> 
#> [[1]]$fixed_sentence
#> [1] "、今日の打ち合わせでしたっけ。すみません、急用が入ってしまって。"

Detecting misrecognition of sentence

client$detect_misrecognition("温泉認識は誤りを起こす")
#> $score
#> [1] 0.9999969
#> 
#> $candidates
#> $candidates[[1]]
#> $candidates[[1]]$begin_pos
#> [1] 0
#> 
#> $candidates[[1]]$end_pos
#> [1] 2
#> 
#> $candidates[[1]]$form
#> [1] "温泉"
#> 
#> $candidates[[1]]$detect_score
#> [1] 0.9999969
#> 
#> $candidates[[1]]$correction
#> $candidates[[1]]$correction[[1]]
#> $candidates[[1]]$correction[[1]]$form
#> [1] "音声"
#> 
#> $candidates[[1]]$correction[[1]]$correct_score
#> [1] 0.7722404
#> 
#> 
#> $candidates[[1]]$correction[[2]]
#> $candidates[[1]]$correction[[2]]$form
#> [1] "厭戦"
#> 
#> $candidates[[1]]$correction[[2]]$correct_score
#> [1] 0.6619857
#> 
#> 
#> $candidates[[1]]$correction[[3]]
#> $candidates[[1]]$correction[[3]]$form
#> [1] "怨念"
#> 
#> $candidates[[1]]$correction[[3]]$correct_score
#> [1] 0.6554197
#> 
#> 
#> $candidates[[1]]$correction[[4]]
#> $candidates[[1]]$correction[[4]]$form
#> [1] "おんねん"
#> 
#> $candidates[[1]]$correction[[4]]$correct_score
#> [1] 0.6554197
#> 
#> 
#> $candidates[[1]]$correction[[5]]
#> $candidates[[1]]$correction[[5]]$form
#> [1] "モンセン"
#> 
#> $candidates[[1]]$correction[[5]]$correct_score
#> [1] 0.6544623

Sentiment analysis

print(c(yamanashi[49], yamanashi[50]))
#> [1] "『こわいよ、お父さん。』"                                                            
#> [2] "『いいいい、大丈夫だ。心配するな。そら、樺の花が流れて来た。ごらん、きれいだろう。』"
client$sentiment(yamanashi[49])
#> $sentiment
#> [1] "Negative"
#> 
#> $score
#> [1] 0.6772835
#> 
#> $emotional_phrase
#> $emotional_phrase[[1]]
#> $emotional_phrase[[1]]$form
#> [1] "こわいよ"
#> 
#> $emotional_phrase[[1]]$emotion
#> [1] "N"
client$sentiment(yamanashi[50])
#> $sentiment
#> [1] "Positive"
#> 
#> $score
#> [1] 0.7016856
#> 
#> $emotional_phrase
#> $emotional_phrase[[1]]
#> $emotional_phrase[[1]]$form
#> [1] "心配する"
#> 
#> $emotional_phrase[[1]]$emotion
#> [1] "不安"
#> 
#> 
#> $emotional_phrase[[2]]
#> $emotional_phrase[[2]]$form
#> [1] "大丈夫だ"
#> 
#> $emotional_phrase[[2]]$emotion
#> [1] "P"
#> 
#> 
#> $emotional_phrase[[3]]
#> $emotional_phrase[[3]]$form
#> [1] "きれいだろう"
#> 
#> $emotional_phrase[[3]]$emotion
#> [1] "P"