设为首页 收藏本站
查看: 1199|回复: 0

[经验分享] solr入门之pinyin4j源代码改写动态加入扩展词及整合进war项目中

[复制链接]

尚未签到

发表于 2017-12-19 22:50:52 | 显示全部楼层 |阅读模式
批量写入功能加入  

  
package com.gome.mx.plus.pinyin.ext;
  

  
import java.io.BufferedWriter;
  
import java.io.File;
  
import java.io.FileInputStream;
  
import java.io.FileOutputStream;
  
import java.io.IOException;
  
import java.io.InputStream;
  
import java.io.OutputStreamWriter;
  
import java.util.HashSet;
  
import java.util.Map;
  
import java.util.Map.Entry;
  
import java.util.Set;
  

  
import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
  

  
import net.sourceforge.pinyin4j.ChineseToPinyinResource;
  
import net.sourceforge.pinyin4j.ResourceHelper;
  
import net.sourceforge.pinyin4j.multipinyin.MultiPinyinConfig;
  
import net.sourceforge.pinyin4j.multipinyin.Trie;
  
/**
  * 将汉语和拼音写入指定的文件里--文件位置能够指定
  * 而且能够动态的载入  不须要重新启动服务
  * 还能指定是否又一次写 还是追加的方式
  * 还能够将原来已经存在的拼音合并过来--能够指定
  * @author songqinghu
  *
  */

  
