├── .github └── workflows │ ├── documentation.yml │ ├── publish.yml │ └── test.yml ├── .gitignore ├── .vscode └── settings.json ├── LICENSE ├── MANIFEST.in ├── README.md ├── prepare.py ├── setup.cfg ├── setup.py ├── src └── Qieyun │ ├── __init__.py │ ├── _utils.py │ ├── _書影.py │ ├── _韻圖.py │ ├── 韻書.py │ └── 韻書和韻圖.py └── test ├── __init__.py └── main.py /.github/workflows/documentation.yml: -------------------------------------------------------------------------------- 1 | name: Documentation 2 | 3 | on: 4 | release: 5 | types: [created] 6 | workflow_dispatch: 7 | 8 | jobs: 9 | publish: 10 | runs-on: ubuntu-20.04 11 | steps: 12 | - uses: actions/checkout@v2 13 | - name: Setup Python 14 | uses: actions/setup-python@v2 15 | with: 16 | python-version: "3.9" 17 | - name: Upgrade pip 18 | run: python -m pip install --upgrade pip 19 | - name: Install packaging tools 20 | run: pip install setuptools wheel twine 21 | - name: Install pdoc 22 | run: pip install pdoc3 23 | - name: Prepare data 24 | run: python prepare.py 25 | - name: Install package 26 | run: pip install . 27 | - name: Build documentation 28 | run: pdoc --html -o docs -f -c sort_identifiers=False Qieyun 29 | - name: Publish 30 | run: | 31 | # Create a temporary directory 32 | export temp_dir=`mktemp -d -p ~` 33 | 34 | ( 35 | # Preserve .git 36 | mkdir $temp_dir/temp 37 | mv .git $temp_dir/temp 38 | cd $temp_dir/temp 39 | 40 | # Switch branch 41 | git fetch 42 | git checkout gh-pages 43 | git reset --hard gh-pages 44 | ) 45 | 46 | ( 47 | # Move .git 48 | mv $temp_dir/temp/.git $temp_dir 49 | rm -rf $temp_dir/temp 50 | mv docs/Qieyun/* $temp_dir 51 | mv LICENSE $temp_dir 52 | 53 | # Go to the temporary directory 54 | cd $temp_dir 55 | 56 | # Set commit identity 57 | git config user.email "41898282+github-actions[bot]@users.noreply.github.com" 58 | git config user.name "github-actions[bot]" 59 | 60 | # Publish 61 | git add . 62 | if [ -n "$(git status --porcelain)" ]; then 63 | git commit -m "Publish `TZ='Asia/Hong_Kong' date`" 64 | git push -f origin gh-pages 65 | fi 66 | ) 67 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Python package 2 | 3 | on: 4 | release: 5 | types: [created] 6 | 7 | jobs: 8 | deploy: 9 | runs-on: macos-latest 10 | steps: 11 | - uses: actions/checkout@v2 12 | - name: Set up Python 13 | uses: actions/setup-python@v2 14 | with: 15 | python-version: "3.9" 16 | - name: Upgrade pip 17 | run: python -m pip install --upgrade pip 18 | - name: Install packaging tools 19 | run: pip install setuptools wheel twine 20 | - name: Prepare data 21 | run: python prepare.py 22 | - name: Build 23 | run: python setup.py sdist bdist_wheel 24 | - name: Publish 25 | env: 26 | TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} 27 | TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} 28 | run: twine upload dist/* 29 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Python test 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - "*.md" 7 | pull_request: 8 | paths-ignore: 9 | - "*.md" 10 | 11 | jobs: 12 | build: 13 | runs-on: windows-latest 14 | strategy: 15 | matrix: 16 | python-version: [3.6, 3.7, 3.8, 3.9] 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Set up Python ${{ matrix.python-version }} 20 | uses: actions/setup-python@v2 21 | with: 22 | python-version: ${{ matrix.python-version }} 23 | - name: Upgrade pip 24 | run: python -m pip install --upgrade pip 25 | - name: Install packaging tools 26 | run: pip install setuptools wheel twine 27 | - name: Install PyTest 28 | run: pip install pytest 29 | - name: Prepare data 30 | run: python prepare.py 31 | - name: Install package 32 | run: pip install . 33 | - name: Test 34 | run: pytest test/main.py 35 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | *.egg-info 3 | .mypy_cache 4 | .pytest_cache 5 | __pycache__ 6 | /src/Qieyun/rhyme_book.csv 7 | /src/Qieyun/rhyme_table.csv 8 | /build 9 | /docs 10 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { "editor.formatOnSave": true } 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include LICENSE 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > [!WARNING] 2 | > This library (qieyun-python) has been suspended for maintenance, and the _Guangyun_ data contained in it is outdated. Please use [TshetUinh.js](https://github.com/nk2028/tshet-uinh-js) instead.\ 3 | > \ 4 | > 此函式庫(qieyun-python)已暫停維護,其中的《廣韻》資料已過時。請改用 [TshetUinh.js](https://github.com/nk2028/tshet-uinh-js)。 5 | 6 | # qieyun-python 7 | 8 | A Python library for the Qieyun phonological system 9 | 10 | ## Install 11 | 12 | ```sh 13 | $ pip install qieyun 14 | ``` 15 | 16 | ## Documentation 17 | 18 | See [documentation](https://nk2028.shn.hk/qieyun-python/). 19 | -------------------------------------------------------------------------------- /prepare.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | from urllib.request import urlretrieve 3 | 4 | here = path.abspath(path.dirname(__file__)) 5 | url = 'https://raw.githubusercontent.com/nk2028/qieyun-data/7d7586a/' 6 | 7 | 8 | def retrieve(filename): 9 | target = path.join(here, 'src/Qieyun', filename) 10 | if not path.exists(target): 11 | urlretrieve(url + filename, target) 12 | 13 | 14 | retrieve('rhyme_book.csv') 15 | retrieve('rhyme_table.csv') 16 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | license_files = LICENSE 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from os import path, system 4 | from setuptools import setup, find_packages 5 | 6 | here = path.abspath(path.dirname(__file__)) 7 | 8 | if not path.exists(path.join(here, 'src/Qieyun/rhyme_book.csv')) \ 9 | or not path.exists(path.join(here, 'src/Qieyun/rhyme_table.csv')): 10 | system('python3 prepare.py') 11 | 12 | with open(path.join(here, 'README.md'), encoding='utf-8') as f: 13 | long_description = f.read() 14 | 15 | setup( 16 | name='qieyun', 17 | version='0.13.3', 18 | description='A Python library for the Qieyun phonological system', 19 | long_description=long_description, 20 | long_description_content_type='text/markdown', 21 | url='https://github.com/nk2028/qieyun-python', 22 | author='The nk2028 Project', 23 | author_email='support@nk2028.shn.hk', 24 | classifiers=[ 25 | 'Development Status :: 4 - Beta', 26 | 'Intended Audience :: Developers', 27 | 'Topic :: Software Development :: Libraries :: Python Modules', 28 | 'Topic :: Text Processing :: Linguistic', 29 | 'Natural Language :: Chinese (Traditional)', 30 | 'License :: CC0 1.0 Universal (CC0 1.0) Public Domain Dedication', 31 | 'Programming Language :: Python :: 3', 32 | 'Programming Language :: Python :: 3.6', 33 | 'Programming Language :: Python :: 3.7', 34 | 'Programming Language :: Python :: 3.8', 35 | 'Programming Language :: Python :: 3.9', 36 | ], 37 | keywords='middle-chinese historical-linguistics qieyun', 38 | packages=find_packages('src'), 39 | package_dir={'': 'src'}, 40 | package_data={ 41 | 'Qieyun': ['rhyme_book.csv', 'rhyme_table.csv'], 42 | }, 43 | python_requires='>=3.6, <4', 44 | install_requires=['qieyun-encoder>=0.4.2,<0.5', 'networkx>=2.5,<2.6'], 45 | entry_points={}, 46 | project_urls={ 47 | 'Bug Reports': 'https://github.com/nk2028/qieyun-python/issues', 48 | 'Source': 'https://github.com/nk2028/qieyun-python', 49 | }, 50 | ) 51 | -------------------------------------------------------------------------------- /src/Qieyun/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | ''' 4 | 切韻音系 Python 函式庫。 5 | ''' 6 | 7 | from QieyunEncoder import 常量 8 | from QieyunEncoder import 音韻地位 9 | 10 | from .韻書 import 音韻地位2代表字, 音韻地位2字頭_韻書出處們, iter音韻地位 11 | from .韻書和韻圖 import 字頭2音韻地位_出處們 12 | 13 | __version__ = '0.13.3' 14 | -------------------------------------------------------------------------------- /src/Qieyun/_utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from QieyunEncoder import 常量, 音韻地位, 正則化韻 4 | 5 | ''' 6 | 注意!! 7 | 此檔案是半成品,僅供臨時使用。 8 | 在 `_韻圖.py` 使用了 `韻圖2切韻` 這一函式。 9 | 未來寫好後,此檔案將被移除。 10 | ''' 11 | 12 | l韻圖母號2韻圖母位置 = [ 13 | '舌齒音第二位', '舌齒音第一位', # 舌齒音 14 | '喉音第四位', '喉音第三位', '喉音第二位', '喉音第一位', # 喉音 15 | '齒音第五位', '齒音第四位', '齒音第三位', '齒音第二位', '齒音第一位', # 齒音 16 | '牙音第四位', '牙音第三位', '牙音第二位', '牙音第一位', # 牙音 17 | '舌音第四位', '舌音第三位', '舌音第二位', '舌音第一位', # 舌音 18 | '脣音第四位', '脣音第三位', '脣音第二位', '脣音第一位', # 脣音 19 | ] 20 | 21 | 22 | def 韻圖母號_韻圖等_切韻韻2韻圖母(韻圖母號, 韻圖等, 切韻韻): 23 | return [ 24 | # 舌齒音 25 | '日', 26 | '來', 27 | # 喉音 28 | '喻', 29 | '匣', 30 | '曉', 31 | '影', 32 | # 齒音 33 | '邪' if 韻圖等 in '一四' else '禪', 34 | '心' if 韻圖等 in '一四' else '審', 35 | '從' if 韻圖等 in '一四' else '牀', 36 | '清' if 韻圖等 in '一四' else '穿', 37 | '精' if 韻圖等 in '一四' else '照', 38 | # 牙音 39 | '疑', 40 | '羣', 41 | '溪', 42 | '見', 43 | # 舌音 44 | '泥' if 韻圖等 in '一四' else '孃', 45 | '定' if 韻圖等 in '一四' else '澄', 46 | '透' if 韻圖等 in '一四' else '徹', 47 | '端' if 韻圖等 in '一四' else '知', 48 | # 脣音 49 | '微' if 韻圖等 == '三' and 切韻韻 in 常量.輕脣韻 else '明', 50 | '奉' if 韻圖等 == '三' and 切韻韻 in 常量.輕脣韻 else '並', 51 | '敷' if 韻圖等 == '三' and 切韻韻 in 常量.輕脣韻 else '滂', 52 | '非' if 韻圖等 == '三' and 切韻韻 in 常量.輕脣韻 else '幫', 53 | ][韻圖母號] 54 | 55 | 56 | def 韻圖等_切韻韻2切韻等(韻圖等, 切韻韻): 57 | if 切韻韻 in 常量.一三等韻: 58 | return '一' if 韻圖等 == '一' else '三' 59 | if 切韻韻 in 常量.二三等韻: 60 | return '二' if 韻圖等 == '二' else '三' # TODO: 麻庚韻莊組 61 | return '' 62 | 63 | 64 | def 韻圖母_韻圖等2切韻母(韻圖母, 韻圖等): 65 | if 韻圖母 == '喻': 66 | return '云' if 韻圖等 == '三' else '以' 67 | 68 | if 韻圖母 == '照': 69 | return '章' if 韻圖等 == '三' else '莊' 70 | if 韻圖母 == '穿': 71 | return '昌' if 韻圖等 == '三' else '初' 72 | if 韻圖母 == '牀': 73 | return '常' if 韻圖等 == '三' else '從' 74 | if 韻圖母 == '審': 75 | return '書' if 韻圖等 == '三' else '生' 76 | if 韻圖母 == '禪': 77 | return '船' if 韻圖等 == '三' else '俟' 78 | 79 | if 韻圖母 == '非': 80 | return '幫' 81 | if 韻圖母 == '敷': 82 | return '滂' 83 | if 韻圖母 == '奉': 84 | return '並' 85 | if 韻圖母 == '微': 86 | return '明' 87 | 88 | return 韻圖母 89 | 90 | 91 | def 切韻母_切韻韻_韻圖等2重紐(切韻母, 切韻韻, 韻圖等): 92 | return '' if 切韻母 not in 常量.重紐母 or 切韻韻 not in 常量.重紐韻 else \ 93 | 'B' if 韻圖等 == '三' else \ 94 | 'A' 95 | 96 | 97 | def 韻圖2切韻(轉號, 韻圖開合修正後, 韻圖母位置, 韻圖聲, 韻圖韻, 韻圖等): 98 | 切韻韻 = 正則化韻(韻圖韻) 99 | 韻圖母號 = l韻圖母號2韻圖母位置.index(韻圖母位置) 100 | 韻圖母 = 韻圖母號_韻圖等_切韻韻2韻圖母(韻圖母號, 韻圖等, 切韻韻) 101 | 切韻等 = 韻圖等_切韻韻2切韻等(韻圖等, 切韻韻) 102 | 切韻母 = 韻圖母_韻圖等2切韻母(韻圖母, 韻圖等) 103 | if 切韻母 in '幫滂並明': 104 | 韻圖開合修正後 = '' 105 | 重紐 = 切韻母_切韻韻_韻圖等2重紐(切韻母, 切韻韻, 韻圖等) 106 | 音韻描述 = 切韻母 + 韻圖開合修正後 + 切韻等 + 重紐 + 切韻韻 + 韻圖聲 107 | try: 108 | 當前音韻地位 = 音韻地位.from描述(音韻描述) # will perform check 109 | except Exception: 110 | assert False, 音韻描述 111 | return 韻圖母, 當前音韻地位 112 | -------------------------------------------------------------------------------- /src/Qieyun/_書影.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import re 4 | 5 | 6 | def 生成書影(資料名稱, 圖片id, 縮略圖=False): 7 | ''' 8 | Test 9 | ''' 10 | 11 | 縮略圖 = 'thumb/' if 縮略圖 else '' 12 | 13 | if 資料名稱 == '王一': 14 | page = int(圖片id) 15 | return f'https://cethiunn-1305783649.cos.ap-guangzhou.myqcloud.com/王一/{縮略圖}{page:04}.jpg' 16 | 17 | if 資料名稱 == '廣韻': 18 | match = re.fullmatch(r'([^0-9]+)([0-9]+)([^0-9]+)', 圖片id) 19 | volume = ['上平', '下平', '上', '去', '入'].index(match[1]) + 1 20 | # TODO FIXME: 此段程式碼對左右理解有誤 21 | page = int(match[2]) + [8, 2, 2, 4, 3][volume - 1] 22 | position = match[3] # pylint: disable=unused-variable 23 | return f'https://kuankhiunn-1305783649.cos.ap-guangzhou.myqcloud.com/deakdzuondangpuoon/volume{volume}/{縮略圖}p{page:03}.jpg' 24 | 25 | if 資料名稱 == '指微韻鑑(嘉吉本)': 26 | page = int(圖片id) 27 | return f'https://hiunnkyanq-1305783649.cos.ap-guangzhou.myqcloud.com/keakitpuoon/{縮略圖}{page+14:02}.jpg' 28 | 29 | if 資料名稱 == '韻鏡(古逸叢書本)': 30 | page = int(圖片id) 31 | return f'https://hiunnkyanq-1305783649.cos.ap-guangzhou.myqcloud.com/kuujitdzungsjvpuoon/{縮略圖}{page+9:02}.png' 32 | 33 | raise NotImplementedError('未收錄指定書影') 34 | -------------------------------------------------------------------------------- /src/Qieyun/_韻圖.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | ''' 4 | 早期韻圖資料上的操作。 5 | ''' 6 | 7 | from collections import defaultdict 8 | import networkx as nx 9 | from os import path 10 | from QieyunEncoder import 音韻地位 11 | 12 | from ._書影 import 生成書影 13 | from .韻書 import d資料名稱_小韻號_編碼2字頭們 14 | 15 | from ._utils import 韻圖2切韻 16 | 17 | HERE = path.abspath(path.dirname(__file__)) 18 | 19 | d編碼2字頭圖 = defaultdict(nx.Graph) 20 | d編碼2韻圖出處們 = defaultdict(list) 21 | 22 | for (資料名稱, 小韻號, 編碼), 字頭們 in d資料名稱_小韻號_編碼2字頭們.items(): 23 | 字頭圖 = d編碼2字頭圖[編碼] 24 | if len(字頭們) == 1: 25 | 字頭圖.add_node(next(iter(字頭們))) 26 | else: 27 | xs = list(字頭們) 28 | 小韻首字 = xs[0] 29 | for 字頭 in xs[1:]: 30 | 字頭圖.add_edge(小韻首字, 字頭) 31 | 32 | 33 | def _字頭_音韻地位2韻圖出處們(字頭, 當前音韻地位): 34 | ''' 35 | 獲取字頭與音韻地位對應的所有韻圖出處。 36 | ''' 37 | 編碼 = 當前音韻地位.編碼 38 | 39 | def inner(): 40 | for 韻圖出處 in d編碼2韻圖出處們.get(編碼, []): 41 | 對應韻圖字頭 = 韻圖出處['對應韻圖字頭'] 42 | 字頭圖 = d編碼2字頭圖[編碼] 43 | if 對應韻圖字頭 == 字頭 or 對應韻圖字頭 in nx.algorithms.descendants(字頭圖, 字頭): 44 | yield 韻圖出處 45 | return list(inner()) 46 | 47 | 48 | def _讀取資料(): 49 | ''' 50 | 讀取韻書與韻圖資料,將韻書的小韻對應到韻圖等字頭。 51 | 此函式執行後,結果將存儲於 `d廣韻小韻號2韻圖出處` 中。 52 | ''' 53 | with open(path.join(HERE, 'rhyme_table.csv'), encoding='utf-8') as f: 54 | next(f) # skip header 55 | for line in f: 56 | 資料名稱, 字頭, 轉號, 韻圖開合修正後, 韻圖母位置, 韻圖聲, 韻圖韻, 韻圖等 = line.rstrip( 57 | '\n').split(',') 58 | 59 | 轉號 = int(轉號) 60 | 61 | 韻圖母, 當前音韻地位 = 韻圖2切韻(轉號, 韻圖開合修正後, 韻圖母位置, 韻圖聲, 韻圖韻, 韻圖等) 62 | 編碼 = 當前音韻地位.編碼 63 | 64 | 縮略圖 = 生成書影(資料名稱, 轉號, 縮略圖=True) 65 | 書影 = 生成書影(資料名稱, 轉號) 66 | 67 | d編碼2韻圖出處們[編碼].append({ 68 | '資料名稱': 資料名稱, 69 | '韻圖韻': 韻圖韻, 70 | '韻圖開合修正後': 韻圖開合修正後, 71 | '韻圖母位置': 韻圖母位置, 72 | '韻圖母': 韻圖母, 73 | '韻圖等': 韻圖等, 74 | '韻圖聲': 韻圖聲, 75 | '對應韻圖字頭': 字頭, 76 | '縮略圖': 縮略圖, 77 | '書影': 書影, 78 | }) 79 | 80 | 81 | _讀取資料() 82 | -------------------------------------------------------------------------------- /src/Qieyun/韻書.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | ''' 4 | 切韻系韻書資料上的操作。 5 | ''' 6 | 7 | from collections import defaultdict, namedtuple 8 | from os import path 9 | from QieyunEncoder import 音韻地位 10 | from typing import Optional 11 | 12 | from ._書影 import 生成書影 13 | 14 | HERE = path.abspath(path.dirname(__file__)) 15 | 16 | d字頭2編碼們 = defaultdict(dict) 17 | d編碼2字頭們 = defaultdict(dict) 18 | d編碼2廣韻字頭們 = defaultdict(dict) 19 | d字頭_編碼2韻書出處們 = defaultdict(list) 20 | d資料名稱_小韻號_編碼2字頭們 = defaultdict(dict) 21 | 22 | 音韻地位_韻書出處們 = namedtuple('條目', ['音韻地位', '韻書出處們']) 23 | 字頭_韻書出處們 = namedtuple('條目', ['字頭', '韻書出處們']) 24 | 25 | 26 | def iter音韻地位(): 27 | ''' 28 | 所有至少對應一個字頭的音韻地位。 29 | ''' 30 | for 編碼 in d編碼2字頭們: 31 | yield 音韻地位.from編碼(編碼) 32 | 33 | 34 | def _字頭2音韻地位_韻書出處們(字頭: str): 35 | ''' 36 | 由字頭查出相應的音韻地位和解釋。 37 | ''' 38 | return [ 39 | 音韻地位_韻書出處們( 40 | 音韻地位=音韻地位.from編碼(編碼), 41 | 韻書出處們=d字頭_編碼2韻書出處們.get((字頭, 編碼), []), 42 | ) for 編碼 in d字頭2編碼們.get(字頭, []) 43 | ] 44 | 45 | 46 | def 音韻地位2代表字(當前音韻地位) -> Optional[str]: 47 | ''' 48 | 獲取音韻地位對應的代表字。 49 | ''' 50 | 編碼 = 當前音韻地位.編碼 51 | 52 | # 優先選擇廣韻字頭 53 | 廣韻字頭們 = d編碼2廣韻字頭們.get(編碼) 54 | if 廣韻字頭們 is not None: 55 | return next(iter(廣韻字頭們)) 56 | 57 | 字頭們 = d編碼2字頭們.get(編碼) 58 | if 字頭們 is not None: 59 | return next(iter(字頭們)) 60 | 61 | return None 62 | 63 | 64 | def 音韻地位2字頭_韻書出處們(當前音韻地位): 65 | ''' 66 | 獲取音韻地位對應的所有字頭及其韻書出處。 67 | ''' 68 | 編碼 = 當前音韻地位.編碼 69 | return [ 70 | 字頭_韻書出處們( 71 | 字頭=字頭, 72 | 韻書出處們=d字頭_編碼2韻書出處們.get((字頭, 編碼), []), 73 | ) for 字頭 in d編碼2字頭們.get(編碼, []) 74 | ] 75 | 76 | 77 | def _讀取資料(): 78 | ''' 79 | TODO: documentation 80 | ''' 81 | with open(path.join(HERE, 'rhyme_book.csv'), encoding='utf-8') as f: 82 | next(f) # skip header 83 | for line in f: 84 | 資料名稱, 小韻號, 韻部原貌, 最簡描述, 反切覈校前, 反切, 字頭覈校前, 字頭, 釋義, 釋義補充, 圖片id = line.rstrip( 85 | '\n').split(',') # pylint: disable=unused-variable 86 | 87 | if 反切 == '': 88 | 反切 = 反切覈校前 89 | if 字頭 == '': 90 | 字頭 = 字頭覈校前 91 | 92 | 縮略圖 = 生成書影(資料名稱, 圖片id, 縮略圖=True) 93 | 書影 = 生成書影(資料名稱, 圖片id) 94 | 95 | 編碼 = 音韻地位.from描述(最簡描述).編碼 96 | 97 | d字頭2編碼們[字頭][編碼] = None 98 | d編碼2字頭們[編碼][字頭] = None 99 | d字頭_編碼2韻書出處們[字頭, 編碼].append({ 100 | '資料名稱': 資料名稱, 101 | '韻部原貌': 韻部原貌, 102 | '反切': 反切, 103 | '釋義': 釋義, 104 | '釋義補充': 釋義補充, 105 | '縮略圖': 縮略圖, 106 | '書影': 書影, 107 | }) 108 | d資料名稱_小韻號_編碼2字頭們[資料名稱, 小韻號, 編碼][字頭] = None 109 | 110 | if 資料名稱 == '廣韻': 111 | d編碼2廣韻字頭們[編碼][字頭] = None 112 | 113 | 114 | _讀取資料() 115 | -------------------------------------------------------------------------------- /src/Qieyun/韻書和韻圖.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | ''' 4 | 切韻系韻書及早期韻圖資料上的操作。 5 | ''' 6 | 7 | from collections import namedtuple 8 | 9 | from .韻書 import _字頭2音韻地位_韻書出處們 10 | from ._韻圖 import _字頭_音韻地位2韻圖出處們 11 | 12 | 音韻地位_出處們 = namedtuple('條目', ['音韻地位', '韻書出處們', '韻圖出處們']) 13 | 14 | 15 | def 字頭2音韻地位_出處們(字頭): 16 | ''' 17 | 獲取字頭對應的所有音韻地位及其韻書和韻圖出處。 18 | ''' 19 | return [ 20 | 音韻地位_出處們( 21 | 音韻地位=音韻地位, 22 | 韻書出處們=韻書出處們, 23 | 韻圖出處們=_字頭_音韻地位2韻圖出處們(字頭, 音韻地位), 24 | ) for 音韻地位, 韻書出處們 in _字頭2音韻地位_韻書出處們(字頭) 25 | ] 26 | -------------------------------------------------------------------------------- /test/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nk2028/qieyun-python/10194b80e4852102c1170886ff296486f6300d41/test/__init__.py -------------------------------------------------------------------------------- /test/main.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import Qieyun 4 | 5 | 6 | def test1(): 7 | ''' 8 | 測試 API 功能是否正常。 9 | ''' 10 | Qieyun.音韻地位2代表字(Qieyun.音韻地位.from描述('心一東入')) 11 | Qieyun.音韻地位2字頭_韻書出處們(Qieyun.音韻地位.from描述('見合三B支平')) 12 | Qieyun.字頭2音韻地位_出處們('速') 13 | --------------------------------------------------------------------------------