With Scalding I need to:
- Group string fields by first 3 chars
- Compare strings in all pairs in every group using
edit-distance
metric ( http://en.wikipedia.org/wiki/Edit_distance) - Write results in CSV file where record is
string; string; distance
To group strings I use map
and groupBy
as in the following example:
import cascading.tuple.Fields
import com.twitter.scalding._
class Scan(args: Args) extends Job(args) {
val output = TextLine("tmp/out.txt")
val wordsList = List(
("aaaa"),
("aaabb"),
("aabbcc"),
("aaabccdd"),
("aaabbccdde"),
("aaabbddd"),
("bbbb"),
("bbbaaa"),
("bbaaabb"),
("bbbcccc"),
("bbbddde"),
("ccccc"),
("cccaaa"),
("ccccaabbb"),
("ccbbbddd"),
("cdddeee")
)
val orderedPipe =
IterableSource[(String)](wordsList, ('word))
.map('word -> 'key ){word:String => word.take(3)}
.groupBy('key) {_.toList[String]('word -> 'x) }
.debug
.write(output)
}
As a result I get:
['aaa', 'List(aaabbddd, aaabbccdde, aaabccdd, aaabb, aaaa)']
['aab', 'List(aabbcc)']
['bba', 'List(bbaaabb)']
['bbb', 'List(bbbddde, bbbcccc, bbbaaa, bbbb)']
['ccb', 'List(ccbbbddd)']
['ccc', 'List(ccccaabbb, cccaaa, ccccc)']
['cdd', 'List(cdddeee)']
Now, in this example, I need to comute edit-distance for strings with aaa
key in this list:
List(aaabbddd, aaabbccdde, aaabccdd, aaabb, aaaa)
next for all strings with 'bbb' key in this list:
List(bbbddde, bbbcccc, bbbaaa, bbbb)
etc.
To compute edit-distance between all strings in every group I need to replace toList
with my own function, how can I do this? And also how can I write results of my function to a CSV file?
Thanks!
Update
How to get List
from Scalding Pipe
?
toList
just returns another Pipe
so I can't use it all:
val orderedPipe =
IterableSource[(String)](wordsList, ('word))
.map('word -> 'key ){word:String => word.take(3)}
.groupBy('key) {_.toList[String]('word -> 'x) }
.combinations(2) //---ERROR! Pipe has no such method!
.debug
.write(output)