hive UDF 提取文本中的地名
来源:互联网 发布:成都网络培训平台 编辑:程序博客网 时间:2024/05/07 07:44
有时候,需要从文本字符串提取出地区名,为了在HIVE中使用方便,开发了一个HIVE UDF,核心在于使用了hanLP这样一个中文文本处理的神器。详细代码如下:
import com.hankcs.hanlp.HanLP;import com.hankcs.hanlp.seg.Segment;import com.hankcs.hanlp.seg.common.Term;import org.apache.hadoop.hive.ql.exec.UDF;import org.apache.hadoop.io.Text;import java.util.ArrayList;import java.util.List;import java.util.regex.Pattern;public class placeExtract extends UDF{ public Text evaluate(Text inputStr){ if(inputStr == null) return null; Text result = new Text(); Segment segment = HanLP.newSegment().enablePlaceRecognize(true); List<Term> termList = segment.seg(inputStr.toString()); StringBuilder places = new StringBuilder(); for(int i=0; i< termList.size(); i++){ String s = termList.get(i).toString(); if(Pattern.matches(".*/ns$",s)){ places.append(s.replace("/ns","")); places.append(" "); } } result.set(places.toString()); return result; } public static void main(String[] args){ String[] testCase = new String[]{ "北京朝阳区大妈", "山东青岛大虾" }; Segment segment = HanLP.newSegment().enablePlaceRecognize(true); for(String sentence : testCase){ List<Term> termList = segment.seg(sentence); StringBuilder places = new StringBuilder(); for(int i=0; i< termList.size(); i++){ String s = termList.get(i).toString(); if(Pattern.matches(".*/ns$",s)){ places.append(s.replace("/ns","")); places.append(" "); } } System.out.println(places); } }}
0 0
- hive UDF 提取文本中的地名
- hive UDF 提取文本中的国家名
- hive中的udf时间函数用法
- hive中的udf时间函数用法
- hive中的udf时间函数用法
- hive中的udf时间函数用法
- hive UDF 过滤字符串中的中英文标点符号
- Hive中的UDF及自定义函数示例
- Hive中的用户自定义函数UDF
- hive udf
- hive-udf
- hive UDF
- hive UDF
- hive UDF
- hive udf
- hive udf
- Hive UDF
- HIVE UDF
- 欧路词典 v11.5.3 最新版
- myeclipse8.5 普通项目转换为web项目
- Java IO之对象的序列化、ObjectInputStream和ObjectOutputStream类
- iOS绘图教程
- linux中env,export, set的区别
- hive UDF 提取文本中的地名
- 【Android】自动判断调试环境决定是否打印日志
- 非常简单的适用的ajax提交数据
- 陌陌记录导出助手下载 v13.11.11
- iOS: setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key name.
- 泸州NGN属南气矿工程----Solris操作系统搭建调试
- Android SDK,ADT,API 版本的对应关系
- 修饰符
- 改变自己就是改变世界的开始