반응형

[python] 한글 자음 확인해서 치환하기 

 

""" 한글 자음인지 확인 
"""
# Define the list of Korean initial consonants (초성)
INITIAL_CONSONANTS = [
    'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 
    'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 
    'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'
]

def is_korean_char(ch):
    """Check if the character is a Korean syllable."""
    return 0xAC00 <= ord(ch) <= 0xD7A3

def get_initial_consonant(ch):
    """Extract the initial consonant from a Korean syllable."""
    if not is_korean_char(ch):
        return None  # or you could return an empty string or raise an exception
    
    # Calculate the index for the initial consonant
    initial_index = (ord(ch) - 0xAC00) // (21 * 28)
    return INITIAL_CONSONANTS[initial_index]

# Example usage
sentence = "안녕하세요"
initials = [get_initial_consonant(ch) for ch in sentence if is_korean_char(ch)]
print(''.join(initials))  # Output: ㅇㄴㅎㅅㅇ
반응형
반응형

[python] 한글 자음, 모음, 초성 추출하기 

 

# pip install jamotools 
# https://pypi.org/project/jamotools/
# A library for Korean Jamo split and vectorize. 
#
# 음절 분할 및 jamos를 음절에 결합하는 API는 hangul-utils 를 기반으로 합니다 .
# 
# Split_syllables : 음절 문자열을 jamos 문자열로 변환하고 유니코드 유형을 변환하도록 선택할 수 있습니다.
# Join_jamos : jamos 문자열을 음절 문자열로 변환합니다.
# Normalize_to_compat_jamo : jamos 문자열을 한글 호환성 Jamo 문자열로 정규화합니다 .
import jamotools

print(jamotools.split_syllable_char(u"안"))
#('ㅇ', 'ㅏ', 'ㄴ')

print(jamotools.split_syllables(u"안녕하세요"))
# ㅇㅏㄴㄴㅕㅇㅎㅏㅅㅔㅇㅛ


sentence = u"앞 집 팥죽은 붉은 팥 풋팥죽이고, 뒷집 콩죽은 햇콩 단콩 콩죽.우리 집  깨죽은 검은 깨 깨죽인데 사람들은 햇콩 단콩 콩죽 깨죽 죽먹기를 싫어하더라."
s = jamotools.split_syllables(sentence)
print(s, '\n')

""" ㅇㅏㅍ ㅈㅣㅂ ㅍㅏㅌㅈㅜㄱㅇㅡㄴ ㅂㅜㄺㅇㅡㄴ ㅍㅏㅌ ㅍㅜㅅㅍㅏㅌㅈㅜㄱㅇㅣㄱㅗ,
ㄷㅟㅅㅈㅣㅂ ㅋㅗㅇㅈㅜㄱㅇㅡㄴ ㅎㅐㅅㅋㅗㅇ ㄷㅏㄴㅋㅗㅇ ㅋㅗㅇㅈㅜㄱ.ㅇㅜㄹㅣ
ㅈㅣㅂ ㄲㅐㅈㅜㄱㅇㅡㄴ ㄱㅓㅁㅇㅡㄴ ㄲㅐ ㄲㅐㅈㅜㄱㅇㅣㄴㄷㅔ ㅅㅏㄹㅏㅁㄷㅡㄹㅇㅡㄴ
ㅎㅐㅅㅋㅗㅇ ㄷㅏㄴㅋㅗㅇ ㅋㅗㅇㅈㅜㄱ ㄲㅐㅈㅜㄱ ㅈㅜㄱㅁㅓㄱㄱㅣㄹㅡㄹ
ㅅㅣㅀㅇㅓㅎㅏㄷㅓㄹㅏ. """

sentence2 = jamotools.join_jamos(s)
print(sentence2)
""" 앞 집 팥죽은 붉은 팥 풋팥죽이고, 뒷집 콩죽은 햇콩 단콩 콩죽.우리 집 깨죽은 검은 깨
깨죽인데 사람들은 햇콩 단콩 콩죽 깨죽 죽먹기를 싫어하더라. """

