我想使用这个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文档。为了解析它,我需要整个数据成为一个单一的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")
我可能也说,我已经尝试粘贴和折叠向量成一个单一的元素,但是这是非常脆弱的。它适用于虚拟数据,但不适用于较大的文件,也不适用于捷克文件(尽管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。也许这有些帮助,但绝对不够。
最后我应该补充一点,我在Windows 8 Professional上工作,运行R-3.2.4 64bit,RStudio版本为0.99.879。