【问题标题】:Remove all except for certain punctuation characters to generate word frequency?删除除某些标点符号之外的所有字符以生成词频?
【发布时间】:2018-07-26 13:25:47
【问题描述】:

我想从字符向量中删除除这四个特定标点字符之外的所有标点符号:+, ., -, /

我知道有类似的问题,但是,我尝试了相应的解决方案,但没有得到我想要的答案。

当前字符向量item 有很多我想去掉的圆括号和方括号。

下面是item 变量的示例:

item
BOYS S SLV MOCK LAYER TEE
BOYS S SLV PRINTED TEE
CHEAP MONDAY TEE (SAD TOP)
LOPPAN S SLV TEE (STRIPE)
FREE PRINTED SLV LESS TEE-ZEBRALOGO & SNAKE
LST-[REVISED]

最终,我想针对变量item 生成唯一的词频。

word          freq
boys          2
s             3
slv           4
tee           4
tee-zebralogo 1
mock          1
layer         1
printed       2
cheap         1
...           ...

这是我当前使用 tm 包的代码:

item_names <- df1$item
item_names <- tolower(item_names)
item_names <- removePunctuation(item_names)
myCorpus <- Corpus(VectorSource(item_names))
myTDM <- TermDocumentMatrix(myCorpus)
findFreqTerms(myTDM)

m <- as.matrix(myTDM)
v <- sort(rowSums(m),decreasing=TRUE)
df4 <- data.frame(word = names(v),freq=v)

从上面的代码中,我可以减少所有的标点符号,但是,我想保留上面的四个标点符号,但我不能令人满意地做到这一点。

我也尝试过 R 的基本功能:

item_names <- df1$item
item_names <- tolower(item_names)
item_names <- gsub(pattern = "[^[:alnum:][:space:][-\\.\\+\\/]]", "", 
item_names)
item_names <- gsub(pattern = "\\s+", " ", item_names)

table(do.call(c, lapply(item_names, function(x) unlist(strsplit(x, " ")))))
df4 <- as.data.frame(table(do.call(c, lapply(item_names, function(x) 
unlist(strsplit(x, c(" ")))))))
View(df4)

上面的代码似乎不起作用,因为它仍然无法消除标点符号,例如()

最后,我想删除除+, ., -, / 之外的所有标点符号,并使用上述两个选项生成词频。

任何帮助将不胜感激。

【问题讨论】:

  • 很多代码,但没有可以改进问题的可重现示例。
  • 不管怎样,你有没有简单地尝试过:gsub("\\)|\\(", "", item_names)?
  • 是的,它确实有效。但是,您知道如何替换一组标点字符,而不仅仅是一个,在本例中为()。否则,您需要根据需要多次重复上述命令来删除标点符号。例如:item_names &lt;- gsub(pattern = "\\)|\\(", "", item_names)item_names &lt;- gsub(pattern = "\\]|\\[", "", item_names)item_names &lt;- gsub(pattern = "\\&amp;", "", item_names)
  • 我将编辑上述问题并添加一些示例。我很抱歉。
  • gsub("([-\\.\\+\\/])|[[:punct:]]", "\\1", item_names)

标签: r gsub tm punctuation


【解决方案1】:

举个例子:

item_names <- c(
  "BOYS S SLV MOCK LAYER TEE",
  "BOYS S SLV PRINTED TEE",
  "CHEAP MONDAY TEE (SAD TOP)",
  "LOPPAN S SLV TEE (STRIPE)",
  "FREE PRINTED SLV LESS TEE-ZEBRALOGO & SNAKE",
  "LST-[REVISED]",
  "(lot of round and square brackets that I would like to get rid [of]. )"
)

我们可以这样做:

gsub("([-\\.\\+\\/])|[[:punct:]]", "\\1", item_names)
[1] "BOYS S SLV MOCK LAYER TEE"                                         
[2] "BOYS S SLV PRINTED TEE"                                            
[3] "CHEAP MONDAY TEE SAD TOP"                                          
[4] "LOPPAN S SLV TEE STRIPE"                                           
[5] "FREE PRINTED SLV LESS TEE-ZEBRALOGO  SNAKE"                        
[6] "LST-REVISED"                                                       
[7] "lot of round and square brackets that I would like to get rid of. "

【讨论】:

  • 这段代码中“\\1”的意义/作用是什么?我昨天看到了一个这样的例子。
  • \\1 重新插入与() 中的内容匹配的所有内容。如果你有两个(),那么\\1用于第一个,\\2用于第二个......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 2019-03-03
  • 1970-01-01
  • 2012-01-31
相关资源
最近更新 更多