1

使用烫伤我需要:

  1. 按前 3 个字符对字符串字段进行分组
  2. edit-distance使用度量 ( http://en.wikipedia.org/wiki/Edit_distance )比较每个组中所有对中的字符串
  3. 将结果写入记录所在的 CSV 文件中string; string; distance

要对我使用的字符串进行分组mapgroupBy如下例所示:

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)
}

结果我得到:

['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)']

aaa现在,在此示例中,我需要计算此列表中带有键的字符串的编辑距离:

List(aaabbddd, aaabbccdde, aaabccdd, aaabb, aaaa)

next 用于此列表中所有带有 'bbb' 键的字符串:

List(bbbddde, bbbcccc, bbbaaa, bbbb)

等等

要计算每个组中所有字符串之间的编辑距离,我需要toList用自己的函数替换,我该怎么做?还有如何将我的函数结果写入 CSV 文件?

谢谢!

更新

如何List从烫伤中获得Pipe

toList只是返回另一个Pipe,所以我不能全部使用它:

  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)
4

1 回答 1

1

编辑距离可以按照维基百科中的描述计算:

def editDistance(a: String, b: String): Int = {

    import scala.math.min

    def min3(x: Int, y: Int, z: Int) = min(min(x, y), z)

    val (m, n) = (a.length, b.length)

    val matrix = Array.fill(m + 1, n + 1)(0)

    for (i <- 0 to m; j <- 0 to n) {

        matrix(i)(j) = if (i == 0) j
                       else if (j == 0) i
                       else if (a(i-1) == b(j-1)) matrix(i-1)(j-1)
                       else min3(
                                 matrix(i - 1)(j) + 1,
                                 matrix(i)(j-1) + 1,
                                 matrix(i - 1)(j - 1) + 1) 
    }

    matrix(m)(n)
}

查找列表元素的成对编辑距离:

def editDistances(list: List[String]) = {

    list.combinations(2).toList.map(x => (x(0), x(1), editDistance(x(0), x(1))))
}

在 groupBy 中使用它:

  val orderedPipe =
      IterableSource[(String)](wordsList, ('word))
      .map('word -> 'key ){word:String => word.take(3)}
      .groupBy('key) {_.mapList[String, List[(String, String, Int)]]('word -> 'x)(editDistances)}
      .debug
      .write(output)    

就写入 csv 格式而言,您可以简单地使用com.twitter.scalding.Csv该类。

write(Csv(outputFile))

于 2014-07-15T16:30:58.887 回答