pinyin: an R package that converts Chinese characters into Latin letters - 将中文转成汉语拼音的 R 语言包 by dapeng

View this thread on steempeak.com
· @dapeng · (edited)
$54.23
pinyin: an R package that converts Chinese characters into Latin letters - 将中文转成汉语拼音的 R 语言包
This post was written bilingually, in English and Chinese. If you use the tool introduced in this post, you are able to read both languages!

> 本文用英文和中文写作,中文在后半部分。

## Introduction

### Background 

Chinese characters are mysterious. You can more or less pronounce German, Italien or French words from their spellings even you know nothing of these languages because they are written in Latin letters, but you cannot pronounce Chinese characters from their looking. If you don't believe me, go to the cn category on steem and try. Chinese characters are not composed of Latin letters.

Fortunately, each Chinese character has a spelling in Latin letters, which is called *pinyin*. It is a must tool to learn Chinese. Here is the defination of pinyin on [Wikipedia](https://en.wikipedia.org/wiki/Pinyin):

> Pinyin, or Hànyǔ Pīnyīn, is the official romanization system for Standard Chinese in mainland China, Malaysia, Singapore, and Taiwan. It is often used to teach Standard Chinese, which is normally written using Chinese characters. The system includes four diacritics denoting tones. Pinyin without tone marks is used to spell Chinese names and words in languages written with the Latin alphabet, and also in certain computer input methods to enter Chinese characters.

> The pinyin system was developed in the 1950s by many linguists, including Zhou Youguang, based on earlier forms of romanization of Chinese. It was published by the Chinese government in 1958 and revised several times. The International Organization for Standardization (ISO) adopted pinyin as an international standard in 1982, followed by the United Nations in 1986. The system was adopted as the official standard in Taiwan in 2009, where it is used for romanization alone (in part to make areas more English-friendly) rather than for educational and computer-input purposes.

If you want to learn Chinese, you have to start with pinyin. Chinese kids learn Chinese from pinyin. You can find the pinyin of each Chinese character in a dictionary. but would it be exciting if Chinese characters are converted to pinyin automatically?

### Motivation

In my computer, many files are named in Chinese. When I want to share them with international friends, I have to convert the file names into Latin letters munally. If there are tens or hundreds of them, hmm, I would give up. What if they can automatically be done with R?

Some of my international friends are learning Chinese. When I share a nice Chinese post on Utopian.io with them, they complain that they have to look up the dictionary from time to time. The Chinese version of MS Word could help, but they have to copy and paste and adjust the layout and convert and so on. If there are multiple posts, they lose their patience. What if they can automatically be done with R?

Thus I developed an R package for converting Chinese characters into pinyin. 

Powered with R language, you can do further statistics and analysis in Chinese pronunciations with the help of pinyin package!

## Features

With R pinyin package, the tasks mentioned above can be achieved with one single command. Chinese characters can be converted into Latin letters in different ways, including the standard full spelling with or without the four tones, or the first letter of each character. You can convert either strings or text files, or rename multiple filenames.

## Quick Start

### Installation of R

[Download R](https://cran.r-project.org) and install it.

### Installation of pinyin

In R environment, run the following codes to install pinyin.

```
# stable version:
install.packages("pinyin")
# or development version:
devtools::install_github("pzhaonet/pinyin")
```

### Examples

As an example, run:

```
library('pinyin')
pinyin('羌笛何须怨杨柳春风不度玉门关')
```

then you get the full spellings with Chinese tones as
```
## [1] "qiānɡ_dí_hé_xū_yuàn_yánɡ_liǔ_chūn_fēnɡ_bú_dù_yù_mén_ɡuān"
```

You can change the separators between the words:

```
pinyin('羌笛何须怨杨柳春风不度玉门关', sep = ' ')
## [1] "qiānɡ dí hé xū yuàn yánɡ liǔ chūn fēnɡ bú dù yù mén ɡuān"
```

You can use numbers to specify the tones:

```
pinyin('羌笛何须怨杨柳春风不度玉门关', sep = ' ', method = 'tone')
## [1] "qiang1 di2 he2 xu1 yuan4 yang2 liu3 chun1 feng1 bu2 du4 yu4 men2 guan1"
```

or toneless:

```
pinyin('羌笛何须怨杨柳春风不度玉门关', sep = ' ', method = 'toneless')
## [1] "qiang di he xu yuan yang liu chun feng bu du yu men guan"
```

or only the first letter of each word:

```
pinyin('羌笛何须怨杨柳春风不度玉门关', sep = '', only_first_letter = TRUE)
## [1] "qdhxyylcfbdymɡ"
```

The function `file.rename2py()`can convert Chinese file names into pinyin, and the function `file2py()` can convert text files into pinyin.

Now you can read Chinese articles aloud to your friends and shock them!

## Link to the Repo

https://github.com/pzhaonet/pinyin

## Proof of Work

In my profile on Utopian.io, the github repo pinyin is listed in [my project page](https://utopian.io/project/pzhaonet/pinyin/github/92541456/all),  which is shown in the screenshot as follows:

![pinyin.jpg](https://res.cloudinary.com/hpiynhbhq/image/upload/v1513784911/jy8irppguojtuv4w9a4i.jpg)

The login page on github.com is shown in the following screenshot:

![pinyin2.jpg](https://res.cloudinary.com/hpiynhbhq/image/upload/v1513843020/ztvmkwbnz688itefzyjq.jpg)


---


我开发的这个 R 语言包,粗暴地用拼音取名为 [pinyin](https://github.com/pzhaonet/pinyin/),作用是把汉字转换成拼音。

有什么用呢?

有时候需要跟外国朋友分享我电脑里的一些文件,文件名是中文的,发过去人家看起来是乱码,所以我会先把文件名手动改成拉丁字母。文件少了还好,要是有成千上万个,算了,我不分享了。

中文在世界上的影响力愈来愈大,有些外国朋友在学中文。我是混 steem 的,看见好的中文帖想发给他们,他们抱怨说还得查字典。那就用中文版的 word 转换不行吗?拷贝,粘贴,调整格式,转换……烦死了。要是有多篇,算了人家没耐心弄了。

现在有了 R 语言的 pinyin 包,一条命令就能批量修改成千上万的文件名,能把多个字符串或多个文本文件转成拼音了。再说中文难学的老外,给我闭嘴。

而且,依靠 R 语言强大的数据处理、统计分析能力,你可以从 pinyin 包出发,对任何一篇文章的中文发音做文本分析了!


## 安装方法 [ān zhuānɡ fānɡ fǎ]

```{r, eval=FALSE}
install.packages('pinyin')
# or
devtools::install_github("pzhaonet/pinyin")
```

## 函数 [hán shù]

pinyin 1.0.0版包含4个函数.`pinyin()`是主函数,可以把一个带汉字的字符串转换成拼音。可以选择:

-   转换成标准的全拼 (默认 `method = 'quanpin'`),或
-   以数字表示声调 (`method = 'tone'`) , 或
-   不含声调(`method = 'toneless'`),
-   也可以选择仅保留每个字的首字母(`only_first_letter = TRUE`),
-   可以自定义相邻两字拼音的分隔符号(`sep = '_'`),
-   如果汉字字符串里边包含非汉字字符,可以选择将这些字符保留原样(`nonezh_replace = NULL`)还是转换成指定字符(如`nonezh_replace = '-'`)。

其他函数是 `pinyin()` 的延伸和示例:

- `file.rename2py()`用来对文件重命名,将文件名里的汉字转换成拼音。
- `file2py()` 用来将指定文件夹里的一个或多个文本文件里的汉字全部转换成拼音。 

## Examples 示例 [shì lì]

以参数的默认值进行转换:

``` r
library('pinyin')
pinyin('羌笛何须怨杨柳春风不度玉门关')
```

    ## [1] "qiānɡ_dí_hé_xū_yuàn_yánɡ_liǔ_chūn_fēnɡ_bú_dù_yù_mén_ɡuān"

更改相邻拼音之间的分隔符号:

``` r
pinyin('羌笛何须怨杨柳春风不度玉门关', sep = ' ')
```

    ## [1] "qiānɡ dí hé xū yuàn yánɡ liǔ chūn fēnɡ bú dù yù mén ɡuān"

用数字表示声调:

``` r
pinyin('羌笛何须怨杨柳春风不度玉门关', sep = ' ', method = 'tone')
```

    ## [1] "qiang1 di2 he2 xu1 yuan4 yang2 liu3 chun1 feng1 bu2 du4 yu4 men2 guan1"

不使用声调:

``` r
pinyin('羌笛何须怨杨柳春风不度玉门关', sep = ' ', method = 'toneless')
```

    ## [1] "qiang di he xu yuan yang liu chun feng bu du yu men guan"

每个拼音只输出首字母:

``` r
pinyin('羌笛何须怨杨柳春风不度玉门关', sep = '', only_first_letter = TRUE)
```

    ## [1] "qdhxyylcfbdymɡ"

非汉字或汉语标点的字符保留原型:

``` r
pinyin('羌笛何须怨?杨柳春风,不度玉门关.', sep = '', nonezh_replace = NULL)
```

    ## [1] "qiānɡdíhéxūyuàn?yánɡliǔchūnfēnɡ,búdùyùménɡuān."


汉语标点符号会自动转换成对应的英文标点符号:

``` r
pinyin('羌笛何须怨?杨柳春风,不度玉门关。', sep = '', nonezh_replace = NULL)
```

    ## [1] "qiānɡdíhéxūyuàn?yánɡliǔchūnfēnɡ,búdùyùménɡuān."

如果自变量是多个字符串的向量,那么只会转换第一个字符串:

``` r
pinyin(c('羌笛何须怨杨柳', '春风不度玉门关'))
```

    ## [1] "qiānɡ_dí_hé_xū_yuàn_yánɡ_liǔ"

这怎么行?哎,用 `sapply()` 嘛:

``` r
sapply(c('羌笛何须怨杨柳', '春风不度玉门关'), pinyin)
```

    ##                 羌笛何须怨杨柳                 春风不度玉门关 
    ## "qiānɡ_dí_hé_xū_yuàn_yánɡ_liǔ"  "chūn_fēnɡ_bú_dù_yù_mén_ɡuān"

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@dapeng/pinyin-an-r-package-that-converts-chineses-characters-into-latin-letters-r">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
post_id21,202,262
authordapeng
permlinkpinyin-an-r-package-that-converts-chineses-characters-into-latin-letters-r
categoryutopian-io
json_metadata"{"type": "development", "repository": {"compare_url": "https://api.github.com/repos/pzhaonet/pinyin/compare/{base}...{head}", "events_url": "https://api.github.com/repos/pzhaonet/pinyin/events", "forks": 1, "merges_url": "https://api.github.com/repos/pzhaonet/pinyin/merges", "issues_url": "https://api.github.com/repos/pzhaonet/pinyin/issues{/number}", "contents_url": "https://api.github.com/repos/pzhaonet/pinyin/contents/{+path}", "fork": false, "git_url": "git://github.com/pzhaonet/pinyin.git", "assignees_url": "https://api.github.com/repos/pzhaonet/pinyin/assignees{/user}", "owner": {"login": "pzhaonet", "type": "User", "following_url": "https://api.github.com/users/pzhaonet/following{/other_user}", "received_events_url": "https://api.github.com/users/pzhaonet/received_events", "subscriptions_url": "https://api.github.com/users/pzhaonet/subscriptions", "repos_url": "https://api.github.com/users/pzhaonet/repos", "gravatar_id": "", "organizations_url": "https://api.github.com/users/pzhaonet/orgs", "id": 4011804, "avatar_url": "https://avatars3.githubusercontent.com/u/4011804?v=4", "site_admin": false, "followers_url": "https://api.github.com/users/pzhaonet/followers", "gists_url": "https://api.github.com/users/pzhaonet/gists{/gist_id}", "html_url": "https://github.com/pzhaonet", "events_url": "https://api.github.com/users/pzhaonet/events{/privacy}", "url": "https://api.github.com/users/pzhaonet", "starred_url": "https://api.github.com/users/pzhaonet/starred{/owner}{/repo}"}, "size": 342, "issue_events_url": "https://api.github.com/repos/pzhaonet/pinyin/issues/events{/number}", "git_refs_url": "https://api.github.com/repos/pzhaonet/pinyin/git/refs{/sha}", "blobs_url": "https://api.github.com/repos/pzhaonet/pinyin/git/blobs{/sha}", "pushed_at": "2017-07-19T07:22:24Z", "watchers_count": 7, "stargazers_url": "https://api.github.com/repos/pzhaonet/pinyin/stargazers", "teams_url": "https://api.github.com/repos/pzhaonet/pinyin/teams", "comments_url": "https://api.github.com/repos/pzhaonet/pinyin/comments{/number}", "archived": false, "svn_url": "https://github.com/pzhaonet/pinyin", "id": 92541456, "subscribers_url": "https://api.github.com/repos/pzhaonet/pinyin/subscribers", "watchers": 7, "ssh_url": "git@github.com:pzhaonet/pinyin.git", "name": "pinyin", "mirror_url": null, "statuses_url": "https://api.github.com/repos/pzhaonet/pinyin/statuses/{sha}", "has_projects": true, "milestones_url": "https://api.github.com/repos/pzhaonet/pinyin/milestones{/number}", "description": "an R package for converting Chineses characters into pinyin", "has_downloads": true, "keys_url": "https://api.github.com/repos/pzhaonet/pinyin/keys{/key_id}", "url": "https://api.github.com/repos/pzhaonet/pinyin", "license": null, "html_url": "https://github.com/pzhaonet/pinyin", "commits_url": "https://api.github.com/repos/pzhaonet/pinyin/commits{/sha}", "open_issues_count": 0, "stargazers_count": 7, "branches_url": "https://api.github.com/repos/pzhaonet/pinyin/branches{/branch}", "score": 28.28726, "forks_url": "https://api.github.com/repos/pzhaonet/pinyin/forks", "deployments_url": "https://api.github.com/repos/pzhaonet/pinyin/deployments", "language": "R", "homepage": null, "trees_url": "https://api.github.com/repos/pzhaonet/pinyin/git/trees{/sha}", "has_issues": true, "collaborators_url": "https://api.github.com/repos/pzhaonet/pinyin/collaborators{/collaborator}", "git_commits_url": "https://api.github.com/repos/pzhaonet/pinyin/git/commits{/sha}", "open_issues": 0, "private": false, "git_tags_url": "https://api.github.com/repos/pzhaonet/pinyin/git/tags{/sha}", "pulls_url": "https://api.github.com/repos/pzhaonet/pinyin/pulls{/number}", "releases_url": "https://api.github.com/repos/pzhaonet/pinyin/releases{/id}", "notifications_url": "https://api.github.com/repos/pzhaonet/pinyin/notifications{?since,all,participating}", "contributors_url": "https://api.github.com/repos/pzhaonet/pinyin/contributors", "full_name": "pzhaonet/pinyin", "updated_at": "2017-10-10T06:27:45Z", "forks_count": 1, "downloads_url": "https://api.github.com/repos/pzhaonet/pinyin/downloads", "hooks_url": "https://api.github.com/repos/pzhaonet/pinyin/hooks", "languages_url": "https://api.github.com/repos/pzhaonet/pinyin/languages", "default_branch": "master", "labels_url": "https://api.github.com/repos/pzhaonet/pinyin/labels{/name}", "issue_comment_url": "https://api.github.com/repos/pzhaonet/pinyin/issues/comments{/number}", "has_pages": false, "tags_url": "https://api.github.com/repos/pzhaonet/pinyin/tags", "clone_url": "https://github.com/pzhaonet/pinyin.git", "archive_url": "https://api.github.com/repos/pzhaonet/pinyin/{archive_format}{/ref}", "has_wiki": true, "created_at": "2017-05-26T19:16:42Z", "subscription_url": "https://api.github.com/repos/pzhaonet/pinyin/subscription"}, "pullRequests": [], "format": "markdown", "image": ["https://res.cloudinary.com/hpiynhbhq/image/upload/v1513784911/jy8irppguojtuv4w9a4i.jpg"], "links": ["https://en.wikipedia.org/wiki/Pinyin", "https://cran.r-project.org", "https://utopian.io/project/pzhaonet/pinyin/github/92541456/all", "https://res.cloudinary.com/hpiynhbhq/image/upload/v1513784911/jy8irppguojtuv4w9a4i.jpg", "https://res.cloudinary.com/hpiynhbhq/image/upload/v1513843020/ztvmkwbnz688itefzyjq.jpg", "https://github.com/pzhaonet/pinyin/"], "app": "utopian/1.0.0", "platform": "github", "tags": ["utopian-io", "cn", "cn-reader", "programming", "cn-programming"], "community": "utopian", "users": ["dapeng"]}"
created2017-12-20 15:51:42
last_update2017-12-21 07:57:06
depth0
children5
net_rshares10,723,600,614,490
last_payout2017-12-27 15:51:42
cashout_time1969-12-31 23:59:59
total_payout_value39.262 SBD
curator_payout_value14.970 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length7,901
author_reputation64,235,894,740,263
root_title"pinyin: an R package that converts Chinese characters into Latin letters - 将中文转成汉语拼音的 R 语言包"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (43)
@codingdefined ·
Your contribution cannot be approved yet because it does not have **proof of work**, if your username is different in Utopian and Github you should provide the proof by providing the screenshot of logged in session and it should clearly shows the project you have contributed to. 

See the [Utopian Rules](https://utopian.io/rules). Please edit your contribution and add **proof** (links, screenshots, commits, etc) of your work, to reapply for approval.

You may edit your post [here](https://utopian.io/utopian-io/@dapeng/pinyin-an-r-package-that-converts-chineses-characters-into-latin-letters-r), as shown below: 
![](https://res.cloudinary.com/hpiynhbhq/image/upload/v1509788371/nbgbomithszxs3nxq6gx.png)

You can contact us on [Discord](https://discord.gg/UCvqCsx).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
post_id21,285,878
authorcodingdefined
permlinkre-dapeng-pinyin-an-r-package-that-converts-chineses-characters-into-latin-letters-r-20171221t051511746z
categoryutopian-io
json_metadata"{"app": "utopian/1.0.0", "community": "utopian", "tags": ["utopian-io"]}"
created2017-12-21 05:15:12
last_update2017-12-21 05:15:12
depth1
children1
net_rshares0
last_payout2017-12-28 05:15:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length828
author_reputation71,157,752,447,147
root_title"pinyin: an R package that converts Chinese characters into Latin letters - 将中文转成汉语拼音的 R 语言包"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@dapeng ·
I have added one more screenshot showing the login page of this github repo. Could you please review it? Thank you!
properties (22)
post_id21,301,585
authordapeng
permlinkre-codingdefined-re-dapeng-pinyin-an-r-package-that-converts-chineses-characters-into-latin-letters-r-20171221t075845868z
categoryutopian-io
json_metadata"{"app": "utopian/1.0.0", "community": "utopian", "tags": ["utopian-io"]}"
created2017-12-21 07:58:42
last_update2017-12-21 07:58:42
depth2
children0
net_rshares0
last_payout2017-12-28 07:58:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length115
author_reputation64,235,894,740,263
root_title"pinyin: an R package that converts Chinese characters into Latin letters - 将中文转成汉语拼音的 R 语言包"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@codingdefined ·
$0.13
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/UCvqCsx).
**[[utopian-moderator]](https://utopian.io/moderators)**
👍  
properties (23)
post_id21,305,959
authorcodingdefined
permlinkre-dapeng-pinyin-an-r-package-that-converts-chineses-characters-into-latin-letters-r-20171221t084459228z
categoryutopian-io
json_metadata"{"app": "utopian/1.0.0", "community": "utopian", "tags": ["utopian-io"]}"
created2017-12-21 08:45:00
last_update2017-12-21 08:45:00
depth1
children1
net_rshares22,240,135,779
last_payout2017-12-28 08:45:00
cashout_time1969-12-31 23:59:59
total_payout_value0.102 SBD
curator_payout_value0.032 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length172
author_reputation71,157,752,447,147
root_title"pinyin: an R package that converts Chinese characters into Latin letters - 将中文转成汉语拼音的 R 语言包"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (1)
@dapeng ·
Thank you!
properties (22)
post_id21,319,570
authordapeng
permlinkre-codingdefined-re-dapeng-pinyin-an-r-package-that-converts-chineses-characters-into-latin-letters-r-20171221t110111827z
categoryutopian-io
json_metadata"{"app": "steemit/0.1", "tags": ["utopian-io"]}"
created2017-12-21 11:01:12
last_update2017-12-21 11:01:12
depth2
children0
net_rshares0
last_payout2017-12-28 11:01:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length10
author_reputation64,235,894,740,263
root_title"pinyin: an R package that converts Chinese characters into Latin letters - 将中文转成汉语拼音的 R 语言包"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@utopian-io ·
### Hey @dapeng I am @utopian-io. I have just upvoted you!
#### Achievements
- Seems like you contribute quite often. AMAZING!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x**
properties (22)
post_id21,428,608
authorutopian-io
permlinkre-dapeng-pinyin-an-r-package-that-converts-chineses-characters-into-latin-letters-r-20171222t034506406z
categoryutopian-io
json_metadata"{"app": "utopian/1.0.0", "community": "utopian", "tags": ["utopian-io"]}"
created2017-12-22 03:45:06
last_update2017-12-22 03:45:06
depth1
children0
net_rshares0
last_payout2017-12-29 03:45:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length1,004
author_reputation152,913,012,544,965
root_title"pinyin: an R package that converts Chinese characters into Latin letters - 将中文转成汉语拼音的 R 语言包"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000