Java 类名:com.alibaba.alink.operator.batch.similarity.TextSimilarityPairwiseBatchOp
Python 类名:TextSimilarityPairwiseBatchOp

功能介绍

文章相似度是在字符串相似度的基础上,基于词,计算两两文章或者句子之间的相似度,文章或者句子需要以空格分割的文本,计算方式和字符串相似度类似: 支持Levenshtein Distance,Longest Common SubString,String Subsequence Kernel,Cosine,SimHashHamming,MinHash和Jaccard七种相似度计算方式,通过选择metric参数可计算不同的相似度。
Levenshtein(Levenshtein Distance)支持距离和相似度两种方式,相似度=(1-距离)/length,length为两个字符长度的最大值,距离应选择metric的参数为LEVENSHTEIN,相似度应选metric的参数为LEVENSHTEIN_SIM。
LCS(Longest Common SubString)支持距离和相似度两种参数,相似度=(1-距离)/length,length为两个字符长度的最大值,距离应选择metric的参数为LCS,相似度应选择metric的参数为LCS_SIM。
SSK(String Subsequence Kernel)支持相似度计算,应选择metric的参数为SSK。
Cosine(Cosine)支持相似度计算,应选择metric的参数为COSINE。
SimhashHamming(SimHash_Hamming_Distance),支持距离和相似度两种方式,相似度=1-距离/64.0,距离应选择metric的参数为SIMHASH_HAMMING,相似度应选择metric的参数为SIMHASH_HAMMING_SIM。
MinHash 支持相似度计算,应选择metric的参数为MINHASH_SIM。
Jaccard 支持相似度计算,应选择metric的参数为JACCARD_SIM。
Alink上文本相似度算法包括Batch组件和Stream组件。

参数说明

| 名称 | 中文名称 | 描述 | 类型 | 是否必须? | 取值范围 | 默认值 | | —- | —- | —- | —- | —- | —- | —- |

| outputCol | 输出结果列列名 | 输出结果列列名,必选 | String | ✓ | | |

| selectedCols | 选择的列名 | 计算列对应的列名列表 | String[] | ✓ | 所选列类型为 [STRING] | |

| lambda | 匹配字符权重 | 匹配字符权重,SSK中使用 | Double | | | 0.5 |

| metric | 度量类型 | 计算距离时,可以取不同的度量 | String | | “LEVENSHTEIN”, “LEVENSHTEIN_SIM”, “LCS”, “LCS_SIM”, “SSK”, “COSINE”, “SIMHASH_HAMMING”, “SIMHASH_HAMMING_SIM”, “JACCARD_SIM” | “LEVENSHTEIN_SIM” |

| numBucket | 分桶个数 | 分桶个数 | Integer | | | 10 |

| numHashTables | 哈希表个数 | 哈希表的数目 | Integer | | | 10 |

| reservedCols | 算法保留列名 | 算法保留列 | String[] | | | null |

| seed | 采样种子 | 采样种子 | Long | | | 0 |

| windowSize | 窗口大小 | 窗口大小 | Integer | | [1, +inf) | 2 |

| numThreads | 组件多线程线程个数 | 组件多线程线程个数 | Integer | | | 1 |

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. [0, "a b c d e", "a a b c e"],
  6. [1, "a a c e d w", "a a b b e d"],
  7. [2, "c d e f a", "b b c e f a"],
  8. [3, "b d e f h", "d d e a c"],
  9. [4, "a c e d m", "a e e f b c"]
  10. ])
  11. inOp1 = BatchOperator.fromDataframe(df, schemaStr='id long, text1 string, text2 string')
  12. inOp2 = StreamOperator.fromDataframe(df, schemaStr='id long, text1 string, text2 string')
  13. batchOp = TextSimilarityPairwiseBatchOp().setSelectedCols(["text1", "text2"]).setMetric("LEVENSHTEIN").setOutputCol("output")
  14. batchOp.linkFrom(inOp1).print()
  15. streamOp = TextSimilarityPairwiseStreamOp().setSelectedCols(["text1", "text2"]).setMetric("COSINE").setOutputCol("output")
  16. streamOp.linkFrom(inOp2).print()
  17. StreamOperator.execute()

Java 代码

  1. import org.apache.flink.types.Row;
  2. import com.alibaba.alink.operator.batch.BatchOperator;
  3. import com.alibaba.alink.operator.batch.similarity.TextSimilarityPairwiseBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import com.alibaba.alink.operator.stream.StreamOperator;
  6. import com.alibaba.alink.operator.stream.similarity.TextSimilarityPairwiseStreamOp;
  7. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  8. import org.junit.Test;
  9. import java.util.Arrays;
  10. import java.util.List;
  11. public class TextSimilarityPairwiseBatchOpTest {
  12. @Test
  13. public void testTextSimilarityPairwiseBatchOp() throws Exception {
  14. List <Row> df = Arrays.asList(
  15. Row.of(0, "a b c d e", "a a b c e"),
  16. Row.of(1, "a a c e d w", "a a b b e d"),
  17. Row.of(2, "c d e f a", "b b c e f a"),
  18. Row.of(3, "b d e f h", "d d e a c"),
  19. Row.of(4, "a c e d m", "a e e f b c")
  20. );
  21. BatchOperator <?> inOp1 = new MemSourceBatchOp(df, "id int, text1 string, text2 string");
  22. StreamOperator <?> inOp2 = new MemSourceStreamOp(df, "id int, text1 string, text2 string");
  23. BatchOperator <?> batchOp = new TextSimilarityPairwiseBatchOp().setSelectedCols("text1", "text2").setMetric(
  24. "LEVENSHTEIN").setOutputCol("output");
  25. batchOp.linkFrom(inOp1).print();
  26. StreamOperator <?> streamOp = new TextSimilarityPairwiseStreamOp().setSelectedCols("text1", "text2").setMetric(
  27. "COSINE").setOutputCol("output");
  28. streamOp.linkFrom(inOp2).print();
  29. StreamOperator.execute();
  30. }
  31. }

运行结果

| id | text1 | text2 | output | | —- | —- | —- | —- |

| 0 | a b c d e | a a b c e | 2.0 |

| 1 | a a c e d w | a a b b e d | 3.0 |

| 2 | c d e f a | b b c e f a | 3.0 |

| 3 | b d e f h | d d e a c | 3.0 |

| 4 | a c e d m | a e e f b c | 4.0 |