R で lsa パッケージを使用する方法を学習しようとしています。以下の例よりもはるかに大きなデータ セットを使用していますが、これは再現性を目的としています (このコードを自分のサイトに投稿したことに対するこの人物への小道具です。素晴らしいリソース)。
解決できないような奇妙なエラー メッセージが表示されます。
Error in Ops.simple_triplet_matrix(m, 1) : Incompatible dimensions.
以下は、私がいじっているコードの一部です。
# load required libraries
library(tm)
library(ggplot2)
library(lsa)
library(SnowballC)
lsa <- function () {
# 1. Prepare mock data
text <- c("transporting food by cars will cause global warming. so we should go local.",
"we should try to convince our parents to stop using cars because it will cause global warming.",
"some food, such as mongo, requires a warm weather to grow. so they have to be transported to canada.",
"a typical Electronic Circuit can be built with a battery, a bulb, and a switch.",
"electricity flows from batteries to the bulb, just like water flows through a tube.",
"batteries have chemical energe in it. then electrons flow through a bulb to light it up.",
"birds can fly because they have feather and they are light.", "why some birds like pigeon can fly while some others like chicken cannot?",
"feather is important for birds' fly. if feather on a bird's wings is removed, this bird cannot fly.")
view <- factor(rep(c("view 1", "view 2", "view 3"), each = 3))
df <- data.frame(text, view, stringsAsFactors = FALSE)
# prepare corpus
corpus <- Corpus(VectorSource(df$text))
# corpus <- tm_map(corpus, tolower)
# corpus <- tm_map(corpus, removePunctuation)
# corpus <- tm_map(corpus, function(x) removeWords(x, stopwords("english")))
# corpus <- tm_map(corpus, stemDocument, language = "english")
corpus <- tm_map(corpus, PlainTextDocument)
# 2. MDS with raw term-document matrix compute distance matrix
td.mat <- TermDocumentMatrix(corpus)
td.mat.lsa <- lw_logtf(td.mat) * gw_idf(td.mat) # weighting
lsaSpace <- lsa(td.mat.lsa) # create LSA space
dist.mat.lsa <- dist(t(as.textmatrix(lsaSpace))) # compute distance matrix
return(dist.mat.lsa) # check distance matrix
}
問題なくコーパスを生成でき、用語ドキュメント マトリックスに変換できます。dt.mat.lsa を定義すると、エラーが発生します。
トレースバックは次のとおりです。
4 stop("Incompatible dimensions.")
3 Ops.simple_triplet_matrix(m, 1)
2 lw_logtf(td.mat) at lsa.R#31
1 lsa()
したがって、私の主な質問は次のとおりです。
- なぜこのエラーが発生するのですか?
- このようなエラーを回避するためにコードを修正するにはどうすればよいですか?
ここで提供できるヘルプを事前に感謝します。これは私の最初の投稿なので、私の質問の質に関するフィードバックも大歓迎です!