2013-01-14 4 views
1

문서의 용어를 비교하여 유사성 매트릭스를 작성해야합니다. 예를 들어 Document1과 Document2가 같은 용어를 2 개 사용하는 경우 m [1, 2]에 의 유사도 행렬에 2를 써야합니다. 내 유사성 매트릭스는 지금 다음과 같습니다.문서 용어 이용시 용어 비교 R

 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] 
[1,] 0 NA NA NA NA NA NA NA NA 
[2,] 0 0 NA NA NA NA NA NA NA 
[3,] 0 0 0 NA NA NA NA NA NA 
[4,] 0 0 0 0 NA NA NA NA NA 
[5,] 0 0 0 0 0 NA NA NA NA 
[6,] 0 0 0 0 0 0 NA NA NA 
[7,] 0 0 0 0 0 0 0 NA NA 
[8,] 0 0 0 0 0 0 0 0 NA 

문서 및 용어는 문서 용어 매트릭스 안에 있습니다. 이제는 유사도 행렬에서 NA를 말하는 모든 문서와 용어를 비교하여 유사도 행렬을 채워야합니다. 문서 쌍의 모든 용어 일치에 대해 +1을 계산하고 행렬의 올바른 위치에 최종 값을 주입해야합니다.

내 문제는 문서 용어 매트릭스에서 단일 문서와 해당 용어에 액세스 할 수 없다는 것입니다. 이 작업을 수행하는 다른 방법이 있습니까? 여기 코드 :

install.packages("tm") 
install.packages("openNLP") 
install.packages("openNLPmodels.en") 

Sys.setenv(NOAWT=TRUE) 

library(tm) 
library(openNLP) 
library(openNLPmodels.en) 

sample = c(
    "count eagle alien", 
    "dis bound eagle", 
    "bound count eagle dis", 
    "count eagle dis alien", 
    "bound eagle", 
    "count dis alien", 
    "bound count alien", 
    "bound count", 
    "count eagle dis" 
) 
print(sample) 
corpus <- Corpus(VectorSource(sample)) 
inspect(corpus) 

corpus <- tm_map(corpus, removeNumbers) 
corpus <- tm_map(corpus, removePunctuation) 
corpus <- tm_map(corpus, tolower) 
corpus <- tm_map(corpus, removeWords, stopwords("english")) 
corpus <- tm_map(corpus, stemDocument,language="english") 
corpus <- tm_map(corpus, stripWhitespace) 
corpus <- tm_map(corpus, tmTagPOS) 
inspect(corpus) 

dtm <- DocumentTermMatrix(corpus) 
inspect(dtm) 

# need to create similarity matrix here 
#dist(dtm, method = "manhattan", diag = FALSE, upper = TRUE) 

rowCount <- nrow(dtm) 
similMatrix = matrix(nrow = rowCount - 1, ncol = rowCount) 
show(similMatrix) 
similMatrix[ row(similMatrix) >= col(similMatrix) ] <- 0 

for(i in 1:(rowCount - 1)){ # rows 
    for (j in i+1:rowCount){  # cols 

     # need to compare document i and j here and write 
     # the value into similarity matrix 
    } 
} 
show(similMatrix) 
+1

많은 패키지가 있습니다. 이것을 재현하려면 모두 설치해야합니까? –

+1

예를 들어,'DocumentTermMatrix'를 정의한 패키지를 설치했다면 결과에'dput'을 써서 표현을 만들면 재현 할 수 있을까요? –

+0

tm openNLP 및 openNLPmodels.en 패키지는 작업을 수행해야하지만 100 % 확실하지는 않습니다. 이 모든 패키지는 내 교수가 작업을 수행하도록 권장되었습니다. –

답변

2

유사성 매트릭스에 행이 하나 더없는 것 같습니다. 마지막 문서를 다루지 않기 때문입니다. 내 것이 이처럼 보입니다.

 [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] 
[1,] NA NA NA NA NA NA NA NA NA 
[2,] 1 NA NA NA NA NA NA NA NA 
[3,] 2 3 NA NA NA NA NA NA NA 
[4,] 3 2 3 NA NA NA NA NA NA 
[5,] 1 2 2 1 NA NA NA NA NA 
[6,] 2 1 2 3 0 NA NA NA NA 
[7,] 2 1 2 2 1 2 NA NA NA 
[8,] 1 1 2 1 1 1 2 NA NA 
[9,] 2 2 3 3 1 2 1 1 NA 

이 결과를 얻으려면 다음 단계를 수행하십시오.

mat=as.data.frame(as.matrix(dtm)) # you get the dataframe from DocumentTerm Matrix 
rowCount <- nrow(dtm) 
colCount <- ncol(dtm) 
similMatrix = matrix(nrow = rowCount, ncol = rowCount) 
similMatrix[ row(similMatrix) >= col(similMatrix) ] <- 0 
for(i in 1:(rowCount)){ #set all columns NA you can change to zeros if you need later 
    similMatrix[i,i]=NA 
} # then we will do the actual job 
for(i in 1:rowCount){ # rows 
    for (j in 1:rowCount){  # cols 
     if(is.na(similMatrix[i,j])==F){ 
     a=mat[i,] 
     b=mat[j,] 
     for(k in 1:colCount){ #n number of Cols in Document term matrix 

      if(a[k]==1 && a[k]==b[k]){ 
       similMatrix[i,j]=similMatrix[i,j]+1 
      } 
     } 
     } 
    } 
} 
+0

이것은 아주 좋습니다 !!! 그것은 여러분이 행렬을 뒤집은 것처럼 보입니다. 행렬의 왼쪽의 botom과 오른쪽 상단 삼각형을 바꿀 수 있습니까? 코드에서 무엇을 변경해야합니까? –

+1

행렬'similMatrix = t (similMatrix)'를 transposing 할 수 있습니다. – user974514

+0

감사합니다. –

관련 문제