Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ayaka14732 committed Nov 14, 2020
0 parents commit 17d7ae3
Show file tree
Hide file tree
Showing 15 changed files with 5,492 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Package

on:
release:
types: [created]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

# Prepare Python environment
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
pip install networkx
- name: Build yitizi.json
run: python build/main.py

# Packaging for Python
- name: Build for Python
run: |
cp LICENSE README.md python
mkdir python/demo && cp demo/*.png python/demo
cp yitizi.json python/src/yitizi
- name: Publish for Python
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
cd python
python setup.py sdist bdist_wheel
twine upload dist/*
# Prepare Node.js environment
- name: Setup Node.js
uses: actions/setup-node@v1
with:
node-version: 14
registry-url: https://registry.npmjs.org/

# Packaging for Node.js
- name: Build for Node.js
run: |
cp LICENSE README.md javascript
mkdir javascript/demo && cp demo/*.png javascript/demo
build/javascript.sh
- name: Publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
cd javascript
npm publish
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/.vscode
/.mypy_cache

/*/**/LICENSE
/*/**/README.md

/python/build
/python/dist
/python/**/*.egg-info

/javascript/index.js

yitizi.json
121 changes: 121 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
Creative Commons Legal Code

CC0 1.0 Universal

CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
HEREUNDER.

Statement of Purpose

The laws of most jurisdictions throughout the world automatically confer
exclusive Copyright and Related Rights (defined below) upon the creator
and subsequent owner(s) (each and all, an "owner") of an original work of
authorship and/or a database (each, a "Work").

Certain owners wish to permanently relinquish those rights to a Work for
the purpose of contributing to a commons of creative, cultural and
scientific works ("Commons") that the public can reliably and without fear
of later claims of infringement build upon, modify, incorporate in other
works, reuse and redistribute as freely as possible in any form whatsoever
and for any purposes, including without limitation commercial purposes.
These owners may contribute to the Commons to promote the ideal of a free
culture and the further production of creative, cultural and scientific
works, or to gain reputation or greater distribution for their Work in
part through the use and efforts of others.

For these and/or other purposes and motivations, and without any
expectation of additional consideration or compensation, the person
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
is an owner of Copyright and Related Rights in the Work, voluntarily
elects to apply CC0 to the Work and publicly distribute the Work under its
terms, with knowledge of his or her Copyright and Related Rights in the
Work and the meaning and intended legal effect of CC0 on those rights.

