2016-12-27 4 views
0

で、APIへの入力テキストファイルに:どのように私はこのAPIを使用したいR

http(s)://lindat.mff.cuni.cz/services/morphodita/api/ 

方法で「タグ」。それは、私のテキスト入力をタグ付けして字句を付けるでしょう。テキスト文字列(下を参照)でうまくいきましたが、ファイル全体をAPIに送る必要があります。

入力が正常に動作としてだけ、その文字列を表示する:

method <- "tag" 
    lemmatized_text <- RCurl::getForm(paste# 
    ("http://lindat.mff.cuni.cz/services/morphodita/api/", method, sep = ""), 
    .params = list(data = "Peter likes cakes. John likes lollypops.",# 
    output = "json", model = "english-morphium-wsj-140407-no_negation"), # 
method = method) 

これは、 - 結果 - 正しい:の行に対応する要素を有するベクターを用いて文字列を置換し、ただし

[1] "{\n \"model\": \"english-morphium-wsj-140407-no_negation\",\n 
\"acknowledgements\": [\n \"http://ufal.mff.cuni.cz 
/morphodita#morphodita_acknowledgements\",\n \"http://ufal.mff.cuni.cz 
/morphodita/users-manual#english-morphium-wsj_acknowledgements\"\n ],\n 
\"result\": [[{\"token\":\"Peter\",\"lemma\":\"Peter\",\"tag\":\"NNP 
\",\"space\":\" \"},{\"token\":\"likes\",\"lemma\":\"like\",\"tag\":\"VBZ 
\",\"space\":\" \"},{\"token\":\"cakes\",\"lemma\":\"cake\",\"tag\":\"NNS 

[truncated by me]    

APIは入力時に文字列を必要とするため、テキストファイルは機能しません。一つだけ、デフォルトでは最初、ベクトル要素が処理されることになります。

method <- "tag" 
    mydata <- c("cakes.", "lollypops") 
    lemmatized_text <- RCurl::getForm(paste("http://lindat.mff.cuni.cz 
    /services/morphodita/api/", method, sep = ""), 
    .params = list(data = mydata, output = "json", 
    model = "english-morphium-wsj-140407-no_negation")) 

[1] "{\n \"model\": \"english-morphium-wsj-140407-no_negation\",\n 
     [truncated by me] 
    \"result\": [[{\"token\":\"cakes\",\"lemma\":\"cake\",\"tag\":\"NNS 
    \"},{\"token\":\".\",\"lemma\":\".\",\"tag\":\".\"}]]\n}\n" 

この問題はsapplyと同時に、ベクトルの各要素にそのAPIを呼び出す機能が、それぞれの要素を軽減することができます結果のベクトルには独立したjsonドキュメントが含まれています。それを解析するには、データ全体を1つのjsonドキュメントにする必要があります。

は最終的に私はtextConnectionを試してみましたが、それは誤った出力が返されます。

mydata <- c("cakes.", "lollypops") 
    mycon <- textConnection(mydata, encoding = "UTF-8") 
    lemmatized_text <- RCurl::getForm(paste# 
    ("http://lindat.mff.cuni.cz/services/morphodita/api/", method,# 
    sep = ""), .params = list(data = mycon, output = "json",# 
    model = "english-morphium-wsj-140407-no_negation")) 

[1] "{\n \"model\": \"english-morphium-wsj-140407-no_negation\",\n 
\"acknowledgements\": [\n \"http://ufal.mff.cuni.cz 
/morphodita#morphodita_acknowledgements\",\n \"http://ufal.mff.cuni.cz 
/morphodita/users-manual#english-morphium-wsj_acknowledgements\"\n ],\n 
\"result\": [[{\"token\":\"5\",\"lemma\":\"5\",\"tag\":\"CD\"}]]\n}\n" 
attr(,"Content-Type") 

を私はおそらくも、私はすでに1つの要素にベクトルを貼り付け、崩壊しようとしたと言うべきで、それは非常に壊れやすいです。これはダミーデータでは機能しますが、大きなファイルでは動作せず、チェコ語のファイルでは動作しません(UTF-8でエンコードされています)。 APIは厳密にUTF-8でエンコードされたデータを必要とします。したがって私はエンコードの問題を疑う。私は、このファイルを試してみました:

mydata <- RCurl::getURI("https://ia902606.us.archive.org/4/items/maidmarian00966gut/maidm10.txt", .opts = list(.encoding = "UTF-8")) 

をし、それが

Error: Bad Request 

を言ったが、私はわずか数行を使用する場合、それは突然働きました。また、MacIntoshからWindowsに改行したファイルのローカルコピーを作成しました。たぶんこれは少し助けたが、間違いなく十分だった。

最終的に私はRstudioバージョン0.99.879で、R-3.2.4 64ビットを実行しているWindows 8 Professionalで動作することを追加する必要があります。

答えて

0

RCurl::getFormの代わりにRCurl::postFormを使用し、他のすべての引き数を同じにしてください。 postForm関数は、私が間違って信じていたように、サーバー上にファイルを書き込むためだけに使うことはできません。 getFormとは異なり、postFormでデータがURLの一部にならないため、処理するデータのサイズには厳密な制限はありません。

process_w_morphodita <- function(method, data, output = "json", model 
    = "czech-morfflex-pdt-161115", guesser = "yes",...){ 
    # for formally optional but very important argument-value pairs see 
    MorphoDiTa REST API reference at 
    # http://lindat.mff.cuni.cz/services/morphodita/api-reference.php 
    pokus <- RCurl::postForm(paste("http://lindat.mff.cuni.cz/services 
    /morphodita/api/", method, sep = ""), .params = list(data = 
    stringi::stri_enc_toutf8(data), output = output, model = model, 
    guesser = guesser,...)) 
    if (output == "vertical") { 
     pokus <- pokus %>% stringr::str_trim(side = "both") %>% 
     stringr::str_conv("UTF-8") %>% stringr::str_replace_all(pattern = 
     "\\\\t", replacement = "\t") %>% stringr::str_replace_all(pattern = 
     "\\\\n", replacement = "\n") # look for four backslashes, replace 
     with one backslash to get vertical format in text file 
     } 
    return(pokus) 
    } 

これは私の便利な関数ですが(RCurl、stringi、stringr、magrittrが必要です)

関連する問題