Code Monkey home page Code Monkey logo

pinyin's Introduction

Pinyin Build Status

基于CC-CEDICT词典的中文转拼音工具, 更准确的汉字转拼音解决方案。 CC-CEDICT.

use \Overtrue\Pinyin;

echo Pinyin::pinyin('带着希望去旅行,比到达终点更美好');
// dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo

//多音字
// 了
Pinyin::pinyin('了然'); // liǎo rán
Pinyin::pinyin('来了'); // lái le

// 还
Pinyin::pinyin('还有'); // hái yǒu
Pinyin::pinyin('交还'); // jiāo huán

// 什
Pinyin::pinyin('什么'); // shén me
Pinyin::pinyin('什锦'); // shí jǐn

// 便
Pinyin::pinyin('便当'); // biàn dāng
Pinyin::pinyin('便宜'); // pián yí

// 剥
Pinyin::pinyin('剥皮'); // bāo pí
Pinyin::pinyin('剥皮器'); // bō pí qì

// 不
Pinyin::pinyin('赔不是'); // péi bú shi
Pinyin::pinyin('跑了和尚,跑不了庙'); // pǎo le hé shàng , pǎo bù liǎo miào

// 降
Pinyin::pinyin('降温'); // jiàng wēn
Pinyin::pinyin('投降'); // tóu xiáng

// 都
Pinyin::pinyin('首都'); // shǒu dū
Pinyin::pinyin('都什么年代了'); // dōu shén me nián dài le

// 乐
Pinyin::pinyin('快乐'); // kuài lè
Pinyin::pinyin('音乐'); // yīn yuè

// 长
Pinyin::pinyin('成长'); // chéng zhǎng
Pinyin::pinyin('长江'); // cháng jiāng

// 难
Pinyin::pinyin('难民'); // nàn mín
Pinyin::pinyin('难过'); // nán guò
...

安装

  1. 使用 Composer 安装:

    composer require overtrue/pinyin >=1.4
    

    或者在你的项目composer.json加入:

    {
        "require": {
            "overtrue/pinyin": ">=1.4"
        }
    }
  2. 直接下载文件 src/Overtrue/Pinyin.php 引入到项目中。

使用

<?php
use \Overtrue\Pinyin;

//获取拼音
echo Pinyin::pinyin('带着希望去旅行,比到达终点更美好');
//或者: Overtrue\pinyin($string);
// dài zhe xī wàng qù lǔ xíng bǐ dào dá zhōng diǎn gèng měi hǎo

//获取首字母
echo Pinyin::letter('带着希望去旅行,比到达终点更美好');
// D Z X W Q L X B D D Z D G M H

设置

  • delimiter 分隔符,默认为一个空格 ' ';
  • traditional 繁体
  • accent 是否输出音调;
  • letter 只输出首字母,或者直接使用Pinyin::letter($string);
  • only_chinese 只保留$string中中文部分。
  • 全局设置:* Pinyin::set('delimiter', '-');

  • 临时设置:* Pinyin::pinyin($word, $settings) 在调用的方法后传参

example:

Pinyin::set('delimiter', '-');//全局
echo Pinyin::pinyin('带着希望去旅行,比到达终点更美好');

// dài-zhe-xī-wàng-qù-lǔ-xíng-bǐ-dào-dá-zhōng-diǎn-gèng-měi-hǎo
$setting = [
			'delimiter' => '-',
			'accent' => false,
		   ];

echo Pinyin::pinyin('带着希望去旅行,比到达终点更美好', $setting);//这里的setting只是临时修改,并非全局设置

// dai-zhe-xi-wang-qu-lu-xing-bi-dao-da-zhong-dian-geng-mei-hao
Pinyin::set('accent', false);
echo Pinyin::pinyin('带着希望去旅行,比到达终点更美好');

// dai zhe xi wang qu lu xing bi dao da zhong dian geng mei hao

TODO

  • 添加获取首字母;
  • 支持繁体;
  • 添加补充词典;
  • 添加词频字典,根据词频提高未匹配词典时多音字准确度;

Contribution

欢迎提意见及完善补充词库 src/Overtrue/data/additional.php! 💋

参考

License

MIT

pinyin's People

Contributors

overtrue avatar

Watchers

 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.