print(sentence == sentence2)
# True


# 자음만 추출
def extract_vowels(text):
    vowels = set(['ㅏ', 'ㅑ', 'ㅓ', 'ㅕ', 'ㅗ', 'ㅛ', 'ㅜ', 'ㅠ', 'ㅡ', 'ㅣ', 'ㅐ', 'ㅒ', 'ㅔ', 'ㅖ', 'ㅘ', 'ㅙ', 'ㅚ', 'ㅝ', 'ㅞ', 'ㅟ', 'ㅢ'])
    result = ''
    for char in text:
        if '가' <= char <= '힣':  # Check if the character is Hangul
            syllables = jamotools.split_syllables(char)
            for syllable in syllables:
                if syllable in vowels:
                    result += syllable
    return result

sentence = u"앞 집 팥죽은 붉은 팥 풋팥죽이고, 뒷집 콩죽은 햇콩 단콩 콩죽.우리 집  깨죽은 검은 깨 깨죽인데 사람들은 햇콩 단콩 콩죽 깨죽 죽먹기를 싫어하더라."
vowels_only = extract_vowels(sentence)
print(vowels_only)
# ㅏㅣㅏㅜㅡㅜㅡㅏㅜㅏㅜㅣㅗㅟㅣㅗㅜㅡㅐㅗㅏㅗㅗㅜㅜㅣㅣㅐㅜㅡㅓㅡㅐㅐㅜㅣㅔㅏㅏㅡㅡㅐㅗㅏㅗㅗㅜㅐㅜㅜㅓㅣㅡㅣㅓㅏㅓㅏ


# 모음만 추출 
def extract_consonants(text):
    consonants = set(['ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ', 'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ'])
    result = ''
    for char in text:
        if '가' <= char <= '힣':  # Check if the character is Hangul
            syllables = jamotools.split_syllables(char)
            for syllable in syllables:
                if syllable in consonants:
                    result += syllable
    return result

sentence = u"앞 집 팥죽은 붉은 팥 풋팥죽이고, 뒷집 콩죽은 햇콩 단콩 콩죽.우리 집  깨죽은 검은 깨 깨죽인데 사람들은 햇콩 단콩 콩죽 깨죽 죽먹기를 싫어하더라."
consonants_only = extract_consonants(sentence)
print(consonants_only)
# ㅇㅍㅈㅂㅍㅌㅈㄱㅇㄴㅂㅇㄴㅍㅌㅍㅅㅍㅌㅈㄱㅇㄱㄷㅅㅈㅂㅋㅇㅈㄱㅇㄴㅎㅅㅋㅇㄷㄴㅋㅇㅋㅇㅈㄱㅇㄹㅈㅂㄲㅈㄱㅇㄴㄱㅁㅇㄴㄲㄲㅈㄱㅇㄴㄷㅅㄹㅁㄷㄹㅇㄴㅎㅅㅋㅇㄷㄴㅋㅇㅋㅇㅈㄱㄲㅈㄱㅈㄱㅁㄱㄱㄹㄹㅅㅇㅎㄷㄹ


# 초성만 추출 
def extract_initial_consonants(text):
    result = ''
    for char in text:
        if '가' <= char <= '힣':  # Check if the character is Hangul
            initial_consonant = jamotools.split_syllable_char(char)[0]
            result += initial_consonant
    return result

sentence = u"앞 집 팥죽은 붉은 팥 풋팥죽이고, 뒷집 콩죽은 햇콩 단콩 콩죽.우리 집  깨죽은 검은 깨 깨죽인데 사람들은 햇콩 단콩 콩죽 깨죽 죽먹기를 싫어하더라."
initial_consonants_only = extract_initial_consonants(sentence)
print(initial_consonants_only)
반응형
반응형

https://namu.wiki/w/%ED%95%9C%EA%B8%80/%EC%9E%90%EB%AA%A8

 

