用 R 提取字符的子集形成一个单词
Extracting a subset of character form a word with R
我需要创建一个函数来提取和更改单词的 a 部分。它将Unicode转换为特定形式的UTF-8。
我的输入例如
word = "Aul<U+00E9>n"
我的输出是
f(word) = "Aul%c3%a9n"
我不知道如何 select 只有第一个单词中的 <U+00E9>
部分。
有人知道怎么做吗?
提前致谢 !
评论太长了,但我在上一条评论中的意思是:
你可以建立一个通信data.frame像:
corresp <- data.frame(uni=c("<U+00E9>", "U+00EC"), utf=c("%c3%a9", "%c3%ac"), stringsAsFactors=F)
然后你可以定义一个重新编码函数,例如喜欢:
recode <- function(word, corresp){
code <- sub("[^<]*(<U.+>)[^>]+", "\1", word)
m_code <- corresp$utf[corresp$uni==code]
return(sub(code, m_code, word))
}
所以:
recode("Aul<U+00E9>n", corresp)
#[1] "Aul%c3%a9n"
请在运行前install.packages("Unicode")
试一试。
word = "Aul<U+00E9>n"
start<-regexpr("<.*?>",word)
end<-a+attr(x = a,which = "match.length")
unipart<-Unicode::u_char_inspect(substr(word,start+3,end-2))[3]
paste(substr(word,1,start-1),paste("%",paste(iconv(unipart,toRaw = T)[[1]],collapse="%"),sep=""),substr(word,end,nchar(word)),sep = "")
>[1] "Aul%c3%a9n"
我需要创建一个函数来提取和更改单词的 a 部分。它将Unicode转换为特定形式的UTF-8。
我的输入例如
word = "Aul<U+00E9>n"
我的输出是
f(word) = "Aul%c3%a9n"
我不知道如何 select 只有第一个单词中的 <U+00E9>
部分。
有人知道怎么做吗? 提前致谢 !
评论太长了,但我在上一条评论中的意思是:
你可以建立一个通信data.frame像:
corresp <- data.frame(uni=c("<U+00E9>", "U+00EC"), utf=c("%c3%a9", "%c3%ac"), stringsAsFactors=F)
然后你可以定义一个重新编码函数,例如喜欢:
recode <- function(word, corresp){
code <- sub("[^<]*(<U.+>)[^>]+", "\1", word)
m_code <- corresp$utf[corresp$uni==code]
return(sub(code, m_code, word))
}
所以:
recode("Aul<U+00E9>n", corresp)
#[1] "Aul%c3%a9n"
请在运行前install.packages("Unicode")
试一试。
word = "Aul<U+00E9>n"
start<-regexpr("<.*?>",word)
end<-a+attr(x = a,which = "match.length")
unipart<-Unicode::u_char_inspect(substr(word,start+3,end-2))[3]
paste(substr(word,1,start-1),paste("%",paste(iconv(unipart,toRaw = T)[[1]],collapse="%"),sep=""),substr(word,end,nchar(word)),sep = "")
>[1] "Aul%c3%a9n"