2015-11-27 71 views
2

我有一个很大的数据框,我正在识别字符串中的模式,然后提取它们。我提供了一小部分来说明我的任务。我通过创建具有多个单词的TermDocumentMatrix来生成我的模式。我将这些模式与来自stringi和stringr软件包的stri_extract和str_replace一起在'punct_prob'数据框中搜索。R我如何使用TermDocumentMatrix保留标点符号()

我的问题是,我需要在'punct_prob $ description'内保持标点符号以保持每个字符串中的字面含义。例如,我不能有2.35毫米变成235毫米。然而,我正在使用的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 

感谢提前任何帮助:)

回答

3

的问题是没有这么多的termdocumentmatrix,但NGRAM分词基础在RWEKA。 Rweka在标记化时删除标点符号。

如果您使用nlp标记器它会保留标点符号。见下面的代码。

P.S.我在第三行中删除了一个空格,所以P.B.是P.B.像它是第2行

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) 

max_ngram = max(sapply(strsplit(punct_prob$description, " "), length)) 

punct_prob_corpus = Corpus(VectorSource(punct_prob$description)) 




NLPBigramTokenizer <- function(x) { 
    unlist(lapply(ngrams(words(x), max_ngram), paste, collapse = " "), use.names = FALSE) 
} 


punct_prob_tdm <- TermDocumentMatrix(punct_prob_corpus, control = list(tokenize = NLPBigramTokenizer)) 
inspect(punct_prob_tdm) 

<<TermDocumentMatrix (terms: 3, documents: 4)>> 
Non-/sparse entries: 3/9 
Sparsity   : 75% 
Maximal term length: 38 
Weighting   : term frequency (tf) 

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

谢谢@phiver - 非常感谢! – CallumH

1

quanteda包是足够聪明而不处理字内的标点字符为“标点符号”到tokenise。这让人很容易的构建你的矩阵:

txt <- 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") 

require(quanteda) 
myDfm <- dfm(txt, ngrams = 6:8, concatenator = " ") 
t(myDfm) 
#          docs 
# features        text1 text2 text3 text4 
# contra angle head for 2.35mm bur   1  0  0  0 
# titanium line mini p.b f.o trip   0  1  0  0 
# line mini p.b f.o trip spray    0  1  0  0 
# titanium line mini p.b f.o trip spray  0  1  0  0 
# titanium line power p.b f.o trip   0  0  1  0 
# line power p.b f.o trip spr    0  0  1  0 
# titanium line power p.b f.o trip spr  0  0  1  0 

如果您想保留“标点符号”,它将被标记化作为一个单独的令牌当它结束的一个术语:

myDfm2 <- dfm(txt, ngrams = 8, concatenator = " ", removePunct = FALSE) 
t(myDfm2) 
#           docs 
# features         text1 text2 text3 text4 
# titanium line mini p.b f.o . trip spray  0  1  0  0 
# titanium line power p.b f.o . trip spr  0  0  1  0 

注意这里说的ngrams参数是完全灵活的,并且可以采用ngram大小的向量,如第一个示例中ngrams = 6:8指示它应该形成6,7和8克。

+0

谢谢@Ken。我很快就会玩这个游戏。我喜欢可变的ngram长度这个想法,这是我首先进入RWeka记号器的原因之一。 – CallumH