查看分词器分词结果

Analyzer analyzer = new WhitespaceAnalyzer();

try {

// 第一个参数只是标识性没有实际作用

TokenStream stream = analyzer.tokenStream("", new StringReader("我爱 北京 天安门"));

// 获取词与词之间的位置增量

PositionIncrementAttribute postiona = stream.addAttribute(PositionIncrementAttribute.class);

// 获取各个单词之间的偏移量

OffsetAttribute offseta = stream.addAttribute(OffsetAttribute.class);

// 获取每个单词信息

CharTermAttribute chara = stream.addAttribute(CharTermAttribute.class);

// 获取当前分词的类型

TypeAttribute typea = stream.addAttribute(TypeAttribute.class);

stream.reset();

while (stream.incrementToken()) {

System.out.print("位置增量" + postiona.getPositionIncrement() + ":\t");

System.out.println(

chara + "\t[" + offseta.startOffset() + " - " + offseta.endOffset() + "]\t<" + typea + ">");

}

System.out.println();

} catch (Exception e) {

e.printStackTrace();

}


转载于:https://my.oschina.net/payzheng/blog/645605

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值