1. Copyright and Related Rights. A Work made available under CC0 may be
protected by copyright and related or neighboring rights ("Copyright and
Related Rights"). Copyright and Related Rights include, but are not
limited to, the following:

i. the right to reproduce, adapt, distribute, perform, display,
communicate, and translate a Work;
ii. moral rights retained by the original author(s) and/or performer(s);
iii. publicity and privacy rights pertaining to a person's image or
likeness depicted in a Work;
iv. rights protecting against unfair competition in regards to a Work,
subject to the limitations in paragraph 4(a), below;
v. rights protecting the extraction, dissemination, use and reuse of data
in a Work;
vi. database rights (such as those arising under Directive 96/9/EC of the
European Parliament and of the Council of 11 March 1996 on the legal
protection of databases, and under any national implementation
thereof, including any amended or successor version of such
directive); and
vii. other similar, equivalent or corresponding rights throughout the
world based on applicable law or treaty, and any national
implementations thereof.

2. Waiver. To the greatest extent permitted by, but not in contravention
of, applicable law, Affirmer hereby overtly, fully, permanently,
irrevocably and unconditionally waives, abandons, and surrenders all of
Affirmer's Copyright and Related Rights and associated claims and causes
of action, whether now known or unknown (including existing as well as
future claims and causes of action), in the Work (i) in all territories
worldwide, (ii) for the maximum duration provided by applicable law or
treaty (including future time extensions), (iii) in any current or future
medium and for any number of copies, and (iv) for any purpose whatsoever,
including without limitation commercial, advertising or promotional
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
member of the public at large and to the detriment of Affirmer's heirs and
successors, fully intending that such Waiver shall not be subject to
revocation, rescission, cancellation, termination, or any other legal or
equitable action to disrupt the quiet enjoyment of the Work by the public
as contemplated by Affirmer's express Statement of Purpose.

3. Public License Fallback. Should any part of the Waiver for any reason
be judged legally invalid or ineffective under applicable law, then the
Waiver shall be preserved to the maximum extent permitted taking into
account Affirmer's express Statement of Purpose. In addition, to the
extent the Waiver is so judged Affirmer hereby grants to each affected
person a royalty-free, non transferable, non sublicensable, non exclusive,
irrevocable and unconditional license to exercise Affirmer's Copyright and
Related Rights in the Work (i) in all territories worldwide, (ii) for the
maximum duration provided by applicable law or treaty (including future
time extensions), (iii) in any current or future medium and for any number
of copies, and (iv) for any purpose whatsoever, including without
limitation commercial, advertising or promotional purposes (the
"License"). The License shall be deemed effective as of the date CC0 was
applied by Affirmer to the Work. Should any part of the License for any
reason be judged legally invalid or ineffective under applicable law, such
partial invalidity or ineffectiveness shall not invalidate the remainder
of the License, and in such case Affirmer hereby affirms that he or she
will not (i) exercise any of his or her remaining Copyright and Related
Rights in the Work or (ii) assert any associated claims and causes of
action with respect to the Work, in either case contrary to Affirmer's
express Statement of Purpose.

4. Limitations and Disclaimers.

a. No trademark or patent rights held by Affirmer are waived, abandoned,
surrendered, licensed or otherwise affected by this document.
b. Affirmer offers the Work as-is and makes no representations or
warranties of any kind concerning the Work, express, implied,
statutory or otherwise, including without limitation warranties of
title, merchantability, fitness for a particular purpose, non
infringement, or the absence of latent or other defects, accuracy, or
the present or absence of errors, whether or not discoverable, all to
the greatest extent permissible under applicable law.
c. Affirmer disclaims responsibility for clearing rights of other persons
that may apply to the Work or any use thereof, including without
limitation any person's Copyright and Related Rights in the Work.
Further, Affirmer disclaims responsibility for obtaining any necessary
consents, permissions or other rights required for any use of the
Work.
d. Affirmer understands and acknowledges that Creative Commons is not a
party to this document and has no duty or obligation with respect to
this CC0 or use of the Work.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Yitizi

Input a Chinese character. Output all the variant characters of it.<br>
輸入一個漢字,輸出它的全部異體字。<br>
输入一个汉字,输出它的全部异体字。

## Usage

### Python

Installation:

```sh
pip install yitizi
```

Usage:

```python
>>> import yitizi
>>> yitizi.get('')
['', '']
```

### JavaScript

Installation:

Usage:

```javascript
> const Yitizi = require('./index.js')
> Yitizi.get('')
[ '', '' ]
```

## Design

As shown in the figure, the following information is stored in `yitizi.csv`:

![](demo/char.png)

This file is processed by `build/main.py`. The data in the file is converted into a graph.

Then, for each node in the graph, output the node and all its neighbor nodes. This becomes the final dictionary of the variant characters.

```
{ 正字: 異體字1, 異體字2,
異體字1: 正字,
異體字2: 正字
}
```

When using the library, the above dictionary is used to determine the variant characters of a given character.
11 changes: 11 additions & 0 deletions build/javascript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
echo "
const Yitizi = {
yitiziData: `cat yitizi.json`,
get: function get(c) {
const res = this.yitiziData[c];
return res == null ? [] : [...res];
}
};
try { module.exports = exports = Yitizi; } catch (e) {}
" > javascript/index.js
16 changes: 16 additions & 0 deletions build/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import json
import networkx as nx

G = nx.Graph()

with open('yitizi.csv') as f:
next(f) # skip header
for line in f:
k, vs = line.rstrip().split(',')
for v in vs:
G.add_edge(k, v)

d = {n: ''.join(G.neighbors(n)) for n in G.nodes()}

with open('yitizi.json', 'w') as f:
json.dump(d, f, ensure_ascii=False, separators=(',\n', ':'))
5 changes: 5 additions & 0 deletions demo/char.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
digraph G {
正字 [fillcolor=grey, style=filled]
正字 -> 異體字1
正字 -> 異體字2
}
Binary file added demo/char.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions javascript/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "yitizi",
"version": "0.0.1",
"description": "Input a Chinese character. Output all the variant characters of it.",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nk2028/yitizi.git"
},
"keywords": [
"nlp",
"chinese"
],
"author": "Ngiox Khyen 2028 Project",
"license": "CC0-1.0",
"bugs": {
"url": "https://github.com/nk2028/yitizi/issues"
},
"homepage": "https://github.com/nk2028/yitizi#readme"
}
1 change: 1 addition & 0 deletions python/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include LICENSE
2 changes: 2 additions & 0 deletions python/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
license_files = LICENSE
53 changes: 53 additions & 0 deletions python/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-

from setuptools import setup, find_packages
from os import path

here = path.abspath(path.dirname(__file__))

assert path.exists(path.join(here, 'src/yitizi/yitizi.json')), 'You must run the build script first'

with open(path.join(here, 'README.md'), encoding='utf8') as f:
long_description = f.read()

setup(
name='yitizi',
version='0.0.1',
description='Input a Chinese character. Output all the variant characters of it.',
long_description=long_description,
long_description_content_type='text/markdown',
url='https://github.com/nk2028/yitizi',
author='Ngiox Khyen 2028 Project',
author_email='[email protected]',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Text Processing :: Linguistic',
'Natural Language :: Chinese (Traditional)',
'Natural Language :: Chinese (Simplified)',
'License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
],
keywords='chinese natural-language-processing',
packages=find_packages('src'),
package_dir={'': 'src'},
package_data={
'yitizi': ['yitizi.json'],
},
include_package_data=True,
python_requires='>=3.5, <4',
install_requires=[],
entry_points={},
project_urls={
'Bug Reports': 'https://github.com/nk2028/yitizi/issues',
'Source': 'https://github.com/nk2028/yitizi',
},
zip_safe=False
)
1 change: 1 addition & 0 deletions python/src/yitizi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .yitizi import *
28 changes: 28 additions & 0 deletions python/src/yitizi/yitizi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-

import json
from os import path

HERE = path.abspath(path.dirname(__file__))

with open(path.join(HERE, 'yitizi.json'), encoding='utf8') as f:
yitizi_data = json.load(f)

def get(c):
'''
Input a Chinese character. Output all the variant characters of it.
```
>>> import yitizi
>>> yitizi.get('和')
['咊', '龢']
```
'''

assert len(c) == 1, 'Expected a single character'

res = yitizi_data.get(c)
if res:
return list(res)
else:
return [] # return empty list if the given character has no variant characters
Loading

0 comments on commit 17d7ae3

Please sign in to comment.