scala - 计算`Spark Dataframe的连续行的编辑距离
问题描述
我有一个数据框如下:
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.Column
import org.apache.spark.sql.functions._
import spark.implicits._
// some data...
val df = Seq(
(1, "AA", "BB", ("AA", "BB")),
(2, "AA", "BB", ("AA", "BB")),
(3, "AB", "BB", ("AB", "BB"))
).toDF("id","name", "surname", "array")
df.show()
我正在寻找计算连续行中“数组”列之间的编辑距离。例如,我想计算第 1 列中的“数组”实体(“AA”、“BB”)和第 2 列中的“数组”实体(“AA”、“BB”)之间的编辑距离。这是我正在使用的编辑距离函数:
def editDist2[A](a: Iterable[A], b: Iterable[A]): Int = {
val startRow = (0 to b.size).toList
a.foldLeft(startRow) { (prevRow, aElem) =>
(prevRow.zip(prevRow.tail).zip(b)).scanLeft(prevRow.head + 1) {
case (left, ((diag, up), bElem)) => {
val aGapScore = up + 1
val bGapScore = left + 1
val matchScore = diag + (if (aElem == bElem) 0 else 1)
List(aGapScore, bGapScore, matchScore).min
}
}
}.last
}
我知道我需要为此功能创建一个 UDF,但似乎无法做到。如果我按原样使用该函数并使用 Spark Windowing 获取前一行:
// creating window - ordered by ID
val window = Window.orderBy("id")
// using the window with lag function to compare to previous value in each column
df.withColumn("edit-d", editDist2(($"array"), lag("array", 1).over(window))).show()
我收到以下错误:
<console>:245: error: type mismatch;
found : org.apache.spark.sql.ColumnName
required: Iterable[?]
df.withColumn("edit-d", editDist2(($"array"), lag("array", 1).over(window))).show()
解决方案
我发现您可以为此使用 Spark 自己的 levenshtein 函数。该函数接受两个字符串进行比较,因此不能与数组一起使用。
// creating window - ordered by ID
val window = Window.orderBy("id")
// using the window with lag function to compare to previous value in each column
df.withColumn("edit-d", levenshtein(($"name"), lag("name", 1).over(window)) + levenshtein(($"surname"), lag("surname", 1).over(window))).show()
给出所需的输出:
+---+----+-------+--------+------+
| id|name|surname| array|edit-d|
+---+----+-------+--------+------+
| 1| AA| BB|[AA, BB]| null|
| 2| AA| BB|[AA, BB]| 0|
| 3| AB| BB|[AB, BB]| 1|
+---+----+-------+--------+------+
推荐阅读
- python-3.x - DRF 序列化程序未在默认 HTML 表单页面 API 中显示字段(外键和 many2many)。字段在 GET 中可用
- java - 在可选列表中查找字符串
- powershell - Are you able to use PtrToStringAuto to decrypt a secure string in Powershell 7 on macOS?
- delphi - 如何使 ListBox 项具有给定的颜色?
- java - Javac 不是内部或外部命令
- ruby - 使用 Ruby 和 Psych 解析 Unity YAML 场景文件的语法错误
- vbscript - VBScript 文件启动 wscript.exe 但不执行代码
- flutter - 颤振集团。在 mapEventToState() 中产生的状态中,我无法获取要传递的值
- graphql - “TypeError:无法读取 allParts 处未定义的属性 'getAllParts' ..\\resolvers.js:6:62)”,
- ruby - Heroku-18:Git 推送失败。在推送时显示不同版本的 Ruby