Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >中文分词代码(此代码为作者多年经验总结,以前发表过VB,PB版本)

中文分词代码(此代码为作者多年经验总结,以前发表过VB,PB版本)

作者头像
jack.yang
发布于 2025-04-05 03:11:42
发布于 2025-04-05 03:11:42
490
举报

/*  * created by yzh 2004.5.12  * 请大家引用时保留这段作者声明,此代码为开源代码;使用不受限制。  * 中文分词代码  *此代码为作者多年经验总结,以前发表过VB,PB版本 */

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Locale; import java.util.TreeMap; import java.util.TreeSet;

public class ChineseSegmenter {

   private static ChineseSegmenter segmenter = null;

   // private Hashtable zhwords;    private TreeMap zhwords;

   private TreeSet cforeign, cnumbers;

   // Char form    public final static int TRAD = 0;

   public final static int SIMP = 1;

   public final static int BOTH = 2;

   // Charform is TRAD, SIMP or BOTH    private ChineseSegmenter(int charform, boolean loadwordfile) {       cforeign = new TreeSet();       cnumbers = new TreeSet();

      if (charform == SIMP) {          loadset(cnumbers, "data/snumbers_u8.txt");          loadset(cforeign, "data/sforeign_u8.txt");       } else if (charform == TRAD) {          loadset(cnumbers, "data/tnumbers_u8.txt");          loadset(cforeign, "data/tforeign_u8.txt");       } else { // BOTH          loadset(cnumbers, "data/snumbers_u8.txt");          loadset(cforeign, "data/sforeign_u8.txt");          loadset(cnumbers, "data/tnumbers_u8.txt");          loadset(cforeign, "data/tforeign_u8.txt");       }

      // zhwords = new Hashtable(120000);       zhwords = new TreeMap();

      if (!loadwordfile) {          return;       }

      String newword = null;       try {          InputStream worddata = null;          if (charform == SIMP) {             worddata = getClass().getResourceAsStream("simplexu8.txt");          } else if (charform == TRAD) {             worddata = getClass().getResourceAsStream("tradlexu8.txt");          } else if (charform == BOTH) {             worddata = getClass().getResourceAsStream("bothlexu8.txt");          }          BufferedReader in = new BufferedReader(new InputStreamReader(                worddata, "UTF8"));          while ((newword = in.readLine()) != null) {             if ((newword.indexOf("#") == -1) && (newword.length() < 5)) {

               zhwords.put(newword.intern(), "1");

               if (newword.length() == 3) {                   if (zhwords.containsKey(newword.substring(0, 2)                         .intern()) == false) {                      zhwords.put(newword.substring(0, 2).intern(), "2");                   }                }

               if (newword.length() == 4) {                   if (zhwords.containsKey(newword.substring(0, 2)                         .intern()) == false) {                      zhwords.put(newword.substring(0, 2).intern(), "2");                   }                   if (zhwords.containsKey(newword.substring(0, 3)                         .intern()) == false) {                      zhwords.put(newword.substring(0, 3).intern(), "2");                   }                }             }          }          in.close();       } catch (IOException e) {          e.printStackTrace();       }

   }    public synchronized static void reset() {       ChineseSegmenter.segmenter = null;    }

   public synchronized static ChineseSegmenter getGBSegmenter() {       Locale.setDefault(Locale.SIMPLIFIED_CHINESE);       if (ChineseSegmenter.segmenter == null) {          ChineseSegmenter.segmenter = new ChineseSegmenter(ChineseSegmenter.SIMP, true);       }       return ChineseSegmenter.segmenter;    }

   public synchronized static ChineseSegmenter getBig5Segmenter() {       Locale.setDefault(Locale.TRADITIONAL_CHINESE);       if (ChineseSegmenter.segmenter == null) {          ChineseSegmenter.segmenter = new ChineseSegmenter(ChineseSegmenter.TRAD, true);       }       return ChineseSegmenter.segmenter;    }

   private void loadset(TreeSet targetset, String sourcefile) {       String dataline;       try {          InputStream setdata = getClass().getResourceAsStream(sourcefile);          BufferedReader in = new BufferedReader(new InputStreamReader(                setdata, "UTF-8"));          while ((dataline = in.readLine()) != null) {             if ((dataline.indexOf("#") > -1) || (dataline.length() == 0)) {                continue;             }             targetset.add(dataline.intern());          }          in.close();       } catch (Exception e) {          System.err.println("Exception loading data file" + sourcefile + " "                + e);          e.printStackTrace();       }

   }

