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

功能介绍

对文本按空白符进行切分操作。

使用方式

文本列通过参数 selectedCol 指定,输出列通过 outputCol 指定。

参数说明

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

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

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

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

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

代码示例

Python 代码

  1. from pyalink.alink import *
  2. import pandas as pd
  3. useLocalEnv(1)
  4. df = pd.DataFrame([
  5. [0, 'That is an English Book!'],
  6. [1, 'Do you like math?'],
  7. [2, 'Have a good day!']
  8. ])
  9. inOp1 = BatchOperator.fromDataframe(df, schemaStr='id long, text string')
  10. op = TokenizerBatchOp().setSelectedCol("text")
  11. op.linkFrom(inOp1).print()
  12. inOp2 = StreamOperator.fromDataframe(df, schemaStr='id long, text string')
  13. op2 = TokenizerStreamOp().setSelectedCol("text")
  14. op2.linkFrom(inOp2).print()
  15. 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.TokenizerBatchOp;
  4. import com.alibaba.alink.operator.batch.source.MemSourceBatchOp;
  5. import com.alibaba.alink.operator.stream.StreamOperator;
  6. import com.alibaba.alink.operator.stream.nlp.TokenizerStreamOp;
  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 TokenizerStreamOpTest {
  12. @Test
  13. public void testTokenizerStreamOp() throws Exception {
  14. List <Row> df = Arrays.asList(
  15. Row.of(0, "That is an English Book!"),
  16. Row.of(1, "Do you like math?"),
  17. Row.of(2, "Have a good day!")
  18. );
  19. BatchOperator <?> inOp1 = new MemSourceBatchOp(df, "id int, text string");
  20. BatchOperator <?> op = new TokenizerBatchOp().setSelectedCol("text");
  21. op.linkFrom(inOp1).print();
  22. StreamOperator <?> inOp2 = new MemSourceStreamOp(df, "id int, text string");
  23. StreamOperator <?> op2 = new TokenizerStreamOp().setSelectedCol("text");
  24. op2.linkFrom(inOp2).print();
  25. StreamOperator.execute();
  26. }
  27. }

运行结果

批运行结果

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

| 0 | that is an english book! |

| 1 | do you like math? |

| 2 | have a good day! |

流运行结果

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

| 0 | that is an english book! |

| 2 | have a good day! |

| 1 | do you like math? |