2

文字列のパターンを識別して抽出する大きなデータフレームがあります。私のタスクを説明するために小さなサブセットを提供しました。複数の単語で TermDocumentMatrix を作成してパターンを生成しています。これらのパターンを stringi および stringr パッケージの stri_extract および str_replace で使用して、「punct_prob」データフレーム内を検索します。

私の問題は、各文字列内で文字通りの意味を維持するために、「punct_prob$description」内で句読点をそのままにしておく必要があることです。たとえば、2.35 mm を 235 mm にすることはできません。ただし、私が使用している TermDocumentMatrix プロシージャは句読点 (または少なくともピリオド) を削除しているため、パターン検索関数はそれらに一致しません。

要するに... TDMを生成するときに句読点を維持するにはどうすればよいですか? TermDocumentMatrix コントロール引数内に removePunctuation=FALSE を含めようとしましたが、成功しませんでした。

library(tm)
punct_prob = data.frame(description = tolower(c("CONTRA ANGLE HEAD 2:1 FOR 2.35mm BUR",
                                    "TITANIUM LINE MINI P.B F.O. TRIP SPRAY",
                                    "TITANIUM LINE POWER P. B F.O. TRIP SPR",
                                    "MEDESY SPECIAL ITEM")))

punct_prob$description = as.character(punct_prob$description)

# a control for the number of words in phrases
max_ngram = max(sapply(strsplit(punct_prob$description, " "), length))

#set up ngrams and tdm
BigramTokenizer <- function(x) {RWeka::NGramTokenizer(x, RWeka::Weka_control(min = max_ngram, max = max_ngram))}
punct_prob_corpus = Corpus(VectorSource(punct_prob$description))
punct_prob_tdm <- TermDocumentMatrix(punct_prob_corpus, control = list(tokenize = BigramTokenizer, removePunctuation=FALSE))
inspect(punct_prob_tdm)

結果の検査 - 句読点なし....

                                   Docs
Terms                              1 2 3 4
  angle head 2 1 for 2 35mm bur    1 0 0 0
  contra angle head 2 1 for 2 35mm 1 0 0 0
  line mini p b f o trip spray     0 1 0 0
  line power p b f o trip spr      0 0 1 0
  titanium line mini p b f o trip  0 1 0 0
  titanium line power p b f o trip 0 0 1 0

事前に助けてくれてありがとう:)

4

2 に答える 2