類似性マトリックスを作成する必要があります。以下のコードは、これまでのところです。しかし、結果は私が必要とするものではありません。このコードは、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