Coder Social home page Coder Social logo

pinin's Introduction

PinIn Download

一个用于解决各类汉语拼音匹配问题的 Java 库。对即时匹配提供基于 NFA 的实现,对索引匹配提供类后缀树的实现。除此之外,它还可以将汉字转换为拼音字符串,包括 ASCII,Unicode 和注音符号。

特性

  • 极为灵活的简拼组合
  • 7 种模糊音选项
  • 支持全拼,双拼(自然码,小鹤),注音(大千)
  • 提供即时匹配逻辑和基于缓存的匹配逻辑
  • 允许实时的配置切换(包括模糊音以及键盘)

对于“**”,可以允许的搜索串包括但不限于“**”,“中guo”,“zhongguo”,“zhong国”,“zhong1国”,“zh1国”,“zh国”。 基于模糊音设置,还允许“zong国”,“z国”等。

双拼输入尚在测试阶段,并且不(也不会)支持字形码。重码过多时,可以使用声调作为辅助码。

对于原理和思路,参见 再谈拼音搜索 系列。

性能

性能测试使用 Enigmatica 整合导出的 测试样本。共约 37k 词条,中英混合,约 400k 字符,容量约 900 KB。性能如下:

部分匹配

匹配逻辑 构建耗时 预热耗时 搜索耗时 内存使用
TreeSearcher 210ms N/A 0.19ms 9.50MB
SimpleSearcher 27ms N/A 9.1ms 1.84MB
CachedSearcher 28ms 16ms 0.55ms 见备注
遍历拼音匹配 N/A N/A 23ms N/A
遍历 contains N/A N/A 0.53ms N/A

前缀匹配

匹配逻辑 构建耗时 预热耗时 搜索耗时 内存使用
TreeSearcher 62.5ms N/A 0.083ms 2.80MB
SimpleSearcher 30ms N/A 2.4ms 1.84MB
CachedSearcher 28ms 2.8ms 0.10ms 见备注
遍历拼音匹配 N/A N/A 8.8ms N/A
遍历 startsWith N/A N/A 0.53ms N/A

CachedSearcher 的内存使用和搜索速度在不同场景下可能会有明显波动,一般介于 TreeSearcherSimpleSearcher 之间。

对于 TreeSearcherCachedSearcher,一些常量参数可以进一步调整,从而在速度与内存消耗间取得平衡。

示例

你可以轻松得使用 JitPack 将 PinIn 导入到你的 Gradle 项目中。

repositories {
  maven { url 'https://jitpack.io' }
}

dependencies {
  implementation 'com.github.Towdium:PinIn:Version'
}

下面的代码展示了 PinIn 的一些基础接口的使用方式。更多示例参见 测试代码

public static void main(String[]args){
    // context
    PinIn p=new PinIn();

    // direct match
    boolean result1=p.contains("测试文本","ceshi");

    // indexed match
    Searcher<Integer> searcher=new TreeSearcher<>(CONTAIN,p));
    p.put("测试文本",0);
    boolean result2=searcher.search("ceshi").contains(0);

    // fuzzy spelling
    p.config().fSh2S(true).commit();  // don't forget to commit config
    boolean result3=p.contains("测试文本","cesi");

    // pinyin format
    Char c=p.genChar('圆');
    Pinyin y=c.pinyins()[0];
    String s1=y.format(UNICODE)  // yuán
    String s2=y.format(PHONETIC)  // ㄩㄢˊ
}

致谢

本项目依赖于 Fastutil。在 shadow 版 Jar 文件中内置了一个裁剪过的实现,使用纯净版 Jar 文件时则需要用户自行配置。

内置的拼音数据来自于 地球拼音pinyin-data

Hava fun!

pinin's People

Contributors

towdium avatar vfyjxf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

vfyjxf texblock

pinin's Issues

关于混合输入时匹配不上的问题

当使用类似于toding、 gudong 、ashanyan这些情况时,会无法匹配对应的文字,即铜锭、滚动、安山岩
下面使用PinIn1.5.0版本情况下匹配的情况。

image
image
image


image
image
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.