根据中文姓名猜测其性别
- 不到20行纯Python代码(核心部分)
- 无任何依赖库
- 兼容python3, python2, pypy
- 82%的准确率
- 可用于猜测性别
- 也可用于判断名字的男性化/女性化程度
## 使用
> pip install ngender
或者(OSX)
> brew install https://raw.githubusercontent.com/observerss/homebrew/61b3623967dc9507958dfb517e7f746baa96dcf1/Library/Formula/ngender.rb
然后在命令行中
```bash
$ ng 赵本山 宋丹丹
name: 赵本山 => gender: male, probability: 0.9836229687547046
name: 宋丹丹 => gender: female, probability: 0.9759486128949907
```
当然也可以在Python程序中用
```py
>>> import ngender
>>> ngender.guess('赵本山')
('male', 0.9836229687547046)
>>> ngender.guess('宋丹丹')
('female', 0.9759486128949907)
>>> %timeit guess('宋丹丹')
100000 loops, best of 3: 4.01 µs per loop
```
## 原理
### 数学
贝叶斯公式: ```P(Y|X) = P(X|Y) * P(Y) / P(X)```
当X条件独立时, ```P(X|Y) = P(X1|Y) * P(X2|Y) * ...```
应用到猜名字上
```
P(gender=男|name=本山)
= P(name=本山|gender=男) * P(gender=男) / P(name=本山)
= P(name has 本|gender=男) * P(name has 山|gender=男) * P(gender=男) / P(name=本山)
```
### 计算
0. 文件`charfreq.csv`是怎么来的?
曾经有个东西叫开房记录.avi(雾),里面有名字和性别, 2000w条, 统计一下得出
0. 怎么算 `P(name has 本|gender=男)`?
“本”在男性名字中出现的次数 / 男性字出现的总次数
0. 怎么算 `P(gender=男)`?
男性名出现的次数 / 总次数
0. 怎么算 `P(name=本山)`?
不用算, 在算概率的时候会互相约去
## 坑
```py
>>> ngender.guess('李胜男')
('male', 0.851334658742)
```
虽然两个字都很偏男性,但是结合起来就是女性名
Metadata
Release files for ngender 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| ngender-0.1.1.tar.gz | 48.7 kB | Without provenance | Details |
Release files / ngender-0.1.1.tar.gz
| Download URL | ngender-0.1.1.tar.gz |
|---|---|
| Size | 48.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e62b1a0e431f090aba9f8fbbf43c1f1da7ab52d9ef7b3d2e1eb22b0e1059b643
|
|
BLAKE2b-256 checksum How to use checksums |
39782b690c3773a936fe87559bbecbd4290b37a956f77b8a3dfe29c14faed79f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for ngender 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.