2

金融記事のセンチメント分析を行っています。単純ベイズ分類器の精度を高めるために、否定処理を実装したいと考えています。

具体的には、「not」または「n't」に続く単語に接頭辞「not_」を追加したい

したがって、私のコーパスに次のようなものがある場合:

 x <- "They didn't sell the company." 

私は以下を取得したい:

"they didn't not_sell the company."

(「しなかった」というストップワードは後で削除されます)

関数のみを見つけることができましgsub()たが、このタスクでは機能しないようです。

どんな助けでも大歓迎です!! ありがとうございました!

4

1 に答える 1

1

具体的には、「not」または「n't」に続く単語に接頭辞「not_」を追加したい

str_negate <- function(x) {
  gsub("not ","not not_",gsub("n't ","n't not_",x))
}

または、strsplit を使用できると思います。

str_negate <- function(x) {
  str_split <- unlist(strsplit(x=x, split=" "))
  is_negative <- grepl("not|n't",str_split,ignore.case=T)
  negate_me <- append(FALSE,is_negative)[1:length(str_split)]
  str_split[negate_me==T]<- paste0("not_",str_split[negate_me==T])
  paste(str_split,collapse=" ")
}

どちらの方法でも次のようになります。

> str_negate("They didn't sell the company")
[1] "They didn't not_sell the company"
> str_negate("They did not sell the company")
[1] "They did not not_sell the company"
于 2014-06-30T21:01:21.860 に答える