2

類似性マトリックスを作成する必要があります。以下のコードは、これまでのところです。しかし、結果は私が必要とするものではありません。このコードは、16 行のマトリックスを返します。これは、document-term マトリックス内の 8 つの固有の用語と、workTitle 内の 2 つの固有の用語の積です。

必要なのは、4 行 (タイトルごとに 1 行) しかないマトリックスで、各行は workTitle の各単語とタイトルの各用語の間の編集距離の合計を表します。

require(tm)

workTitle <- c("biomechanical engineer")
titles <- c("train machinist", "operations supervisor", "pharmacy tech", "mechanical engineer")

# create Corpus and a document-term matrix from the titles
titleCorpus <- Corpus(VectorSource(titles))
titleDtm <- DocumentTermMatrix(titleCorpus)

# print out the document-term matrix
inspect(titleDtm)

# calculate edit distance between every word from the test_var and the column names in the document-term matrix
d <- apply(titleDtm, 1, function(x) {
  terms <- unlist(strsplit(as.character(workTitle), " "))
  adist(colnames(titleDtm), terms)
})

上記のコードの出力は次のとおりです。

       Docs
         1  2  3  4
   [1,] 11 11 11 11
   [2,]  8  8  8  8
   [3,]  3  3  3  3
   [4,]  9  9  9  9
   [5,] 11 11 11 11
   [6,] 11 11 11 11
   [7,] 10 10 10 10
   [8,] 11 11 11 11
   [9,]  0  0  0  0
  [10,]  7  7  7  7
  [11,]  8  8  8  8
  [12,]  9  9  9  9
  [13,]  8  8  8  8
  [14,]  8  8  8  8
  [15,]  7  7  7  7
  [16,]  6  6  6  6
4

1 に答える 1

1

私が正しく理解していれば、次のようなものはどうですか:

terms <- as.character(Dictionary(titleDtm))
dat <- data.frame(adist(titles, terms), row.names = titles)
colnames(dat) <- terms
dat

その結果、

                       engineer machinist mechanical operations pharmacy supervisor tech train
 train machinist             12         6         11         12       11         14   12    10
 operations supervisor       16        17         18         11       18         11   19    17
 pharmacy tech               12        10         11         11        5         13    9    11
 mechanical engineer         11        13          9         16       16         16   16    16

そして、合計について

data.frame(sum = rowSums(dat))

次の出力があります

                      sum
train machinist        88
operations supervisor 127
pharmacy tech          82
mechanical engineer   113
于 2013-09-14T10:46:17.800 に答える