Р: учитывать знаки препинания, чтобы сделать слово сегментация
Я использую NGramTokenizer (), чтобы сделать сегментацию 1~3 грамм, но, кажется, не учитывает пунктуацию и удаляет пунктуацию.
Таким образом, слова сегментации не идеальны для меня.(как результат: оксидант амино, оксидант аминокислота, оксидант гранулы и так далее.)
Существует ли какой-либо способ сегментации для сохранения пунктуации (я думаю, что могу использовать POS-теги для фильтрации строк, содержащих пунктуацию после работы с сегментацией.)
Или по-другому можете считать пунктуация для сегментации слов? Это будет больше идеальный для меня.
text <-  "the slurry includes: attrition pellet, oxidant, amino acid and water."
corpus_text <- VCorpus(VectorSource(text))
content(corpus_text[[1]])
BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 1, max = 3))
dtm <-  DocumentTermMatrix(corpus_text, control = list(tokenize = BigramTokenizer))
mat <- as.matrix(dtm)
colnames(mat)
 [1] "acid"                      "acid and"                  "acid and water"           
 [4] "amino"                     "amino acid"                "amino acid and"           
 [7] "and"                       "and water"                 "attrition"                
[10] "attrition pellet"          "attrition pellet oxidant"  "includes"                 
[13] "includes attrition"        "includes attrition pellet" "oxidant"                  
[16] "oxidant amino"             "oxidant amino acid"        "pellet"                   
[19] "pellet oxidant"            "pellet oxidant amino"      "slurry"                   
[22] "slurry includes"           "slurry includes attrition" "the"                      
[25] "the slurry"                "the slurry includes"       "water"    
2 ответа:
Вы можете использовать функцию
tokenizeпакетаquantedaследующим образом:library(quanteda) text <- "some text, with commas, and semicolons; and even fullstop. to be toekinzed" tokens(text, what = "word", remove_punct = FALSE, ngrams = 1:3)Вывод:
tokens from 1 document. text1 : [1] "some" "text" "," "with" [5] "commas" "," "and" "semicolons" [9] ";" "and" "even" "fullstop" [13] "." "to" "be" "toekinzed" [17] "some text" "text ," ", with" "with commas" [21] "commas ," ", and" "and semicolons" "semicolons ;" [25] "; and" "and even" "even fullstop" "fullstop ." [29] ". to" "to be" "be toekinzed" "some text ," [33] "text , with" ", with commas" "with commas ," "commas , and" [37] ", and semicolons" "and semicolons ;" "semicolons ; and" "; and even" [41] "and even fullstop" "even fullstop ." "fullstop . to" ". to be" [45] "to be tokeinzed"Дополнительную информацию о том, что такое каждый аргумент в функции, смотрите в документации
Обновление: Для определения частоты термина документа рассмотрим построение матрицы частот документа
В качестве примера попробуйте следующее:
Для биграмм (обратите внимание, что вам не нужно маркировать):
dfm(text, remove_punct = FALSE, ngrams = 2, concatenator = " ")
Вероятно, вы можете передать корпус через tm_map перед DTM, что-то вроде
text <- "the slurry includes: attrition pellet, oxidant, amino acid and water." corpus_text <- VCorpus(VectorSource(text)) content(corpus_text[[1]]) clean_corpus <- function(corpus){ corpus <- tm_map(corpus, removePunctuation) #other common punctuation corpus <- tm_map(corpus, stripWhitespace) corpus <- tm_map(corpus, removeWords, c(stopwords("en"), "and")) #ignoring "and" return(corpus) } corpus_text <- clean_corpus(corpus_text) content(clean_corpus(corpus_text)[[1]]) #" slurry includes attrition pellet oxidant amino acid water" BigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 1, max = 3)) dtm <- DocumentTermMatrix(corpus_text, control = list(tokenize = BigramTokenizer)) mat <- as.matrix(dtm) colnames(mat)