2016-12-27 69 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文档。为了解析它,我需要整个数据成为一个单一的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。

回答

0

我应该使用RCurl::postForm而不是RCurl::getForm,所有其他参数保持不变。正如我错误地认为的那样,postForm函数不仅可用于在服务器上写入文件。 它不会对要处理的数据的大小施加严格的限制,因为与postForm的数据不会成为URL的一部分,与getForm不同。

这是我的方便功能(需要RCurl,stringi,stringr,magrittr):

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) 
    }