Code Monkey home page Code Monkey logo

pinyintowords's Introduction

PinyinToWords

根据拼音查找对应的词条,类似于词典或输入法功能

性能

  • 单次查询耗时 0.2ms 左右

使用方式

  • Gradle

    // Step 1. Add the JitPack repository to your build file
    // Add it in your root build.gradle at the end of repositories:
    allprojects {
      repositories {
        ...
        maven { url 'https://jitpack.io' }
      }
    }
    // Step 2. Add the dependency
    implementation 'com.github.goyourfly:PinyinToWords:VERSION'
    
  • 初始化词库,首次初始化会从词库读取拼音并生成字典树,耗时 5s 左右,后续将字典树缓存后耗时 150ms

    /**
     * cachePath 缓存路径
     * cacheType 字典树缓存类型,ProtoBuf or Json,默认 ProtoBuf
     */
    PinyinToWords.init(cachePath:File,cacheType:CacheType)
    
  • 查找词条

    // [Word(pinyin=nihao, zh=你好, freq=34121)]
    PinyinToWords.findWords("nihao")
    // [Word(pinyin=shenme, zh=什么, freq=1041223), Word(pinyin=shenme, zh=甚么, freq=4729), Word(pinyin=shenme, zh=神么, freq=127)]
    PinyinToWords.findWords("shenme")
    
  • 自定义词库

    // 重写 PathProvider
    interface PathProvider{
      // 词库文件
      fun getDictFile():String
    }
    
    // 重写 WordsParser 用于将词库的每行数据解析为 Word 对象
    interface WordsParser {
      fun parse(line: String): Word
    }
    

实现方式

  • 字典树 + 随机读

感谢

  • 词库是从 webdict 获取,共 201195 个词条,感谢大佬,原词库没有拼音,用 TinyPinyin 生成

pinyintowords's People

Contributors

goyourfly avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

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.