def romanize(self, string): """Convert a string from Hangul to Latin Script :param string: string; any sentence, romanize if hangul else return the same text""" if not self.has_hangul(string): return string sentence_latin = '' syllables = list(string) for i, syllable in enumerate(syllables): if kr.is_hangul(syllable): sentence_latin += self.jaeum( syllable, self.__get_final_prior(syllables, i)) sentence_latin += self.moeum(syllable) sentence_latin += self.batchim( syllable, self.__get_initial_next(syllables, i)) else: sentence_latin += syllable sentence_latin = sentence_latin.strip() if sentence_latin and not sentence_latin[0].isupper() \ and sentence_latin[0].isalpha(): sentence_latin = sentence_latin.capitalize() return sentence_latin
def has_ssangbatchim(self, syllable): """True if param contains a double batchim :param syllable: char; a syllable""" if kr.is_hangul(syllable): return self.is_ssangbatchim(kr.get_final(syllable)) else: return False
def has_hangul(self, string): """Return True if there is any hangul char in the string""" syllables = list(string) for syllable in syllables: if kr.is_hangul(syllable): return True return False
def __get_final_prior(self, syllables, i): """Get the batchim of the prior syllable of a word :param syllabes: list; syllables in Hangul :param i: int; the current index of the syllable in the sentence""" if i > 0: if syllables[i - 1] != '' and kr.is_hangul(syllables[i - 1]): return kr.get_final(syllables[i - 1]) return ''
def __get_initial_next(self, syllables, i): """Get the initial letter of the next syllable of a word :param syllabes: list; syllables in Hangul :param i: int; the current index of the syllable in the sentence""" if i < (len(syllables) - 1): if syllables[i + 1] != '' and kr.is_hangul(syllables[i + 1]): return kr.get_initial(syllables[i + 1]) return ''
def pull(text): exploded = [list(hangul.split_char(c)) if hangul.is_hangul(c) else c for c in text] for i in range(len(exploded)): if i == 0: continue a = exploded[i - 1] b = exploded[i] if type(a) is list and type(b) is list: if not a[2] and b[0] != "ㅇ": a[2] = simplify_rule.get(b[0], b[0]) b[0] = "ㅇ" elif a[2] in compound_rule and b[0] in compound_rule[a[2]]: a[2] = compound_rule[a[2]][b[0]] b[0] = "ㅇ" return "".join([hangul.join_char(c) if type(c) is list else c for c in exploded])
def pull(text): exploded = [ list(hangul.split_char(c)) if hangul.is_hangul(c) else c for c in text ] for i in range(len(exploded)): if i == 0: continue a = exploded[i - 1] b = exploded[i] if type(a) is list and type(b) is list: if not a[2] and b[0] != "ㅇ": next = b[0] if next == "ㅉ": next = "ㅈ" elif next == "ㅃ": next = "ㅂ" elif next == "ㄸ": next = "ㄷ" a[2] = next b[0] = "ㅇ" elif a[2] == "ㄹ": if b[0] == "ㄱ": a[2] = "ㄺ" elif b[0] == "ㅁ": a[2] = "ㄻ" elif b[0] == "ㅍ": a[2] = "ㄿ" b[0] = "ㅇ" elif a[2] == "ㄱ" and b[0] == "ㅅ": a[2] = "ㄳ" b[0] = "ㅇ" elif a[2] == "ㄴ" and b[0] == "ㅈ": a[2] = "ㄵ" b[0] = "ㅇ" elif a[2] == "ㅂ" and b[0] == "ㅅ": a[2] = "ㅄ" b[0] = "ㅇ" elif a[2] == "ㄹ": if b[0] == "ㅂ": a[2] = "ㄼ" elif b[0] == "ㅅ": a[2] = "ㄽ" elif b[0] == "ㅌ": a[2] = "ㄾ" b[0] = "ㅇ" return "".join( [hangul.join_char(c) if type(c) is list else c for c in exploded])
def pull(text): exploded = [list(hangul.split_char(c)) if hangul.is_hangul(c) else c for c in text] for i in range(len(exploded)): if i == 0: continue a = exploded[i - 1] b = exploded[i] if type(a) is list and type(b) is list: if not a[2] and b[0] != "ㅇ": next = b[0] if next == "ㅉ": next = "ㅈ" elif next == "ㅃ": next = "ㅂ" elif next == "ㄸ": next = "ㄷ" a[2] = next b[0] = "ㅇ" elif a[2] == "ㄹ": if b[0] == "ㄱ": a[2] = "ㄺ" elif b[0] == "ㅁ": a[2] = "ㄻ" elif b[0] == "ㅍ": a[2] = "ㄿ" b[0] = "ㅇ" elif a[2] == "ㄱ" and b[0] == "ㅅ": a[2] = "ㄳ" b[0] = "ㅇ" elif a[2] == "ㄴ" and b[0] == "ㅈ": a[2] = "ㄵ" b[0] = "ㅇ" elif a[2] == "ㅂ" and b[0] == "ㅅ": a[2] = "ㅄ" b[0] = "ㅇ" elif a[2] == "ㄹ": if b[0] == "ㅂ": a[2] = "ㄼ" elif b[0] == "ㅅ": a[2] = "ㄽ" elif b[0] == "ㅌ": a[2] = "ㄾ" b[0] = "ㅇ" return "".join([hangul.join_char(c) if type(c) is list else c for c in exploded])