-
Notifications
You must be signed in to change notification settings - Fork 0
/
runLSA.scala
252 lines (209 loc) · 8.99 KB
/
runLSA.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* Copyright 2015 Sanford Ryza, Uri Laserson, Sean Owen and Joshua Wills
*
* See LICENSE file for further information.
*/
package com.cloudera.datascience.lsa
import breeze.linalg.{DenseMatrix => BDenseMatrix, DenseVector => BDenseVector,
SparseVector => BSparseVector}
import com.cloudera.datascience.lsa.ParseWikipedia._
import org.apache.spark.{SparkContext, SparkConf}
import org.apache.spark.SparkContext._
import org.apache.spark.mllib.linalg._
import org.apache.spark.mllib.linalg.distributed.RowMatrix
import org.apache.spark.rdd.RDD
import scala.collection.Map
import scala.collection.mutable.ArrayBuffer
object RunLSA {
def main(args: Array[String]) {
val k = if (args.length > 0) args(0).toInt else 100
val numTerms = if (args.length > 1) args(1).toInt else 50000
val sampleSize = if (args.length > 2) args(2).toDouble else 0.1
val conf = new SparkConf().setAppName("Wiki LSA")
conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
val sc = new SparkContext(conf)
val (termDocMatrix, termIds, docIds, idfs) = preprocessing(sampleSize, numTerms, sc)
termDocMatrix.cache()
val mat = new RowMatrix(termDocMatrix)
val svd = mat.computeSVD(k, computeU=true)
println("Singular values: " + svd.s)
val topConceptTerms = topTermsInTopConcepts(svd, 10, 10, termIds)
val topConceptDocs = topDocsInTopConcepts(svd, 10, 10, docIds)
for ((terms, docs) <- topConceptTerms.zip(topConceptDocs)) {
println("Concept terms: " + terms.map(_._1).mkString(", "))
println("Concept docs: " + docs.map(_._1).mkString(", "))
println()
}
}
/**
* Returns an RDD of rows of the term document matrix, a mapping of column indices to terms, and a
* mapping of row IDs to document titles.
*/
def preprocessing(sampleSize: Double, numTerms: Int, sc: SparkContext)
: (RDD[Vector], Map[Int, String], Map[Long, String], Map[String, Double]) = {
val pages = readFile("hdfs:///user/ds/Wikipedia/", sc)
.sample(false, sampleSize, 11L)
val plainText = pages.filter(_ != null).flatMap(wikiXmlToPlainText)
val stopWords = sc.broadcast(loadStopWords("stopwords.txt")).value
val lemmatized = plainText.mapPartitions(iter => {
val pipeline = createNLPPipeline()
iter.map{ case(title, contents) => (title, plainTextToLemmas(contents, stopWords, pipeline))}
})
val filtered = lemmatized.filter(_._2.size > 1)
termDocumentMatrix(filtered, stopWords, numTerms, sc)
}
def topTermsInTopConcepts(svd: SingularValueDecomposition[RowMatrix, Matrix], numConcepts: Int,
numTerms: Int, termIds: Map[Int, String]): Seq[Seq[(String, Double)]] = {
val v = svd.V
val topTerms = new ArrayBuffer[Seq[(String, Double)]]()
val arr = v.toArray
for (i <- 0 until numConcepts) {
val offs = i * v.numRows
val termWeights = arr.slice(offs, offs + v.numRows).zipWithIndex
val sorted = termWeights.sortBy(-_._1)
topTerms += sorted.take(numTerms).map{case (score, id) => (termIds(id), score)}
}
topTerms
}
def topDocsInTopConcepts(svd: SingularValueDecomposition[RowMatrix, Matrix], numConcepts: Int,
numDocs: Int, docIds: Map[Long, String]): Seq[Seq[(String, Double)]] = {
val u = svd.U
val topDocs = new ArrayBuffer[Seq[(String, Double)]]()
for (i <- 0 until numConcepts) {
val docWeights = u.rows.map(_.toArray(i)).zipWithUniqueId
topDocs += docWeights.top(numDocs).map{case (score, id) => (docIds(id), score)}
}
topDocs
}
/**
* Selects a row from a matrix.
*/
def row(mat: BDenseMatrix[Double], index: Int): Seq[Double] = {
(0 until mat.cols).map(c => mat(index, c))
}
/**
* Selects a row from a matrix.
*/
def row(mat: Matrix, index: Int): Seq[Double] = {
val arr = mat.toArray
(0 until mat.numCols).map(i => arr(index + i * mat.numRows))
}
/**
* Selects a row from a distributed matrix.
*/
def row(mat: RowMatrix, id: Long): Array[Double] = {
mat.rows.zipWithUniqueId.map(_.swap).lookup(id).head.toArray
}
/**
* Finds the product of a dense matrix and a diagonal matrix represented by a vector.
* Breeze doesn't support efficient diagonal representations, so multiply manually.
*/
def multiplyByDiagonalMatrix(mat: Matrix, diag: Vector): BDenseMatrix[Double] = {
val sArr = diag.toArray
new BDenseMatrix[Double](mat.numRows, mat.numCols, mat.toArray)
.mapPairs{case ((r, c), v) => v * sArr(c)}
}
/**
* Finds the product of a distributed matrix and a diagonal matrix represented by a vector.
*/
def multiplyByDiagonalMatrix(mat: RowMatrix, diag: Vector): RowMatrix = {
val sArr = diag.toArray
new RowMatrix(mat.rows.map(vec => {
val vecArr = vec.toArray
val newArr = (0 until vec.size).toArray.map(i => vecArr(i) * sArr(i))
Vectors.dense(newArr)
}))
}
/**
* Returns a matrix where each row is divided by its length.
*/
def rowsNormalized(mat: BDenseMatrix[Double]): BDenseMatrix[Double] = {
val newMat = new BDenseMatrix[Double](mat.rows, mat.cols)
for (r <- 0 until mat.rows) {
val length = math.sqrt((0 until mat.cols).map(c => mat(r, c) * mat(r, c)).sum)
(0 until mat.cols).map(c => newMat.update(r, c, mat(r, c) / length))
}
newMat
}
/**
* Returns a distributed matrix where each row is divided by its length.
*/
def rowsNormalized(mat: RowMatrix): RowMatrix = {
new RowMatrix(mat.rows.map(vec => {
val length = math.sqrt(vec.toArray.map(x => x * x).sum)
Vectors.dense(vec.toArray.map(_ / length))
}))
}
/**
* Finds terms relevant to a term. Returns the term IDs and scores for the terms with the highest
* relevance scores to the given term.
*/
def topTermsForTerm(normalizedVS: BDenseMatrix[Double], termId: Int): Seq[(Double, Int)] = {
// Look up the row in VS corresponding to the given term ID.
val termRowVec = new BDenseVector[Double](row(normalizedVS, termId).toArray)
// Compute scores against every term
val termScores = (normalizedVS * termRowVec).toArray.zipWithIndex
// Find the terms with the highest scores
termScores.sortBy(-_._1).take(10)
}
/**
* Finds docs relevant to a doc. Returns the doc IDs and scores for the docs with the highest
* relevance scores to the given doc.
*/
def topDocsForDoc(normalizedUS: RowMatrix, docId: Long): Seq[(Double, Long)] = {
// Look up the row in US corresponding to the given doc ID.
val docRowArr = row(normalizedUS, docId)
val docRowVec = Matrices.dense(docRowArr.length, 1, docRowArr)
// Compute scores against every doc
val docScores = normalizedUS.multiply(docRowVec)
// Find the docs with the highest scores
val allDocWeights = docScores.rows.map(_.toArray(0)).zipWithUniqueId
// Docs can end up with NaN score if their row in U is all zeros. Filter these out.
allDocWeights.filter(!_._1.isNaN).top(10)
}
/**
* Finds docs relevant to a term. Returns the doc IDs and scores for the docs with the highest
* relevance scores to the given term.
*/
def topDocsForTerm(US: RowMatrix, V: Matrix, termId: Int): Seq[(Double, Long)] = {
val termRowArr = row(V, termId).toArray
val termRowVec = Matrices.dense(termRowArr.length, 1, termRowArr)
// Compute scores against every doc
val docScores = US.multiply(termRowVec)
// Find the docs with the highest scores
val allDocWeights = docScores.rows.map(_.toArray(0)).zipWithUniqueId
allDocWeights.top(10)
}
def termsToQueryVector(terms: Seq[String], idTerms: Map[String, Int], idfs: Map[String, Double])
: BSparseVector[Double] = {
val indices = terms.map(idTerms(_)).toArray
val values = terms.map(idfs(_)).toArray
new BSparseVector[Double](indices, values, idTerms.size)
}
def topDocsForTermQuery(US: RowMatrix, V: Matrix, query: BSparseVector[Double])
: Seq[(Double, Long)] = {
val breezeV = new BDenseMatrix[Double](V.numRows, V.numCols, V.toArray)
val termRowArr = (breezeV.t * query).toArray
val termRowVec = Matrices.dense(termRowArr.length, 1, termRowArr)
// Compute scores against every doc
val docScores = US.multiply(termRowVec)
// Find the docs with the highest scores
val allDocWeights = docScores.rows.map(_.toArray(0)).zipWithUniqueId
allDocWeights.top(10)
}
def printTopTermsForTerm(normalizedVS: BDenseMatrix[Double],
term: String, idTerms: Map[String, Int], termIds: Map[Int, String]) {
printIdWeights(topTermsForTerm(normalizedVS, idTerms(term)), termIds)
}
def printTopDocsForDoc(normalizedUS: RowMatrix, doc: String, idDocs: Map[String, Long],
docIds: Map[Long, String]) {
printIdWeights(topDocsForDoc(normalizedUS, idDocs(doc)), docIds)
}
def printTopDocsForTerm(US: RowMatrix, V: Matrix, term: String, idTerms: Map[String, Int],
docIds: Map[Long, String]) {
printIdWeights(topDocsForTerm(US, V, idTerms(term)), docIds)
}
def printIdWeights[T](idWeights: Seq[(Double, T)], entityIds: Map[T, String]) {
println(idWeights.map{case (score, id) => (entityIds(id), score)}.mkString(", "))
}
}