hangulpy 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hangulpy-1.0.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 gaon12
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,139 @@
1
+ Metadata-Version: 2.1
2
+ Name: hangulpy
3
+ Version: 1.0.0
4
+ Summary: A Python library for processing Hangul, inspired by es-hangul.
5
+ Home-page: https://github.com/gaon12/hangulpy
6
+ Author: Jeong Gaon
7
+ Author-email: gokirito12@gmail.com
8
+ License: MIT
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+
16
+ # hangulpy
17
+
18
+ hangulpy는 한글 처리를 위한 파이썬 라이브러리입니다. es-hangul의 파이썬 버전으로, 초성 검색과 조사 붙이기 등의 기능을 제공합니다.
19
+
20
+ ## 설치
21
+
22
+ ```bash
23
+ pip install hangulpy
24
+ ```
25
+
26
+ ## 사용법
27
+
28
+ ### 초성 검색
29
+
30
+ ```python
31
+ from hangulpy import chosungIncludes
32
+
33
+ searchWord = '라면'
34
+ userInput = 'ㄹㅁ'
35
+
36
+ result = chosungIncludes(searchWord, userInput)
37
+ print(result) # True
38
+ ```
39
+
40
+ ### 조사 붙이기
41
+
42
+ ```python
43
+ from hangulpy import josa
44
+
45
+ word1 = '사과'
46
+ sentence1 = josa(word1, '을/를') + ' 먹었습니다.'
47
+ print(sentence1) # '사과를 먹었습니다.'
48
+
49
+ word2 = '바나나'
50
+ sentence2 = josa(word2, '이/가') + ' 맛있습니다.'
51
+ print(sentence2) # '바나나가 맛있습니다.'
52
+ ```
53
+
54
+ ### 자음 또는 모음 여부
55
+
56
+ ```python
57
+ from hangulpy import is_hangul_consonant, is_hangul_vowel
58
+
59
+ char1 = 'ㄱ'
60
+ char2 = 'ㅏ'
61
+
62
+ print(is_hangul_consonant('ㄱ')) # True
63
+ print(is_hangul_consonant('ㅏ')) # False
64
+ print(is_hangul_vowel('ㅏ')) # True
65
+ print(is_hangul_vowel('ㄱ')) # False
66
+ ```
67
+
68
+ ### 문자열 포함 여부 확인
69
+
70
+ ```python
71
+ from hangulpy import hangul_contains
72
+
73
+ word = '사과'
74
+ print(hangul_contains(word, '')) # True
75
+ print(hangul_contains(word, '', notallowempty=True)) # False
76
+ print(hangul_contains(word, 'ㅅ')) # True
77
+ print(hangul_contains(word, '삭')) # True
78
+ print(hangul_contains(word, '삽')) # False
79
+ print(hangul_contains(word, '사과')) # True
80
+
81
+ # 문장처럼 입력 값 사이에 공백이 포함된 경우
82
+ print(hangul_contains('사과는 맛있다', '사과는 ㅁ')) # True
83
+ print(hangul_contains('사과는 맛있다', '사과는 ')) # True
84
+ ```
85
+
86
+ ### 초/중/종성 분해(문자열 변환)
87
+ ```python
88
+ from hangulpy import decompose_hangul_char
89
+
90
+ char = '괜'
91
+ print(decompose_hangul_char(char)) # ('ㄱ', ('ㅗ', 'ㅐ'), ('ㄴ', 'ㅈ'))
92
+ ```
93
+
94
+ #### 분해 시 배열로 반환
95
+ ```python
96
+ char1 = '값'
97
+ print(split_hangul_char(char1)) # ['ㄱ', 'ㅏ', 'ㅂ', 'ㅅ']
98
+
99
+ char2 = 'ㅘ'
100
+ print(split_hangul_char(char2)) # ['ㅗ', 'ㅏ']
101
+
102
+ char3 = 'ㄵ'
103
+ print(split_hangul_char(char3)) # ['ㄴ', 'ㅈ']
104
+ ```
105
+
106
+ ### 자음으로 끝나는지 확인
107
+ ```python
108
+ from hangulpy import ends_with_consonant
109
+
110
+ print(ends_with_consonant('강')) # False
111
+ print(ends_with_consonant('각')) # True
112
+ print(ends_with_consonant('ㄱ')) # True
113
+ print(ends_with_consonant('ㅏ')) # False
114
+ print(ends_with_consonant('a')) # False
115
+ print(ends_with_consonant('한')) # True
116
+ print(ends_with_consonant('하')) # False
117
+ ```
118
+
119
+ ### 초성 또는 종성으로 쓰일 수 있는지 확인
120
+ ```python
121
+ from hangulpy import can_be_chosung, can_be_jongsung
122
+
123
+ print(can_be_chosung('ㄱ')) # True
124
+ print(can_be_chosung('ㄳ')) # False
125
+ print(can_be_chosung('ㄸ')) # True
126
+ print(can_be_jongsung('ㄲ')) # True
127
+ print(can_be_jongsung('ㄸ')) # False
128
+ print(can_be_jongsung('ㄳ')) # True
129
+ ```
130
+
131
+ ### 숫자 읽기
132
+ ```python
133
+ from hangulpy import number_to_hangul, hangul_to_number
134
+
135
+ print(number_to_hangul(1234)) # 천이백삼십사
136
+ print(number_to_hangul(3.1415926)) # 삼 점 일사일오구이육
137
+ print(hangul_to_number("천이백삼십사")) # 1234
138
+ print(hangul_to_number("삼점일사일오구이육")) # 3.1415926
139
+ ```
@@ -0,0 +1,124 @@
1
+ # hangulpy
2
+
3
+ hangulpy는 한글 처리를 위한 파이썬 라이브러리입니다. es-hangul의 파이썬 버전으로, 초성 검색과 조사 붙이기 등의 기능을 제공합니다.
4
+
5
+ ## 설치
6
+
7
+ ```bash
8
+ pip install hangulpy
9
+ ```
10
+
11
+ ## 사용법
12
+
13
+ ### 초성 검색
14
+
15
+ ```python
16
+ from hangulpy import chosungIncludes
17
+
18
+ searchWord = '라면'
19
+ userInput = 'ㄹㅁ'
20
+
21
+ result = chosungIncludes(searchWord, userInput)
22
+ print(result) # True
23
+ ```
24
+
25
+ ### 조사 붙이기
26
+
27
+ ```python
28
+ from hangulpy import josa
29
+
30
+ word1 = '사과'
31
+ sentence1 = josa(word1, '을/를') + ' 먹었습니다.'
32
+ print(sentence1) # '사과를 먹었습니다.'
33
+
34
+ word2 = '바나나'
35
+ sentence2 = josa(word2, '이/가') + ' 맛있습니다.'
36
+ print(sentence2) # '바나나가 맛있습니다.'
37
+ ```
38
+
39
+ ### 자음 또는 모음 여부
40
+
41
+ ```python
42
+ from hangulpy import is_hangul_consonant, is_hangul_vowel
43
+
44
+ char1 = 'ㄱ'
45
+ char2 = 'ㅏ'
46
+
47
+ print(is_hangul_consonant('ㄱ')) # True
48
+ print(is_hangul_consonant('ㅏ')) # False
49
+ print(is_hangul_vowel('ㅏ')) # True
50
+ print(is_hangul_vowel('ㄱ')) # False
51
+ ```
52
+
53
+ ### 문자열 포함 여부 확인
54
+
55
+ ```python
56
+ from hangulpy import hangul_contains
57
+
58
+ word = '사과'
59
+ print(hangul_contains(word, '')) # True
60
+ print(hangul_contains(word, '', notallowempty=True)) # False
61
+ print(hangul_contains(word, 'ㅅ')) # True
62
+ print(hangul_contains(word, '삭')) # True
63
+ print(hangul_contains(word, '삽')) # False
64
+ print(hangul_contains(word, '사과')) # True
65
+
66
+ # 문장처럼 입력 값 사이에 공백이 포함된 경우
67
+ print(hangul_contains('사과는 맛있다', '사과는 ㅁ')) # True
68
+ print(hangul_contains('사과는 맛있다', '사과는 ')) # True
69
+ ```
70
+
71
+ ### 초/중/종성 분해(문자열 변환)
72
+ ```python
73
+ from hangulpy import decompose_hangul_char
74
+
75
+ char = '괜'
76
+ print(decompose_hangul_char(char)) # ('ㄱ', ('ㅗ', 'ㅐ'), ('ㄴ', 'ㅈ'))
77
+ ```
78
+
79
+ #### 분해 시 배열로 반환
80
+ ```python
81
+ char1 = '값'
82
+ print(split_hangul_char(char1)) # ['ㄱ', 'ㅏ', 'ㅂ', 'ㅅ']
83
+
84
+ char2 = 'ㅘ'
85
+ print(split_hangul_char(char2)) # ['ㅗ', 'ㅏ']
86
+
87
+ char3 = 'ㄵ'
88
+ print(split_hangul_char(char3)) # ['ㄴ', 'ㅈ']
89
+ ```
90
+
91
+ ### 자음으로 끝나는지 확인
92
+ ```python
93
+ from hangulpy import ends_with_consonant
94
+
95
+ print(ends_with_consonant('강')) # False
96
+ print(ends_with_consonant('각')) # True
97
+ print(ends_with_consonant('ㄱ')) # True
98
+ print(ends_with_consonant('ㅏ')) # False
99
+ print(ends_with_consonant('a')) # False
100
+ print(ends_with_consonant('한')) # True
101
+ print(ends_with_consonant('하')) # False
102
+ ```
103
+
104
+ ### 초성 또는 종성으로 쓰일 수 있는지 확인
105
+ ```python
106
+ from hangulpy import can_be_chosung, can_be_jongsung
107
+
108
+ print(can_be_chosung('ㄱ')) # True
109
+ print(can_be_chosung('ㄳ')) # False
110
+ print(can_be_chosung('ㄸ')) # True
111
+ print(can_be_jongsung('ㄲ')) # True
112
+ print(can_be_jongsung('ㄸ')) # False
113
+ print(can_be_jongsung('ㄳ')) # True
114
+ ```
115
+
116
+ ### 숫자 읽기
117
+ ```python
118
+ from hangulpy import number_to_hangul, hangul_to_number
119
+
120
+ print(number_to_hangul(1234)) # 천이백삼십사
121
+ print(number_to_hangul(3.1415926)) # 삼 점 일사일오구이육
122
+ print(hangul_to_number("천이백삼십사")) # 1234
123
+ print(hangul_to_number("삼점일사일오구이육")) # 3.1415926
124
+ ```
@@ -0,0 +1,11 @@
1
+ # hangulpy/__init__.py
2
+
3
+ from .chosung import chosungIncludes, get_chosung
4
+ from .hangul_check import is_hangul_consonant, is_hangul_vowel
5
+ from .hangul_contains import hangul_contains
6
+ from .hangul_decompose import decompose_hangul_char
7
+ from .hangul_ends_with_consonant import ends_with_consonant
8
+ from .hangul_number import float_to_hangul, hangul_to_number, number_to_hangul
9
+ from .hangul_role import can_be_chosung, can_be_jongsung
10
+ from .hangul_split import split_hangul_char
11
+ from .josa import has_jongsung, josa
@@ -0,0 +1,29 @@
1
+ # chosung.py
2
+
3
+ from hangulpy.utils import CHOSUNG_LIST, CHOSUNG_BASE, is_hangul_char, HANGUL_BEGIN_UNICODE
4
+
5
+ def get_chosung(c):
6
+ """
7
+ 주어진 한글 음절의 초성을 반환합니다.
8
+
9
+ :param c: 한글 음절 문자
10
+ :return: 초성 문자 (한글이 아니면 그대로 반환)
11
+ """
12
+ if is_hangul_char(c):
13
+ char_index = ord(c) - HANGUL_BEGIN_UNICODE
14
+ chosung_index = char_index // CHOSUNG_BASE
15
+ return CHOSUNG_LIST[chosung_index]
16
+ return c
17
+
18
+ def chosungIncludes(word, pattern):
19
+ """
20
+ 주어진 단어에 패턴의 초성이 포함되어 있는지 확인합니다.
21
+
22
+ :param word: 검색할 단어
23
+ :param pattern: 검색할 초성 패턴
24
+ :return: 패턴이 단어의 초성에 포함되어 있으면 True, 아니면 False
25
+ """
26
+ # 단어의 각 문자를 초성으로 변환하여 문자열을 생성합니다.
27
+ word_chosung = ''.join(get_chosung(c) for c in word)
28
+ # 패턴이 초성 문자열에 포함되어 있는지 확인합니다.
29
+ return pattern in word_chosung
@@ -0,0 +1,21 @@
1
+ # hangul_check.py
2
+
3
+ from hangulpy.utils import CHOSUNG_LIST, JUNGSUNG_LIST
4
+
5
+ def is_hangul_consonant(char):
6
+ """
7
+ 주어진 문자가 한글 자음인지 확인합니다.
8
+
9
+ :param char: 확인할 문자
10
+ :return: 한글 자음이면 True, 아니면 False
11
+ """
12
+ return char in CHOSUNG_LIST
13
+
14
+ def is_hangul_vowel(char):
15
+ """
16
+ 주어진 문자가 한글 모음인지 확인합니다.
17
+
18
+ :param char: 확인할 문자
19
+ :return: 한글 모음이면 True, 아니면 False
20
+ """
21
+ return char in JUNGSUNG_LIST
@@ -0,0 +1,22 @@
1
+ # hangul_contains.py
2
+
3
+ from hangulpy.utils import is_hangul_char
4
+ from hangulpy.hangul_split import split_hangul_char
5
+
6
+ def hangul_contains(word, pattern, notallowempty=False):
7
+ """
8
+ 주어진 한글 문자열이 다른 한글 문자열을 포함하는지 검사합니다.
9
+
10
+ :param word: 검사할 한글 문자열
11
+ :param pattern: 포함 여부를 검사할 한글 문자열 패턴
12
+ :param notallowempty: 패턴이 빈 문자열일 때 false를 반환하는 옵션
13
+ :return: 포함되면 True, 아니면 False
14
+ """
15
+ if not pattern:
16
+ return not notallowempty
17
+
18
+ # 문자열을 분해하여 리스트로 변환
19
+ word_split = ''.join(''.join(split_hangul_char(char)) for char in word)
20
+ pattern_split = ''.join(''.join(split_hangul_char(char)) for char in pattern)
21
+
22
+ return pattern_split in word_split
@@ -0,0 +1,28 @@
1
+ # hangul_decompose.py
2
+
3
+ from hangulpy.utils import CHOSUNG_LIST, CHOSUNG_BASE, is_hangul_char, HANGUL_BEGIN_UNICODE, JUNGSUNG_LIST, JUNGSUNG_DECOMPOSE, JONGSUNG_LIST, JONGSUNG_DECOMPOSE, JUNGSUNG_BASE
4
+
5
+ def decompose_hangul_char(c):
6
+ """
7
+ 주어진 한글 음절을 초성, 중성, 종성으로 분해합니다.
8
+
9
+ :param c: 한글 음절 문자
10
+ :return: (초성, 중성, 종성) 튜플
11
+ """
12
+ if is_hangul_char(c):
13
+ # 한글 음절의 유니코드 값을 기준으로 각 성분의 인덱스를 계산합니다.
14
+ char_index = ord(c) - HANGUL_BEGIN_UNICODE
15
+ chosung_index = char_index // CHOSUNG_BASE
16
+ jungsung_index = (char_index % CHOSUNG_BASE) // JUNGSUNG_BASE
17
+ jongsung_index = char_index % JUNGSUNG_BASE
18
+
19
+ # 중성 및 종성 분해
20
+ jungsung = JUNGSUNG_LIST[jungsung_index]
21
+ jongsung = JONGSUNG_LIST[jongsung_index]
22
+
23
+ jungsung_decomposed = JUNGSUNG_DECOMPOSE.get(jungsung, (jungsung,))
24
+ jongsung_decomposed = JONGSUNG_DECOMPOSE.get(jongsung, (jongsung,))
25
+
26
+ return (CHOSUNG_LIST[chosung_index], jungsung_decomposed, jongsung_decomposed)
27
+
28
+ return (c, '', '') # 한글이 아니면 그대로 반환
@@ -0,0 +1,19 @@
1
+ # hangul_ends_with_consonant.py
2
+
3
+ from hangulpy.utils import is_hangul_char, HANGUL_BEGIN_UNICODE, CHOSUNG_LIST, JONGSUNG_LIST
4
+
5
+ def ends_with_consonant(char):
6
+ """
7
+ 주어진 문자가 자음으로 끝나는지 확인합니다.
8
+
9
+ :param char: 확인할 문자
10
+ :return: 자음으로 끝나면 True, 아니면 False
11
+ """
12
+ if not is_hangul_char(char):
13
+ return char in CHOSUNG_LIST
14
+
15
+ # 한글 음절의 유니코드 값을 기준으로 종성 인덱스를 확인합니다.
16
+ char_index = ord(char) - HANGUL_BEGIN_UNICODE
17
+ jongsung_index = char_index % 28 # 28개의 종성
18
+
19
+ return JONGSUNG_LIST[jongsung_index] != ''
@@ -0,0 +1,98 @@
1
+ # hangul_number.py
2
+
3
+ from hangulpy.utils import UNITS, NUMBERS
4
+
5
+ def number_to_hangul(num):
6
+ """
7
+ 주어진 숫자를 한글로 변환합니다.
8
+
9
+ :param num: 변환할 숫자
10
+ :return: 한글 숫자 문자열
11
+ """
12
+ if num == 0:
13
+ return "영"
14
+
15
+ if isinstance(num, float):
16
+ return float_to_hangul(num)
17
+
18
+ result = ""
19
+ str_num = str(num)[::-1]
20
+
21
+ for i, digit in enumerate(str_num):
22
+ digit = int(digit)
23
+ if digit != 0:
24
+ if i == 4:
25
+ result = "만" + result
26
+ elif i > 4 and i % 4 == 0:
27
+ result = UNITS[4 * (i // 4)] + result
28
+ part = NUMBERS[digit] + UNITS[i % 4]
29
+ if i % 4 == 0 and digit == 1 and i != 0:
30
+ part = UNITS[i % 4] # 1은 생략
31
+ result = part + result
32
+
33
+ if result.startswith('일'):
34
+ result = result[1:]
35
+
36
+ return result
37
+
38
+ def float_to_hangul(num):
39
+ """
40
+ 주어진 소수를 한글로 변환합니다.
41
+
42
+ :param num: 변환할 소수
43
+ :return: 한글 숫자 문자열
44
+ """
45
+ int_part, frac_part = str(num).split('.')
46
+ return f"{number_to_hangul(int(int_part))} 점 {''.join(NUMBERS[int(d)] for d in frac_part)}"
47
+
48
+ def hangul_to_number(hangul):
49
+ """
50
+ 주어진 한글 숫자 문자열을 숫자로 변환합니다.
51
+
52
+ :param hangul: 변환할 한글 숫자 문자열
53
+ :return: 숫자
54
+ """
55
+ num_map = {name: value for value, name in enumerate(NUMBERS) if name}
56
+ unit_map = {name: 10**idx for idx, name in enumerate(UNITS) if name}
57
+ num = 0
58
+ tmp = 0
59
+ stack = []
60
+ is_float = False
61
+ float_part = 0
62
+ float_div = 1
63
+
64
+ # 소수점 분리
65
+ if '점' in hangul:
66
+ integer_part, fractional_part = hangul.split('점')
67
+ else:
68
+ integer_part, fractional_part = hangul, ''
69
+
70
+ # 정수 부분 처리
71
+ for char in integer_part:
72
+ if char in num_map:
73
+ stack.append(num_map[char])
74
+ elif char in unit_map:
75
+ if not stack:
76
+ stack.append(1)
77
+ tmp += sum(stack) * unit_map[char]
78
+ stack = []
79
+ else:
80
+ if stack:
81
+ tmp += sum(stack)
82
+ stack = []
83
+ num += tmp
84
+ tmp = 0
85
+
86
+ if stack:
87
+ tmp += sum(stack)
88
+ num += tmp
89
+
90
+ # 소수 부분 처리
91
+ if fractional_part:
92
+ for char in fractional_part:
93
+ if char in num_map:
94
+ float_part = float_part * 10 + num_map[char]
95
+ float_div *= 10
96
+ return num + float_part / float_div
97
+
98
+ return num
@@ -0,0 +1,21 @@
1
+ # hangul_role.py
2
+
3
+ from hangulpy.utils import CHOSUNG_LIST, JONGSUNG_LIST
4
+
5
+ def can_be_chosung(char):
6
+ """
7
+ 주어진 문자가 한글 초성으로 쓰일 수 있는지 확인합니다.
8
+
9
+ :param char: 확인할 문자
10
+ :return: 초성으로 쓰일 수 있으면 True, 아니면 False
11
+ """
12
+ return char in CHOSUNG_LIST
13
+
14
+ def can_be_jongsung(char):
15
+ """
16
+ 주어진 문자가 한글 종성으로 쓰일 수 있는지 확인합니다.
17
+
18
+ :param char: 확인할 문자
19
+ :return: 종성으로 쓰일 수 있으면 True, 아니면 False
20
+ """
21
+ return char in JONGSUNG_LIST
@@ -0,0 +1,28 @@
1
+ # hangul_split.py
2
+
3
+ from hangulpy.utils import CHOSUNG_LIST, CHOSUNG_BASE, is_hangul_char, HANGUL_BEGIN_UNICODE, JUNGSUNG_LIST, JUNGSUNG_DECOMPOSE, JONGSUNG_LIST, JONGSUNG_DECOMPOSE, JUNGSUNG_BASE
4
+
5
+ def split_hangul_char(c):
6
+ """
7
+ 주어진 한글 음절을 초성, 중성, 종성으로 분해하여 배열 형태로 반환합니다.
8
+
9
+ :param c: 한글 음절 문자
10
+ :return: 초성, 중성, 종성을 배열 형태로 반환
11
+ """
12
+ if is_hangul_char(c):
13
+ # 한글 음절의 유니코드 값을 기준으로 각 성분의 인덱스를 계산합니다.
14
+ char_index = ord(c) - HANGUL_BEGIN_UNICODE
15
+ chosung_index = char_index // CHOSUNG_BASE
16
+ jungsung_index = (char_index % CHOSUNG_BASE) // JUNGSUNG_BASE
17
+ jongsung_index = char_index % JUNGSUNG_BASE
18
+
19
+ # 중성 및 종성 분해
20
+ jungsung = JUNGSUNG_LIST[jungsung_index]
21
+ jongsung = JONGSUNG_LIST[jongsung_index]
22
+
23
+ jungsung_decomposed = JUNGSUNG_DECOMPOSE.get(jungsung, [jungsung])
24
+ jongsung_decomposed = JONGSUNG_DECOMPOSE.get(jongsung, [jongsung])
25
+
26
+ return [CHOSUNG_LIST[chosung_index]] + jungsung_decomposed + jongsung_decomposed
27
+
28
+ return [c] # 한글이 아니면 그대로 반환
@@ -0,0 +1,43 @@
1
+ # josa.py
2
+
3
+ from hangulpy.utils import is_hangul_char, HANGUL_BEGIN_UNICODE, JONGSUNG_COUNT
4
+
5
+ def has_jongsung(char):
6
+ """
7
+ 주어진 한글 음절에 받침이 있는지 확인합니다.
8
+
9
+ :param char: 한글 음절 문자
10
+ :return: 받침이 있으면 True, 없으면 False
11
+ """
12
+ if is_hangul_char(char):
13
+ # 한글 음절의 유니코드 값을 기준으로 받침의 유무를 확인합니다.
14
+ char_index = ord(char) - HANGUL_BEGIN_UNICODE
15
+ return (char_index % JONGSUNG_COUNT) != 0
16
+ return False
17
+
18
+ def josa(word, particle):
19
+ """
20
+ 주어진 단어에 적절한 조사를 붙여 반환합니다.
21
+
22
+ :param word: 조사와 결합할 단어
23
+ :param particle: 붙일 조사 ('을/를', '이/가', '은/는', '과/와')
24
+ :return: 적절한 조사가 붙은 단어 문자열
25
+ """
26
+ if not word:
27
+ return ''
28
+
29
+ # 단어의 마지막 글자를 가져옵니다.
30
+ word_ending = word[-1]
31
+ # 마지막 글자의 받침 유무를 확인합니다.
32
+ jongsung_exists = has_jongsung(word_ending)
33
+
34
+ if particle == '을/를':
35
+ return word + ('을' if jongsung_exists else '를')
36
+ elif particle == '이/가':
37
+ return word + ('이' if jongsung_exists else '가')
38
+ elif particle == '은/는':
39
+ return word + ('은' if jongsung_exists else '는')
40
+ elif particle == '과/와':
41
+ return word + ('과' if jongsung_exists else '와')
42
+ else:
43
+ raise ValueError(f"Unsupported particle: {particle}")
@@ -0,0 +1,84 @@
1
+ # utils.py
2
+
3
+ # 유니코드 한글 시작과 끝
4
+ # 유니코드에서 한글 음절은 U+AC00에서 U+D7A3까지 할당되어 있습니다.
5
+ HANGUL_BEGIN_UNICODE = 0xAC00 # '가'의 유니코드 값
6
+ HANGUL_END_UNICODE = 0xD7A3 # '힣'의 유니코드 값
7
+
8
+ # 초성 리스트
9
+ # 한글의 초성 19개를 정의합니다.
10
+ CHOSUNG_LIST = [
11
+ 'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'
12
+ ]
13
+
14
+ # 초성 인덱스 계산 상수
15
+ # 초성, 중성, 종성의 조합으로 한글 음절을 만들 때, 초성의 위치를 계산하는 데 사용합니다.
16
+ CHOSUNG_BASE = 588 # 초성당 588개의 음절이 있습니다. (21 * 28)
17
+
18
+ # 중성 리스트
19
+ # 한글의 중성 21개를 정의합니다.
20
+ JUNGSUNG_LIST = [
21
+ 'ㅏ', 'ㅐ', 'ㅑ', 'ㅒ', 'ㅓ', 'ㅔ', 'ㅕ', 'ㅖ', 'ㅗ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅛ', 'ㅜ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅠ', 'ㅡ', 'ㅢ', 'ㅣ'
22
+ ]
23
+
24
+ # 복합 중성 분해 정보
25
+ JUNGSUNG_DECOMPOSE = {
26
+ 'ㅘ': ('ㅗ', 'ㅏ'),
27
+ 'ㅙ': ('ㅗ', 'ㅐ'),
28
+ 'ㅚ': ('ㅗ', 'ㅣ'),
29
+ 'ㅝ': ('ㅜ', 'ㅓ'),
30
+ 'ㅞ': ('ㅜ', 'ㅔ'),
31
+ 'ㅟ': ('ㅜ', 'ㅣ'),
32
+ 'ㅢ': ('ㅡ', 'ㅣ')
33
+ }
34
+
35
+ # 종성 리스트
36
+ # 한글의 종성 28개를 정의합니다.
37
+ JONGSUNG_LIST = [
38
+ '', 'ㄱ', 'ㄲ', 'ㄳ', 'ㄴ', 'ㄵ', 'ㄶ', 'ㄷ', 'ㄹ', 'ㄺ', 'ㄻ', 'ㄼ', 'ㄽ', 'ㄾ', 'ㄿ', 'ㅀ', 'ㅁ', 'ㅂ', 'ㅄ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'
39
+ ]
40
+
41
+ # 복합 종성 분해 정보
42
+ JONGSUNG_DECOMPOSE = {
43
+ 'ㄳ': ('ㄱ', 'ㅅ'),
44
+ 'ㄵ': ('ㄴ', 'ㅈ'),
45
+ 'ㄶ': ('ㄴ', 'ㅎ'),
46
+ 'ㄺ': ('ㄹ', 'ㄱ'),
47
+ 'ㄻ': ('ㄹ', 'ㅁ'),
48
+ 'ㄼ': ('ㄹ', 'ㅂ'),
49
+ 'ㄽ': ('ㄹ', 'ㅅ'),
50
+ 'ㄾ': ('ㄹ', 'ㅌ'),
51
+ 'ㄿ': ('ㄹ', 'ㅍ'),
52
+ 'ㅀ': ('ㄹ', 'ㅎ'),
53
+ 'ㅄ': ('ㅂ', 'ㅅ')
54
+ }
55
+
56
+ # 중성 인덱스 계산 상수
57
+ # 중성의 위치를 계산하는 데 사용합니다.
58
+ JUNGSUNG_BASE = 28 # 중성당 28개의 음절이 있습니다.
59
+
60
+ # 받침 유무를 확인하기 위한 상수
61
+ # 한글 음절의 받침 개수는 28개입니다.
62
+ JONGSUNG_COUNT = 28
63
+
64
+ # 숫자 읽기 관련 코드
65
+ UNITS = [
66
+ "", "십", "백", "천", "만", "십만", "백만", "천만", "억", "십억", "백억", "천억",
67
+ "조", "십조", "백조", "천조", "경", "십경", "백경", "천경", "해", "십해", "백해", "천해",
68
+ "자", "십자", "백자", "천자", "양", "십양", "백양", "천양", "구", "십구", "백구", "천구",
69
+ "간", "십간", "백간", "천간", "정", "십정", "백정", "천정", "재", "십재", "백재", "천재",
70
+ "극", "십극", "백극", "천극", "항하사", "십항하사", "백항하사", "천항하사", "아승기", "십아승기", "백아승기", "천아승기",
71
+ "나유타", "십나유타", "백나유타", "천나유타", "불가사의", "십불가사의", "백불가사의", "천불가사의", "무량대수", "십무량대수", "백무량대수", "천무량대수",
72
+ "겁", "십겁", "백겁", "천겁", "훈공", "십훈공", "백훈공", "천훈공"
73
+ ]
74
+
75
+ NUMBERS = ["", "일", "이", "삼", "사", "오", "육", "칠", "팔", "구"]
76
+
77
+ def is_hangul_char(c):
78
+ """
79
+ 주어진 문자가 한글 음절인지 확인합니다.
80
+
81
+ :param c: 확인할 문자
82
+ :return: 한글 음절이면 True, 아니면 False
83
+ """
84
+ return HANGUL_BEGIN_UNICODE <= ord(c) <= HANGUL_END_UNICODE
@@ -0,0 +1,139 @@
1
+ Metadata-Version: 2.1
2
+ Name: hangulpy
3
+ Version: 1.0.0
4
+ Summary: A Python library for processing Hangul, inspired by es-hangul.
5
+ Home-page: https://github.com/gaon12/hangulpy
6
+ Author: Jeong Gaon
7
+ Author-email: gokirito12@gmail.com
8
+ License: MIT
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Requires-Python: >=3.8
13
+ Description-Content-Type: text/markdown
14
+ License-File: LICENSE
15
+
16
+ # hangulpy
17
+
18
+ hangulpy는 한글 처리를 위한 파이썬 라이브러리입니다. es-hangul의 파이썬 버전으로, 초성 검색과 조사 붙이기 등의 기능을 제공합니다.
19
+
20
+ ## 설치
21
+
22
+ ```bash
23
+ pip install hangulpy
24
+ ```
25
+
26
+ ## 사용법
27
+
28
+ ### 초성 검색
29
+
30
+ ```python
31
+ from hangulpy import chosungIncludes
32
+
33
+ searchWord = '라면'
34
+ userInput = 'ㄹㅁ'
35
+
36
+ result = chosungIncludes(searchWord, userInput)
37
+ print(result) # True
38
+ ```
39
+
40
+ ### 조사 붙이기
41
+
42
+ ```python
43
+ from hangulpy import josa
44
+
45
+ word1 = '사과'
46
+ sentence1 = josa(word1, '을/를') + ' 먹었습니다.'
47
+ print(sentence1) # '사과를 먹었습니다.'
48
+
49
+ word2 = '바나나'
50
+ sentence2 = josa(word2, '이/가') + ' 맛있습니다.'
51
+ print(sentence2) # '바나나가 맛있습니다.'
52
+ ```
53
+
54
+ ### 자음 또는 모음 여부
55
+
56
+ ```python
57
+ from hangulpy import is_hangul_consonant, is_hangul_vowel
58
+
59
+ char1 = 'ㄱ'
60
+ char2 = 'ㅏ'
61
+
62
+ print(is_hangul_consonant('ㄱ')) # True
63
+ print(is_hangul_consonant('ㅏ')) # False
64
+ print(is_hangul_vowel('ㅏ')) # True
65
+ print(is_hangul_vowel('ㄱ')) # False
66
+ ```
67
+
68
+ ### 문자열 포함 여부 확인
69
+
70
+ ```python
71
+ from hangulpy import hangul_contains
72
+
73
+ word = '사과'
74
+ print(hangul_contains(word, '')) # True
75
+ print(hangul_contains(word, '', notallowempty=True)) # False
76
+ print(hangul_contains(word, 'ㅅ')) # True
77
+ print(hangul_contains(word, '삭')) # True
78
+ print(hangul_contains(word, '삽')) # False
79
+ print(hangul_contains(word, '사과')) # True
80
+
81
+ # 문장처럼 입력 값 사이에 공백이 포함된 경우
82
+ print(hangul_contains('사과는 맛있다', '사과는 ㅁ')) # True
83
+ print(hangul_contains('사과는 맛있다', '사과는 ')) # True
84
+ ```
85
+
86
+ ### 초/중/종성 분해(문자열 변환)
87
+ ```python
88
+ from hangulpy import decompose_hangul_char
89
+
90
+ char = '괜'
91
+ print(decompose_hangul_char(char)) # ('ㄱ', ('ㅗ', 'ㅐ'), ('ㄴ', 'ㅈ'))
92
+ ```
93
+
94
+ #### 분해 시 배열로 반환
95
+ ```python
96
+ char1 = '값'
97
+ print(split_hangul_char(char1)) # ['ㄱ', 'ㅏ', 'ㅂ', 'ㅅ']
98
+
99
+ char2 = 'ㅘ'
100
+ print(split_hangul_char(char2)) # ['ㅗ', 'ㅏ']
101
+
102
+ char3 = 'ㄵ'
103
+ print(split_hangul_char(char3)) # ['ㄴ', 'ㅈ']
104
+ ```
105
+
106
+ ### 자음으로 끝나는지 확인
107
+ ```python
108
+ from hangulpy import ends_with_consonant
109
+
110
+ print(ends_with_consonant('강')) # False
111
+ print(ends_with_consonant('각')) # True
112
+ print(ends_with_consonant('ㄱ')) # True
113
+ print(ends_with_consonant('ㅏ')) # False
114
+ print(ends_with_consonant('a')) # False
115
+ print(ends_with_consonant('한')) # True
116
+ print(ends_with_consonant('하')) # False
117
+ ```
118
+
119
+ ### 초성 또는 종성으로 쓰일 수 있는지 확인
120
+ ```python
121
+ from hangulpy import can_be_chosung, can_be_jongsung
122
+
123
+ print(can_be_chosung('ㄱ')) # True
124
+ print(can_be_chosung('ㄳ')) # False
125
+ print(can_be_chosung('ㄸ')) # True
126
+ print(can_be_jongsung('ㄲ')) # True
127
+ print(can_be_jongsung('ㄸ')) # False
128
+ print(can_be_jongsung('ㄳ')) # True
129
+ ```
130
+
131
+ ### 숫자 읽기
132
+ ```python
133
+ from hangulpy import number_to_hangul, hangul_to_number
134
+
135
+ print(number_to_hangul(1234)) # 천이백삼십사
136
+ print(number_to_hangul(3.1415926)) # 삼 점 일사일오구이육
137
+ print(hangul_to_number("천이백삼십사")) # 1234
138
+ print(hangul_to_number("삼점일사일오구이육")) # 3.1415926
139
+ ```
@@ -0,0 +1,18 @@
1
+ LICENSE
2
+ README.md
3
+ setup.py
4
+ hangulpy/__init__.py
5
+ hangulpy/chosung.py
6
+ hangulpy/hangul_check.py
7
+ hangulpy/hangul_contains.py
8
+ hangulpy/hangul_decompose.py
9
+ hangulpy/hangul_ends_with_consonant.py
10
+ hangulpy/hangul_number.py
11
+ hangulpy/hangul_role.py
12
+ hangulpy/hangul_split.py
13
+ hangulpy/josa.py
14
+ hangulpy/utils.py
15
+ hangulpy.egg-info/PKG-INFO
16
+ hangulpy.egg-info/SOURCES.txt
17
+ hangulpy.egg-info/dependency_links.txt
18
+ hangulpy.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ hangulpy
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,21 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(
4
+ name='hangulpy',
5
+ version='1.0.0',
6
+ description='A Python library for processing Hangul, inspired by es-hangul.',
7
+ long_description=open('README.md', encoding='utf-8').read(),
8
+ long_description_content_type='text/markdown',
9
+ url='https://github.com/gaon12/hangulpy',
10
+ author='Jeong Gaon',
11
+ author_email='gokirito12@gmail.com',
12
+ license='MIT',
13
+ packages=find_packages(),
14
+ install_requires=[],
15
+ classifiers=[
16
+ 'Programming Language :: Python :: 3',
17
+ 'License :: OSI Approved :: MIT License',
18
+ 'Operating System :: OS Independent',
19
+ ],
20
+ python_requires='>=3.8',
21
+ )