public>  

  //这里改为系统的绝对路径
  private static String path;
  

  private static boolean flag = true;//能够设置文件位置
  /**
  * @描写叙述:获取配置文件的位置 ---仅仅能设置一次
  * @return void
  * @exception
  * @createTime:2016年4月6日
  * @author: songqinghu
  */
  public static void setPath(String path){
  if(flag){
  PYWriterUtils.path = path;
  flag = false;//仅仅能设置 一次
  }
  }
  

  public static String getPath(){
  return PYWriterUtils.path;
  }
  


  private static>  

  

  /**
  *
  * @描写叙述:默认写入的方式  设置为追加模式  合并已经存在的拼音为一个
  * @param word  汉字
  * @param pinyin 拼音
  * @param voice  声调
  * @return
  * @return boolean  是否成功
  * @exception
  * @createTime:2016年4月6日
  * @author: songqinghu
  * @throws Exception
  */
  public static boolean dufaultWriter(String word,String pinyin,Integer voice) throws Exception{
  return writerControler(word, pinyin, voice, true, true);
  }
  /**
  *
  * @描写叙述:能够设置的写入方式  --这里还要添加一个批量写入的功能  本方法仅仅是处理一个汉字
  * @param word  汉字
  * @param pinyin 拼音
  * @param voice  声调
  * @param additional 是否追加到文件后
  * @param merge 是否合并已经出现的拼音到文件里
  * @return
  * @return boolean
  * @exception
  * @createTime:2016年4月6日
  * @author: songqinghu
  * @throws Exception
  * 龦
  */
  public static boolean writerControler(String word,String pinyin,Integer voice,
  boolean additional ,boolean merge) throws Exception{
  

  String path = PYWriterUtils.path;
  if (path != null) {
  File userMultiPinyinFile = new File(path);
  if (userMultiPinyinFile.exists()) {
  //获取
  BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(userMultiPinyinFile, additional)));
  //加入音调
  pinyin = pinyin + voice;
  //写入--16进制  查询 --
  if(word !=null && word.length()>0){
  char c = word.toCharArray()[0];
  if(c>128){//是汉字
  String unicode = Integer.toHexString(c).toUpperCase();//编码
  if(merge){//假设要合并
  Trie trie = ChineseToPinyinResource.getInstance().getUnicodeToHanyuPinyinTable();
  

  if(trie.get(unicode)!=null){ //存在了编码和拼音相应关系---这里最好在推断一次是否存在了该拼音
  String before = trie.get(unicode).getPinyin();
  before = before.trim().substring(1, before.trim().length()-1);//去除()
  //存在了 就不加入进去了
  boolean flag = false;
  String[] words = before.split(",");
  for (String str : words) {
  if(str.equals(pinyin)){
  flag = true; //存在该拼音
  break;
  }
  }
  if(flag){
  pinyin = before;
  }else{
  pinyin = before +Field.COMMA+ pinyin ;
  }
  }
  //不存在  不须要改变pinyin
  }
  pinyin = addSymbol(pinyin);
  writer.write(unicode+Field.SPACE+pinyin);
  writer.newLine();
  }
  }
  writer.flush();
  writer.close();
  //写入完毕  更新词库
  reloadText();
  return true;
  }
  }else{
  throw new Exception("找不到用户扩展字典");
  }
  return false;
  }
  

  /**
  * 完毕批量加入的功能
  */
  /**
  *
  * @描写叙述:批量加入汉字和拼音的映射关系到自己定义词库中----这里有个问题 当 批量输入一个多音字 拼音都是map中同一个key时仅仅能提交成功一个--建议提交两次
  * @param contents  汉字  拼音  音调  这里一个汉字  能够输入多个拼音了
  * @param additional 是否追加到文件后
  * @param merge 是否合并已经出现的拼音到文件里
  * @return
  * @return boolean
  * @exception
  * @createTime:2016年4月7日
  * @author: songqinghu
  */
  public static boolean writerBatch(Map<String,Map<String,Integer>> contents,boolean additional ,boolean merge){
  //载入文件部分
  BufferedWriter writer =null;
  try {
  if (path != null) {
  File userMultiPinyinFile = new File(path);
  if (userMultiPinyinFile.exists()) {
  writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(userMultiPinyinFile, additional)));
  //写入处理部分
  Set<Entry<String, Map<String, Integer>>> entrySet = contents.entrySet();
  for (Entry<String, Map<String, Integer>> entry : entrySet) {
  String word = entry.getKey().trim();//汉语
  String pinyin = &quot;&quot;;
  for (Entry<String, Integer> content : entry.getValue().entrySet()) {
  String py = content.getKey().trim();
  Integer voice = content.getValue();
  pinyin = pinyin + py + voice+&quot;,&quot;;
  }
  //拼音加入结束  去除最后一个,
  pinyin = pinyin.substring(0, pinyin.length()-1);
  //汉字和拼音都已经处理完毕 进入单个词语写入模块 --方法 抽取出来公用
  String line = midWriter(word, pinyin, merge);
  if(line != null){
  writer.write(line);
  writer.newLine();
  }
  }
  writer.flush();
  return true;
  }
  }else{
  throw new  Exception(&quot;请配置用户词典绝对路径&quot;);
  }
  } catch (Exception e) {
  e.printStackTrace();
  }finally {
  try {
  if(writer!=null)
  writer.close();
  PYWriterUtils.reloadText();
  } catch (IOException e) {
  e.printStackTrace();
  }
  }
  return false;
  }
  /**
  *
  * @描写叙述:方法抽取--对单个字进行处理
  * @return
  * @return String 组合后的一行要写入的   形式    E4A3 (ang3,yi1,wang3)
  * @exception
  * @createTime:2016年4月7日
  * @author: songqinghu
  */
  private static String midWriter(String word ,String pinyin,boolean merge){
  

  if(word !=null && word.length()>0){
  char c = word.toCharArray()[0];
  if(c>128){//假设是汉字
  String unicode  = Integer.toHexString(c).toUpperCase();//变为16进制
  if(merge){//假设要合并 须要先取出来  在合并  取不到还要处理一下
  //获取到总的资源池
  Trie trie = ChineseToPinyinResource.getInstance().getUnicodeToHanyuPinyinTable();
  //假设存在该词语的拼音
  if(trie.get(unicode)!=null &&trie.get(unicode).getPinyin()!=null){
  String before = trie.get(unicode).getPinyin();
  //对已经处在字符串进行处理 --(xxx) (xxxx,xxxx)
  before = before.trim().substring(1, before.trim().length()-1);//去除()
  //假设存在了  就不再反复加入了
  String[] splits = before.split(&quot;,&quot;);
  String[] strings = pinyin.trim().split(&quot;,&quot;);
  Set<String> temp  = new HashSet<String>();
  //去反复
  for (String split : splits) {
  temp.add(split.trim());
  }
  for (String string : strings) {
  temp.add(string);
  }
  pinyin =&quot;&quot;;
  for (String tem : temp) {
  pinyin = pinyin + tem+Field.COMMA;
  }
  pinyin =  pinyin.substring(0,pinyin.length()-1);//去除最后一个,
  }
  //不存在 直接 保持拼音不变
  }
  //组合成写入的格式
  pinyin = addSymbol(pinyin);
  

  return unicode + Field.SPACE+pinyin;
  }
  }
  return null;
  }
  

  /**
  *
  * @描写叙述:默认批量写入功能
  * @param contents
  * @return
  * @return boolean
  * @exception
  * @createTime:2016年4月7日
  * @author: songqinghu
  */
  public static boolean defaultWriterBatch(Map<String,Map<String,Integer>> contents){
  

  return writerBatch(contents, true, true);
  }
  

  /**
  *
  * @描写叙述:当自己定义文件须要更新时,调用方法 又一次载入自己的配置文件
  * @return
  * @return boolean
  * @exception
  * @createTime:2016年4月6日
  * @author: songqinghu
  * @throws IOException
  */

  public static boolean>  

  if (path != null) {
  File userMultiPinyinFile = new File(path);
  FileInputStream is = new FileInputStream(userMultiPinyinFile);
  if(is !=null){
  ChineseToPinyinResource.getInstance().getUnicodeToHanyuPinyinTable().load(is);
  return true;
  }
  }
  return false;
  }
  

  

  

  /**
  * 加入操作符号
  */
  private static String addSymbol(String pinyin){
  return Field.LEFT_BRACKET+pinyin+Field.RIGHT_BRACKET;
  }
  

  class Field {
  static final String LEFT_BRACKET = &quot;(&quot;;
  

  static final String RIGHT_BRACKET = &quot;)&quot;;
  

  static final String COMMA = &quot;,&quot;;
  

  static final String SPACE = &quot; &quot;;
  }
  
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-425866-1-1.html 上篇帖子: solr和es的区别 下篇帖子: 使用solr界面管理工具创建core 不能用的解决方法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表