如何将全文更改为小写,但使用R将首字母缩略词保留为大写?我需要它来进行文本挖掘和使用udpi包。我当然可以使用大写字母,但无论如何在使用小写字母时仍保留大写字母的缩写?
tolower(“ NASA是一家美国公司”)。
tolower('NASA IS A US COMPANY')
tolower('NASA IS A US COMPANY')
Expected: NASA is a US company
Actual: nasa is a us company
答案 0 :(得分:3)
我们可以做到: 测试是输入:
paste(lapply(strsplit(test," "),function(x) ifelse(x %in% toupper(tm::stopwords()),
tolower(x),x))[[1]],collapse=" ")
[1] "NASA is a US COMPANY"
答案 1 :(得分:2)
我编辑了
Capitalize the first letter of both words in a two word string
一点点。
char[]
您必须管理一些厌烦症
getText()
之后,您可以在下面获得结果
simpleCap <- function(x,abr) {
s <- strsplit(x, " ")[[1]]
loc = which(!s %in% abr)
loc_abr = which(s %in% abr)
tmp_s = s[!s %in% abr]
paste(toupper(substring(tmp_s, 1,1)), tolower(substring(tmp_s, 2)),
sep="", collapse=" ")
result = character(length(s))
result[loc] = strsplit(paste(toupper(substring(tmp_s, 1,1)), tolower(substring(tmp_s, 2)),
sep="", collapse=" ")," ")[[1]]
result[loc_abr] = abr
result = paste(result,collapse = " ")
return(result)
}
答案 2 :(得分:0)
怎么样?
acronyms <- c('NASA','US')
test <- 'NASA IS A US COMPANY'
a <- tolower(test)
b <- as.list(strsplit(a, " ")[[1]])
for (i in 1:length(b)) {
if (toupper(b[i]) %in% acronyms) {
b[i] <- toupper(b[i])
}
}
c <- paste(b, collapse=" ")