2015-01-19 7 views
0

이전에 어떤 질문도 찾을 수 없으므로 도움을 받으실 수 있습니다.R : tm package, aggregate/join docs

메타 데이터 (예 : 다른 작성자의 집합 텍스트)를 기반으로 tm 코퍼스의 데이터를 집계하는 좋은 방법은 무엇입니까? TM에서

  • 내장 된 기능, DocumentTermMatrix는 메타 데이터 기능을 구축 할 수 있도록합니다 :

    는 그것을 할 수있는 두 개 이상의 확실한 방법이 있습니다. 불행히도 나는 이것을 밝힐 수 없었다.

  • 테이블의 일부 외부 메타 데이터를 기반으로 코퍼스 내 문서를 조인하는 방법입니다. 그것은 단지 문서 ID를 대체하기 위해 메타 데이터를 사용합니다. DocumentId,

    AUTHORNAME

    그리고 문서의 양을 포함하는 TM 구축 된 코퍼스 :

그래서 당신은 포함하는 테이블을 가질 것이다. 코퍼스 객체의 메타 데이터로 테이블을 도입하는 것이 어렵지 않다는 것을 알고 있습니다.

매트릭스는 다음 기능을 사용하여 만들 수 있습니다.

library(tm) # version 0.6, you seem to be using an older version 
corpus <-Corpus(DirSource("/directory-with-texts"), 
readerControl = list(language="lat")) 

metadata <- data.frame(DocID, Author) 

#A very crude way to enter metadata into the corpus (assumes the same sequence): 
for (i in 1:length(corpus)) { 
    attr(corpus[[i]], "Author") <- metadata$Author[i] 
} 

a_documenttermmatrix_by_DocId <-DocumentTermMatrix(corpus) 

각 작성자가 문서 대신 여러 문서를 집계 할 수있는 빈도를 보여주는 매트릭스를 어떻게 작성 하시겠습니까? 이 단계에서이 작업을 수행하는 것이 좋을 것입니다. 몇 가지 조건만으로 후 처리 작업을 수행하는 것이 아닙니다.

a_documenttermmatrix_by_Author <- ? 

감사합니다.

답변

0

코퍼스 텍스트를 테이블에서 찾을 수있는 경우 매우 임시 해결 방법이 있습니다. 그러나 이것은 'tm'형식의 큰 코퍼스에서는 도움이되지 않지만 다른 경우에는 유용 ​​할 수 있습니다. 매우 미숙 한 그대로 그것을 향상 시키십시오.

custom_term_matrix <- function(author_vector, text_vector) 
{ 
    author_vector <- factor(author_vector) 
    temp <- data.frame(Author = levels(author_vector)) 

    for (i in 1:length(temp$Author)){ 
    temp$Content[i] <- paste(c(as.character(text_vector[author_vector == 
     levels(author_vector)[i]])), sep=" ", collapse="") 
    } 

    m <- list(id = "Author", content = "Content") 
    myReader <- readTabular(mapping = m) 
    mycorpus <- Corpus(DataframeSource(data1), readerControl = list(reader = myReader)) 

    custom_matrix <<- DocumentTermMatrix(mycorpus, control = 
    list(removePunctuation = TRUE)) 
} 

tm에는 내부적으로 찾을 수없는 기능이있을 수 있으므로 도움을 주셔서 감사합니다.

1

DocumentTermMatrix는 실제로 각 용어와 문서의 용어 빈도가 포함 된 고급 드레싱 (슬램 라이브러리의 단순 삼중 항 매트릭스)이있는 매트릭스입니다. 작성자별로 여러 문서의 데이터를 집계하는 것은 실제로 작성자의 열을 추가하는 것입니다. 표준 R 행렬로 행렬 포맷 고려 표준 서브 세트/집계 방법을 사용 행이 저자이며 열이 각 측면 어디에

# Format the document term matrix as a standard matrix. 
# The rownames of m become the document Id's 
# The colnames of m become the individual terms 
m <- as.matrix(dtm) 

# Transpose matrix to use the "by" operator. 
# Rows become individual terms 
# Columns become document ids 
# Group columns by Author 
# Aggregate column sums (word frequencies) for each author, resulting in a list. 
author.list <- by(t(m), metadata$Author, colSums) 

# Format the list as a matrix and do stuff with it 
author.dtm <- matrix(unlist(author.list), nrow = length(author.list), byrow = T) 

# Add column names (term) and row names (author) 
colnames(author.dtm) <- rownames(m) 
rownames(author.dtm) <- names(author.list) 

# View the resulting matrix 
View(author.dtm[1:10, 1:10]) 

생성 행렬은 표준 행렬 것이다. 그 시점에서 원하는 분석을 할 수 있어야합니다.

관련 문제