   public boolean isNumber(String testword) {       boolean result = true;       for (int i = 0; i < testword.length(); i++) {          if (cnumbers.contains(testword.substring(i, i + 1).intern()) == false) {             result = false;             break;          }       }       return result;    }

   public boolean isAllForeign(String testword) {       boolean result = true;       for (int i = 0; i < testword.length(); i++) {          if (cforeign.contains(testword.substring(i, i + 1).intern()) == false) {             result = false;             break;          }       }

      return result;    }

   public boolean isNotCJK(String testword) {       boolean result = true;       for (int i = 0; i < testword.length(); i++) {          if (Character.UnicodeBlock.of(testword.charAt(i)) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {             result = false;             break;          }       }

      return result;    }

   public String segmentLine(String cline, String separator) {       StringBuffer currentword = new StringBuffer();       StringBuffer outline = new StringBuffer();       int i, clength;       char currentchar;       // separator = " ";

      clength = cline.length();       for (i = 0; i < clength; i++) {          currentchar = cline.charAt(i);          if (Character.UnicodeBlock.of(currentchar) == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS                || isNumber(cline.substring(i, i + 1)) == true) {             // Character in CJK block             if (currentword.length() == 0) { // start looking for next                                        // word                if (i > 0                      && (Character.isWhitespace(cline.charAt(i - 1)) == false)) {                   outline.append(separator);                }                currentword.append(currentchar);

            } else {                if (zhwords.containsKey(new String(currentword.toString()                      + currentchar).intern()) == true                      && ((String) (zhwords.get(new String(currentword                            .toString()                            + currentchar).intern()))).equals("1") == true) {                   // word is in lexicon                   currentword.append(currentchar);                } else if (isAllForeign(currentword.toString())                      && cforeign.contains(new String(                            new char[] { currentchar }).intern())                      && i + 2 < clength                      && (zhwords.containsKey(cline.substring(i, i + 2)                            .intern()) == false)) {                   // Possible a transliteration of a foreign name                   currentword.append(currentchar);                } else if (isNumber(currentword.toString())                      && cnumbers.contains(new String(                            new char[] { currentchar }).intern())                /*                 * && (i + 2 < clength) &&                 * (zhwords.containsKey(cline.substring(i, i+2).intern()) ==                 * false)                 */) {                   // Put all consecutive number characters together                   currentword.append(currentchar);                } else if ((zhwords.containsKey(new String(currentword                      .toString()                      + currentchar).intern()))                      && (((String) (zhwords.get(new String(currentword                            .toString()                            + currentchar).intern()))).equals("2") == true)                      && i + 1 < clength                      && (zhwords.containsKey(new String(currentword                            .toString()                            + currentchar + cline.charAt(i + 1))                            .intern()) == true)) {                   // Starts a word in the lexicon                   currentword.append(currentchar);

               } else { // Start anew                      outline.append(currentword.toString());                   if (Character.isWhitespace(currentchar) == false) {                      outline.append(separator);                   }                   currentword.setLength(0);                   currentword.append(currentchar);                }             }

         } else { // Not chinese character             // System.err.println("not cjk");             if (currentword.length() > 0) {                outline.append(currentword.toString());                if (Character.isWhitespace(currentchar) == false) {                   outline.append(separator);                }                currentword.setLength(0);             }             outline.append(currentchar);          }       }

      outline.append(currentword.toString());

      return outline.toString();       // return offsets;    }

   public static void main(String[] args) throws Exception {

      ChineseSegmenter seg = ChineseSegmenter.getGBSegmenter();       System.out.println(seg.segmentLine("Some string in chinese.", " "));    }

}

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2006-03-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
深入了解平均精度(mAP):通过精确率-召回率曲线评估目标检测性能
平均精度(Average Precision,mAP)是一种常用的用于评估目标检测模型性能的指标。在目标检测任务中,模型需要识别图像中的不同目标,并返回它们的边界框(bounding box)和类别。mAP用于综合考虑模型在不同类别上的准确度和召回率。
deephub
2023/08/28
2.7K0
深入了解平均精度(mAP):通过精确率-召回率曲线评估目标检测性能
什么是mAP ? 比较目标检测模型性能的统计量,了解一下?
翻译 | 张建军 出品 | 人工智能头条(公众号ID:AI_Thinker) 在机器学习领域,对于大多数常见问题,通常会有多个模型可供选择。当然,每个模型会有自己的特性,并会受到不同因素的影响而表现不同。 每个模型的好坏是通过评价它在某个数据集上的性能来判断的,这个数据集通常被叫做“验证/测试”数据集。这个性能由不同的统计量来度量,包括准确率( accuracy )、精确率( precision )、召回率( recall )等等。选择我们会根据某个特定的应用场景来选择相应的统计量。而对每个应用来说,找到
用户1737318
2018/06/05
1K0
YOLO 目标检测实战项目『原理篇』
在目标检测中,IoU 为预测框 (Prediction) 和真实框 (Ground truth) 的交并比。如下图所示,在关于小猫的目标检测中,紫线边框为预测框 (Prediction),红线边框为真实框 (Ground truth)。
机器视觉CV
2019/11/14
4.7K1
YOLO 目标检测实战项目『原理篇』
什么是MAP? 理解目标检测模型中的性能评估
【导读】近日,机器学习工程师Tarang Shah发布一篇文章,探讨了机器学习中模型的度量指标的相关问题。本文首先介绍了机器学习中两个比较直观和常用的度量指标:精确度和召回率,然后详细讲解了目标检测领
WZEARW
2018/04/13
3.2K0
什么是MAP? 理解目标检测模型中的性能评估
目标检测技术指标mAP:识别准确率IOU:检测效果
mAP:识别准确率 mAP在目标检测中用于判断识别的准确率,即用于衡量物品被检测出的概率,其跟以下两个指标有关: Precision(准确率):检测出的“物品有多少是真的物品 Recall(召回率):数据集中的物品有多少被检出 对于以上两个概念,将其置于标准二分类问题框架下有以下公式: $$ Precision = \cfrac{TP}{TP+FP} \\ Recall = \cfrac{TP}{TP+FN} $$ 对于以上,有: TP:正例,被识别为正例 FP:反例,被识别为正例 TN:反例,被识别为正
月见樽
2018/07/04
2K0
目标检测中的平均精度(mAP)详解--建议收藏+掌握
本文将详细介绍目标检测中的平均精度(mAP),建议收藏并掌握。(公众号:OpenCV与AI深度学习)
Color Space
2022/09/26
9.8K0
ECCV 2018 | 旷视科技Oral论文解读:IoU-Net让目标检测用上定位置信度
论文:Acquisition of Localization Confidence for Accurate Object Detection
机器之心
2018/08/07
1.6K0
ECCV 2018 | 旷视科技Oral论文解读:IoU-Net让目标检测用上定位置信度
利用mAP评估目标检测模型
在本文中,我们将了解如何使用 precision 和召回率来计算平均精度 (mAP)。mAP 将真实边界框与检测到的框进行比较并返回分数。分数越高,模型的检测越准确。
数据科学工厂
2023/01/19
8550
Python 深度学习目标检测评价指标
准确率 (Accuracy),混淆矩阵 (Confusion Matrix),精确率(Precision),召回率(Recall),平均正确率(AP),mean Average Precision(mAP),交除并(IoU),ROC + AUC,非极大值抑制(NMS)。
用户9925864
2022/07/27
8990
Python 深度学习目标检测评价指标
【深度学习】目标检测
目标检测(Object Detection)的任务是找出图像中所有感兴趣的目标(物体),确定它们的类别和位置,是计算机视觉领域的核心问题之一。由于各类物体有不同的外观、形状和姿态,加上成像时光照、遮挡等因素的干扰,目标检测一直是计算机视觉领域最具有挑战性的问题。
杨丝儿
2022/03/01
3K0
【深度学习】目标检测
目标检测任务中的一些评估准则
本篇文章介绍一下目标检测中常用的一些评估准则,大家跑 yolo 的时候可能看着一堆输出不知道啥意思,希望这篇文章能够解决大家的疑惑,主要是翻译 GitHub 上的一个 repo,原文是英文写的,链接在这里,写的挺不错,就翻译过来给英文不好的同学看看,另外还加了几个项目中没有提到的准则
棒棒鸡不棒
2022/09/02
9660
目标检测任务中的一些评估准则
目标检测mAP计算方式
目标检测中常见的mAP计算说起来比较麻烦,所以结合VOC的计算代码进行一次详细的解析。
泽霖
2023/11/26
5510
业余AI与专业AI的区别,就在这些评估指标上
嘿,AI探险家们!你是否曾经花了好几周训练了一个"完美"的模型,却发现它在实际应用中表现得像个"学渣"?别担心,我们都经历过这种痛苦。事实上,这正是业余AI实践者和专业人士之间的重要分水岭——专业人士知道,评估模型不能只看它在训练数据上多么优秀,而是需要一套科学的"成绩单"来衡量它在未知数据上的真实表现。
martinzh7
2025/05/30
1190
业余AI与专业AI的区别,就在这些评估指标上
ADA-YOLO | YOLOv8+注意力+Adaptive Head,相对YOLOv8,mAP提升3%+118FPS
近年来,目标检测技术取得了显著的进展,使得可以实现对解剖结构、病变或异常的自动识别和定位。多年来,目标检测方法取得了重大的进步,这是由于大规模的带有标注的数据集的出现和深度学习技术的开发所驱动的。这些技术在改善医疗诊断和治疗结果方面展示出巨大的潜力。
集智书童公众号
2024/01/17
1.3K0
ADA-YOLO | YOLOv8+注意力+Adaptive Head,相对YOLOv8,mAP提升3%+118FPS
【小知识】目标检测各类指标概念总结
目标检测论文中出现过很多容易混淆的评价指标,比如FLOPS、FLOPs、 GFLOPS,包括最基本的AP、mAP这些定义,索性将这些基本概念搞清楚,做个总结。
机器学习AI算法工程
2024/07/04
4510
【小知识】目标检测各类指标概念总结
从 YOLOv1 到 YOLOv2:目标检测的进化之路
你有没有想过,当你用手机拍一张照片,里面的人、车、狗是怎么被自动识别出来的?这背后靠的就是目标检测技术。目标检测是计算机视觉中的一个重要领域,它不仅要回答“图片里有什么”,还要告诉你“这些东西在哪里”。今天,我们要聊的是目标检测领域的两个经典模型:YOLOv1 和 YOLOv2。它们的名字听起来很酷——“You Only Look Once”(你只看一次),不仅名字帅,性能也很强。这篇博客将带你走进 YOLO 的世界,聊聊它们的原理、区别,以及那些听起来高大上的概念,比如 mAP、FPS、IoU 等。我们会尽量用大白话解释,并在后面深入讲解数学公式和代码实现,让你轻松看懂!
机器学习司猫白
2025/03/15
1050
从 YOLOv1 到 YOLOv2:目标检测的进化之路
YOLO系列改进 | YOLOF的小小改进升级之轻量化TE-YOLOF
显微图像中的血细胞分析通过识别不同的细胞对象在疾病识别领域中起着至关重要的作用。在血细胞领域,血液中有三种重要成分:白细胞(WBC)、红细胞(RBC)和血小板。这些血细胞的比例和数量严重影响医生对疾病的判断。因此,找到一种基于深度卷积神经网络的目标检测算法来准确高效地检测血细胞,可以提高医疗系统的效率。
AiCharm
2023/05/15
5730
YOLO系列改进 | YOLOF的小小改进升级之轻量化TE-YOLOF
目标检测中常提到的IoU和mAP究竟是什么?
intersect over union,中文:交并比。指目标预测框和真实框的交集和并集的比例。
AI粉嫩特工队
2019/09/26
3.4K0
目标检测中常提到的IoU和mAP究竟是什么?
绝对不容错过:最完整的检测模型评估指标mAP计算指南(附代码)在这里!
作者: 叶 虎 编辑: 赵一帆 前 言 本文翻译自Measuring Object Detection models - mAP - What is Mean Average Pr
机器学习算法工程师
2018/07/27
4.3K0
绝对不容错过:最完整的检测模型评估指标mAP计算指南(附代码)在这里!
【必备】目标检测中的评价指标有哪些?
上期我们一起学习了全卷积神经网络FCN,今天我们看下目标检测中的评价指标都有哪些?
智能算法
2020/05/08
17.7K0
推荐阅读
相关推荐
深入了解平均精度(mAP):通过精确率-召回率曲线评估目标检测性能
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档