Giter VIP home page Giter VIP logo

pinyin's Introduction

Pinyin Build Status

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

use \Overtrue\Pinyin\Pinyin; //请注意1.x系列的命名空间不同

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:2.*
    

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

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

使用

<?php
use \Overtrue\Pinyin\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

//获取首字母
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

在Laravel中使用

composer require overtrue/pinyin:2.*

在laravel配置文件中app.phpproviders里加入:

'Overtrue\Pinyin\PinyinServiceProvider',

然后看起来可能是这样:

	'providers' =>
		//...
		'Illuminate\Validation\ValidationServiceProvider',
		'Illuminate\View\ViewServiceProvider',
		'Overtrue\Pinyin\PinyinServiceProvider',
	],

然后你可以添加配置文件:config/pinyin.php(注意:此项为可选,如果不需要设置则不用创建此文件):

<?php

return [
	'delimiter' => '-',
	'accent' => false,
	//...
];

以上的设置会是全局的设置,如需临时设置请在方法里传参,例如:

Pinyin::letter('您好世界', ['delimiter' => '-']); //N-H-S-J

使用

与上面的使用方法一样:

use \Overtrue\Pinyin\Pinyin;

//...

$pinyin = Pinyin::pinyin("带着希望去旅行,比到达终点更美好");

说明

  • 1.x的命名空间是\Overtrue,2.x的命名空间是\Overtrue\Pinyin(之前的写法不规范 🙈)

TODO

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

Contribution

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

参考

License

MIT

pinyin's People

Contributors

overtrue 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.