한글/자모

현재 사전이나 컴퓨터 한글 코드에서 한글 자모는 다음 순으로 배열한다. 한글 맞춤법 제4항 붙임 2와 그 해설에 따

namu.wiki

 

한글을 이루는 낱글자. '자모'라고 하여 ' 음'과 ' 음'의 약자로 알고 있는 경우가 많으나, 한자를 보면 母가 아닌 母이다. '글자(字)를 이루는 모(母)체'라는 뜻.

 

자음(19자) : ㄱ ㄲ ㄴ ㄷ ㄸ ㄹ ㅁ ㅂ ㅃ ㅅ ㅆ ㅇ ㅈ ㅉ ㅊ ㅋ ㅌ ㅍ ㅎ
모음(21자) : ㅏ ㅐ ㅑ ㅒ ㅓ ㅔ ㅕ ㅖ ㅗ ㅘ ㅙ ㅚ ㅛ ㅜ ㅝ ㅞ ㅟ ㅠ ㅡ ㅢ ㅣ
받침(27자) : ㄱ ㄲ ㄳ ㄴ ㄵ ㄶ ㄷ ㄹ ㄺ ㄻ ㄼ ㄽ ㄾ ㄿ ㅀ ㅁ ㅂ ㅄ ㅅ ㅆ ㅇ ㅈ ㅊ ㅋ ㅌ ㅍ ㅎ

 

반응형
반응형

[python] 한글 자음 모음 분리하기 

 

 

pip install jamo

https://pypi.org/project/jamo/

 

jamo

A Hangul syllable and jamo analyzer.

pypi.org

https://github.com/jdongian/python-jamo  

 

------------

 

pip install jamotools

https://pypi.org/project/jamotools/

A library for Korean Jamo split and vectorize.  한국어 Jamo를 분할하고 벡터화하는 라이브러리입니다.

 

 

>>> import jamotools
>>> print(jamotools.split_syllable_char(u"안"))
('ㅇ', 'ㅏ', 'ㄴ')

>>> print(jamotools.split_syllables(u"안녕하세요"))
ㅇㅏㄴㄴㅕㅇㅎㅏㅅㅔㅇㅛ

>>> sentence = u"앞 집 팥죽은 붉은 팥 풋팥죽이고, 뒷집 콩죽은 햇콩 단콩 콩죽.우리 집
    깨죽은 검은 깨 깨죽인데 사람들은 햇콩 단콩 콩죽 깨죽 죽먹기를 싫어하더라."
>>> s = jamotools.split_syllables(sentence)
>>> print(s)
ㅇㅏㅍ ㅈㅣㅂ ㅍㅏㅌㅈㅜㄱㅇㅡㄴ ㅂㅜㄺㅇㅡㄴ ㅍㅏㅌ ㅍㅜㅅㅍㅏㅌㅈㅜㄱㅇㅣㄱㅗ,
ㄷㅟㅅㅈㅣㅂ ㅋㅗㅇㅈㅜㄱㅇㅡㄴ ㅎㅐㅅㅋㅗㅇ ㄷㅏㄴㅋㅗㅇ ㅋㅗㅇㅈㅜㄱ.ㅇㅜㄹㅣ
ㅈㅣㅂ ㄲㅐㅈㅜㄱㅇㅡㄴ ㄱㅓㅁㅇㅡㄴ ㄲㅐ ㄲㅐㅈㅜㄱㅇㅣㄴㄷㅔ ㅅㅏㄹㅏㅁㄷㅡㄹㅇㅡㄴ
ㅎㅐㅅㅋㅗㅇ ㄷㅏㄴㅋㅗㅇ ㅋㅗㅇㅈㅜㄱ ㄲㅐㅈㅜㄱ ㅈㅜㄱㅁㅓㄱㄱㅣㄹㅡㄹ
ㅅㅣㅀㅇㅓㅎㅏㄷㅓㄹㅏ.

