0

我写了一个小代码来从 R 中的推文中提取主题标签

m<-c(paste("Hello! #London is gr8. #Wow"," ")) # My tweet
#m<- c("Hello! #London is gr8. #Wow")

x<- unlist(gregexpr("#(\\S+)",m))
#substring(m,x)[1]

subs<-function(x){
  return(substring(m,x+1,(x-2+regexpr(" |\\n",substring(m,x)[1]))))
}

tag<- sapply(x, subs)
#x
tag

如果没有我在推文末尾添加空格,此代码将无法工作。可能是什么原因?我也试过 \n 。

4

3 回答 3

1

尝试这个:

m <- c("Hello! #London is gr8. #Wow")
x <- unlist(strsplit(m, " "))
tag <- x[grep("^#", x)]
tag

现在,让我们假设您有一个推文列表,如下所示:

m1 <- c("Hello! #London is gr8. #Wow")
m2 <- c("#Hello! #London is gr8. #Wow")
m3 <- c("#Hello! #London i#s gr8. #Wow")
m4 <- c("Hello! #London is gr8. #Wow ")
m <- list(m1, m2, m3, m4)

你可以写一个小函数:

getTags <- function(tweet) {
  x <- unlist(strsplit(tweet, " "))
  tag <- x[grep("^#", x)]
  return(tag)
}

并应用它:

lapply(m, function(tweet) getTags(tweet))
[[1]]
[1] "#London" "#Wow"   
[[2]]
[1] "#Hello!" "#London" "#Wow"   
[[3]]
[1] "#Hello!" "#London" "#Wow"   
[[4]]
[1] "#London" "#Wow"  

事后想...

如果你想要散列(或任何标点符号被删除),函数应该是

getTags <- function(tweet) {
  x <- unlist(strsplit(tweet, " "))
  tag <- x[grep("^#", x)]
  tag <- gsub("#", "", tag)
  return(tag)
}

或者

getTags <- function(tweet) {
  x <- unlist(strsplit(tweet, " "))
  tag <- x[grep("^#", x)]
  tag <- gsub("[[:punct:]]", "", tag)
  return(tag)
}
于 2012-12-08T18:08:29.123 回答
1

gregexpr为您提供所需的信息:

R> m<- c("Hello! #London is gr8. #Wow")
R> (x<- gregexpr("#(\\S+)",m)[[1]])
[1]  8 24
attr(,"match.length")
[1] 7 4
attr(,"useBytes")
[1] TRUE

所以我们可以将match.length与起始位置结合起来:

R> substring(m, x+1 , x - 1 + attr(x,"match.length"))
[1] "London" "Wow"   
于 2012-12-07T16:04:45.997 回答
0

$匹配字符串的结尾。

m<- c("Hello! #London is gr8. #Wow")

subs<-function(x){
  return(substring(m,x+1,(x-2+regexpr(" |$",substring(m,x)[1]))))
}

其余代码完好无损:

> tag
[1] "London" "Wow"   
于 2012-12-08T19:58:38.857 回答