で、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で動作することを追加する必要があります。