Java 类名:com.alibaba.alink.operator.stream.nlp.KeywordsExtractionStreamOp
Python 类名:KeywordsExtractionStreamOp

功能介绍

从每行文本中抽取与文本意义最相关的若干词语。

算法原理

流式关键词提取基于 TextRank 算法。
TextRank 受到网页间关系的 PageRank 算法启发,利用局部词汇之间关系(共现窗口)构建图,计算词的重要性,选取权重大的作为关键词。
在构建的图中,每个词语对应一个节点 。 两个不同的词语 只要在同一个窗口中共同出现过,对应节点间就存在两条有向边 和 , 权重分别为 1,即 。
每个节点初始重要性值 ,并按照下面公式进行迭代更新直至收敛: 其中,d 是阻尼系数。

使用方式

文本列通过参数 selectedCol 指定,需要是空格分隔的词语。
文本列可以使用分词(SegmentStreamOp)组件的输出结果列,同时也可以在之前接入停用词过滤(StopWordsRemoverStreamOp)组件去掉常见的高频词。
基于 TextRank 的方法,需要设置窗口大小 windowSize、最大迭代步数 maxIter、收敛阈值 epsilon 和阻尼稀疏 dampingFactor。

文献索引

TextRank:https://web.eecs.umich.edu/~mihalcea/papers/mihalcea.emnlp04.pdf

参数说明

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

| selectedCol | 选中的列名 | 计算列对应的列名 | String | ✓ | 所选列类型为 [STRING] | |

| dampingFactor | 阻尼系数 | 阻尼系数 | Double | | | 0.85 |

| epsilon | 收敛阈值 | 收敛阈值 | Double | | | 1.0E-6 |

| maxIter | 最大迭代步数 | 最大迭代步数,默认为 100 | Integer | | [1, +inf) | 100 |

| outputCol | 输出结果列 | 输出结果列列名,可选,默认null | String | | | null |

| topN | 前N的数据 | 挑选最近的N个数据 | Integer | | [1, +inf) | 10 |

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

代码示例

Python 代码

  1. df = pd.DataFrame([
  2. [0, u'二手旧书:医学电磁成像'],
  3. [1, u'二手美国文学选读( 下册 )李宜燮南开大学出版社 9787310003969'],
  4. [2, u'二手正版图解象棋入门/谢恩思主编/华龄出版社'],
  5. [3, u'二手中国糖尿病文献索引'],
  6. [4, u'二手郁达夫文集( 国内版 )全十二册馆藏书']
  7. ])
  8. inOp1 = BatchOperator.fromDataframe(df, schemaStr='id int, text string')
  9. inOp2 = StreamOperator.fromDataframe(df, schemaStr='id int, text string')
  10. segment = SegmentBatchOp().setSelectedCol("text").linkFrom(inOp1)
  11. remover = StopWordsRemoverBatchOp().setSelectedCol("text").linkFrom(segment)
  12. keywords = KeywordsExtractionBatchOp().setSelectedCol("text").setMethod("TF_IDF").setTopN(3).linkFrom(remover)
  13. keywords.print()
  14. segment2 = SegmentStreamOp().setSelectedCol("text").linkFrom(inOp2)
  15. remover2 = StopWordsRemoverStreamOp().setSelectedCol("text").linkFrom(segment2)
  16. keywords2 = KeywordsExtractionStreamOp().setSelectedCol("text").setTopN(3).linkFrom(remover2)
  17. keywords2.print()
  18. 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.nlp.KeywordsExtractionBatchOp;
  4. import com.alibaba.alink.operator.batch.nlp.SegmentBatchOp;
  5. import com.alibaba.alink.operator.batch.nlp.StopWordsRemoverBatchOp;
  6. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  7. import com.alibaba.alink.operator.stream.StreamOperator;
  8. import com.alibaba.alink.operator.stream.nlp.KeywordsExtractionStreamOp;
  9. import com.alibaba.alink.operator.stream.nlp.SegmentStreamOp;
  10. import com.alibaba.alink.operator.stream.nlp.StopWordsRemoverStreamOp;
  11. import com.alibaba.alink.operator.stream.source.MemSourceStreamOp;
  12. import org.junit.Test;
  13. import java.util.Arrays;
  14. import java.util.List;
  15. public class KeywordsExtractionStreamOpTest {
  16. @Test
  17. public void testKeywordsExtractionStreamOp() throws Exception {
  18. List <Row> df = Arrays.asList(
  19. Row.of(0, "二手旧书:医学电磁成像"),
  20. Row.of(1, "二手美国文学选读( 下册 )李宜燮南开大学出版社 9787310003969"),
  21. Row.of(2, "二手正版图解象棋入门/谢恩思主编/华龄出版社"),
  22. Row.of(3, "二手中国糖尿病文献索引"),
  23. Row.of(4, "二手郁达夫文集( 国内版 )全十二册馆藏书")
  24. );
  25. BatchOperator <?> inOp1 = new MemSourceBatchOp(df, "id int, text string");
  26. StreamOperator <?> inOp2 = new MemSourceStreamOp(df, "id int, text string");
  27. BatchOperator <?> segment = new SegmentBatchOp().setSelectedCol("text").linkFrom(inOp1);
  28. BatchOperator <?> remover = new StopWordsRemoverBatchOp().setSelectedCol("text").linkFrom(segment);
  29. BatchOperator <?> keywords =
  30. new KeywordsExtractionBatchOp().setSelectedCol("text").setMethod("TF_IDF").setTopN(
  31. 3).linkFrom(remover);
  32. keywords.print();
  33. StreamOperator <?> segment2 = new SegmentStreamOp().setSelectedCol("text").linkFrom(inOp2);
  34. StreamOperator <?> remover2 = new StopWordsRemoverStreamOp().setSelectedCol("text").linkFrom(segment2);
  35. StreamOperator <?> keywords2 = new KeywordsExtractionStreamOp().setSelectedCol("text").setTopN(3).linkFrom(
  36. remover2);
  37. keywords2.print();
  38. StreamOperator.execute();
  39. }
  40. }

运行结果

批运行结果

| text | id | | —- | —- |

| 中国 索引 文献 | 3 |

| 电磁 成像 旧书 | 0 |

| 下册 选读 文学 | 1 |

| 图解 正版 入门 | 2 |

| 全 文集 版 | 4 |

流运行结果

| id | text | | —- | —- |

| 0 | 旧书 电磁 医学 |

| 4 | 郁达夫 馆藏 文集 |

| 1 | 美国 出版社 文学 |

| 3 | 中国 文献 糖尿病 |

| 2 | 正版 华龄 图解 |