>>> sentence2 = jamotools.join_jamos(s)
>>> print(sentence2)
앞 집 팥죽은 붉은 팥 풋팥죽이고, 뒷집 콩죽은 햇콩 단콩 콩죽.우리 집 깨죽은 검은 깨
깨죽인데 사람들은 햇콩 단콩 콩죽 깨죽 죽먹기를 싫어하더라.

>>> print(sentence == sentence2)
True
반응형
반응형

Pandas df.to_csv("file.csv" encode="utf-8") still gives trash characters for minus sign

 

Pandas df.to_csv("file.csv" encode="utf-8") still gives trash characters for minus sign

I've read something about a Python 2 limitation with respect to Pandas' to_csv( ... etc ...). Have I hit it? I'm on Python 2.7.3 This turns out trash characters for ≥ and - when they appear in st...

stackoverflow.com

https://stackoverflow.com/questions/25788037/pandas-df-to-csvfile-csv-encode-utf-8-still-gives-trash-characters-for-min
pandas dataframe을 csv 형태로,  to_csv 


utf-8 encoding으로 저장하면 
내용 중 한글이 깨지는 문제 발생
euc-kr 로 저장할 때는 문제 없음
df.to_csv('file.csv',encoding='euc-kr')


df.to_csv('file.csv',encoding='utf-8')
한글 깨짐


해결책: df.to_csv('file.csv',encoding='utf-8-sig')


windows 환경 (정확히는 win7)
Python 3.6.4 :: Anaconda custom (64-bit)
pandas==0.23.4

 

 

 

 

 

 

 

반응형
반응형

[tensorflow] 한글 음절 인식기 - hangul-syllable-recognition

 

https://github.com/junstar92/hangul-syllable-recognition

 

GitHub - junstar92/hangul-syllable-recognition: hangul syllable recognition 한글 음절 인식기

hangul syllable recognition 한글 음절 인식기. Contribute to junstar92/hangul-syllable-recognition development by creating an account on GitHub.

github.com

 

Introduction

한글은 조합이 다양하기 때문에 영어에 비해서 OCR 성능이 조금 떨어진다고 알고 있다.

다양한 폰트와 손글씨 데이터를 가지고, 얼마나 한글을 잘 인식하는지 확인하기 위해서 프로젝트를 진행했다.

Getting started

python and pakage version

python3 == 3.8.3

tensorflow_gpu == 2.3.0

numpy == 1.19.5

argparse == 1.1

pandas == 1.2.0

cv2 == 4.5.1

streamlit == 0.74.1

streamlit_drawable_canvas

반응형
반응형

 

영어/한국어 Word2Vec 실습

wikidocs.net/50739

 

위키독스

온라인 책을 제작 공유하는 플랫폼 서비스

wikidocs.net

 

반응형
반응형

Internet Explorer 에서의 ajax 에서의 한글 깨짐 현상

IE에서만 encodeURI를 적용하는게 맞다.

// 윈도우인지 다른 브라우저인지 확인 
            var ua = window.navigator.userAgent;
            var postData;
            // 윈도우라면 ? 
            if (ua.indexOf('MSIE') > 0 || ua.indexOf('Trident') > 0) {
                postData = encodeURI(sendData);
            } else {
                postData = sendData;
            }

            $.ajax({
                url: "thumnailUpload.php", // Url to which the request is send
                type: "POST",             // Type of request to be send, called as method
                data: postData, // Data sent to server, a set of key/value pairs (i.e. form fields and values)
                contentType: false,       // The content type used when sending data to the server.
                cache: false,     

 

반응형

'프로그래밍 > Script' 카테고리의 다른 글

JSFIDDLE - https://jsfiddle.net/  (0) 2020.01.10
12 Extremely Useful Hacks for JavaScript  (0) 2019.12.31
JSON Editor Online  (0) 2019.11.26
[jQuery] AJAX Cross Origin plugin  (0) 2019.10.21
javascript, nl2br, nl to &nbsp;  (0) 2019.09.11

+ Recent posts