2013-01-14 124 views
1

我需要通過比較文檔條款來構建相似矩陣。例如,如果Document1和Document2有兩個相同的術語,那麼我需要在我的相似度矩陣 (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

這太好了!它似乎只是顛倒了矩陣,是否可以改變矩陣的左邊底部和右邊頂部三角形?我需要在代碼中更改什麼? –

+1

你可以嘗試轉置矩陣'similMatrix = t(similMatrix)'。 – user974514

+0

非常感謝你! –