使用 R 将 URI 转换为 IRI?

Convert URI to IRI using R?

我正在使用 R 创建 RDF 关联数据。现在我有这样的 URI:

test:Value_ONE%20OR%20TWO 

我反而想使用正确的编码创建 IRI。此处描述了 URI 到 IRI 的转换:

https://www.w3.org/International/iri-edit/draft-duerst-iri.html#URItoIRI

有人可以用示例 R 代码指导我将百分比编码的 URI 转换为 IRI 吗?

您将不得不尝试一下逻辑,但以下内容适用于您发送的 link 中的第一个示例。幸运的是,大部分转换可以在基 R 中完成。我添加了 tidyverse 只是为了建议计算方法。

Map 只是 tidyverse 版本的 apply 系列,它遍历列表或向量。 map_int/map_chr可以用sapply代替,map/map2可以用lapply代替。当您想在 R 中进行字符串操作(提取和替换)时,stringr 是您最好的朋友:

library(tidyverse)

testURI = 'http://www.example.org/D%C3%BCrst'
#testURI = 'test:Value_ONE%20OR%20TWO'

########################################
# extract any pattern that matches %\w\w
# "\w" is a regex representation for any character
# a "\" must be prepended to the regex in R
########################################

extractPerc <- testURI %>%
  str_extract_all(regex('(%\w{2})+')) %>%
  unlist()

extractPercDecoded <- map_chr(extractPerc, URLdecode)

extractPercInt <- map_int(extractPercDecoded, utf8ToInt)

############################################
# Keep as a list so the Hex code isn't converted to it's
# character representation or it's numeric default
############################################

extractPercHex <- map(extractPercInt, as.hexmode)


#####################################################
# iterate over the string and replace the %s with the hexs
# There's definitely a better way to replace the %-html representation
# with the hex representation, but I can't quite figure it out
####################################################

newURI = testURI

map2(extractPerc, extractPercHex, function(x, y){
  newURI <<- str_replace(newURI, 
                         x, 
                         str_c('&#x', y, ';')) 
  })

newURI