Skip to content

Instantly share code, notes, and snippets.

@kyunghyuncho
Created August 12, 2019 18:05
Show Gist options
  • Save kyunghyuncho/f20a3f4564c6c169f507aebcfb087d9c to your computer and use it in GitHub Desktop.
Save kyunghyuncho/f20a3f4564c6c169f507aebcfb087d9c to your computer and use it in GitHub Desktop.
import copy
import re
import numpy
def align_sents(more_sentences, fewer_sentences):
j_more = 0
fewer2more = [None] * len(fewer_sentences)
more_sentences = more_sentences
for ii in range(len(fewer_sentences)):
fewer_sent = fewer_sentences[ii]
matches = [False] * len(more_sentences)
for jj in range(j_more+1, len(more_sentences)-(len(fewer_sentences)-ii+1)):
more_sent = more_sentences[jj]
ci_start = 0
found = False
for ci in range(len(fewer_sent)-1):
phr = fewer_sent[ci_start:ci+1]
if phr not in more_sent:
if found:
matches[jj] = True
break
else:
ci_start = ci_start + 1
else:
found = True
first_false = j_more+1
for jj in range(j_more+1, len(more_sentences)):
if not matches[jj]:
first_false = jj
break
if ii == len(fewer_sentences) - 1:
fewer2more[ii] = numpy.arange(j_more,len(more_sentences))
else:
fewer2more[ii] = numpy.arange(j_more,first_false)
j_more = first_false
return fewer2more
def sentence_segmentation(in_ko, in_zh, out_ko, out_zh):
ko_separator = '. '
zh_separator = '。 '
f_in_ko = open(in_ko, 'r')
f_in_zh = open(in_zh, 'r')
f_out_ko = open(out_ko, 'w')
f_out_zh = open(out_zh, 'w')
n_count = 0
for ko_line, zh_line in zip(f_in_ko, f_in_zh):
# ko_line = "참찬문하부사(參贊門下府事) 박위(朴葳)를 순군옥(巡軍獄)에 가두었다. 처음에 동래 현령(東萊縣令) 김가행(金可行)과 염장관(鹽場官) 박중질(朴仲質) 등이 국가의 안위(安危)와 왕씨(王氏)의 명운(命運)으로써 밀성(密城)의 장님[盲人] 이흥무(李興茂)에게 점[卜]을 쳤는데, 일이 발각되자, 흥무(興茂)를 잡아 와서 순군옥(巡軍獄)에 가두고, 성헌(省憲) 과 형조(刑曹)로 하여금 순군 만호부(巡軍萬戶府)와 함께 그 일을 조사하게 하니, 흥무가 죄를 자백[伏罪]하였다. “가행(可行)과 중질(仲質) 등이 박위(朴葳)의 말로써, 와서 점치게 하면서 말하기를, ‘고려 왕조 공양왕의 명운(命運)이 우리 주상 전하(主上殿下)보다 누가 낫겠는가? 또 왕씨(王氏)의 가운데서 누가 명운(命運)이 귀한 사람인가?’ 하므로, 내가 남평군(南平君) 왕화(王和)의 명운이 귀하다 하고, 그 아우 영평군(鈴平君) 왕거(王琚)가 그 다음이 된다고 하였습니다.” 이에 박위를 가두고 순군(巡軍)에게 명하여 가행과 중질을 경상도에서 잡아 오게 하였다."
# zh_line = "○囚參贊門下府事朴葳于巡軍獄。 初東萊縣令金可行、鹽場官朴仲質等, 以國家安危、王氏命運, 卜於密城盲人李興茂。 事覺, 執興茂來囚巡軍獄, 令省憲、刑曹, 同巡軍萬戶府, 案其事。 興茂伏之曰: “可行、仲質等, 以朴葳言來卜曰: ‘前朝恭讓之命, 與我主上殿下孰優? 且王氏之中, 誰是命貴者?’ 我以南平君王和之命爲貴, 其弟鈴平君王琚次之。” 於是囚葳, 命巡軍執可行、仲質于慶尙道。"
ko_sentences = ko_line.split(ko_separator)
zh_sentences = zh_line.split(zh_separator)
ko_sentences_pre = copy.copy(ko_sentences)
ko_sentences_post = []
for ko_sent in ko_sentences:
ko_sentences_post.append(' '.join(re.findall('\(.*?\)',ko_sent)).replace('(','').replace(')','').replace(' ', ''))
ko_sentences = ko_sentences_post
if len(ko_sentences) > len(zh_sentences):
alignment = align_sents(ko_sentences, zh_sentences)
aligned_pairs = []
for ii, more_idx in enumerate(alignment):
fewer = zh_sentences[ii]
more = ko_separator.join([ko_sentences_pre[jj] for jj in more_idx])
aligned_pairs.append((fewer, more))
for pair in aligned_pairs:
f_out_zh.write(pair[0].strip()+'\n')
f_out_ko.write(pair[1].strip()+'\n')
elif len(ko_sentences) < len(zh_sentences):
alignment = align_sents(zh_sentences, ko_sentences)
aligned_pairs = []
for ii, more_idx in enumerate(alignment):
fewer = ko_sentences_pre[ii]
more = zh_separator.join([zh_sentences[jj] for jj in more_idx])
aligned_pairs.append((fewer, more))
for pair in aligned_pairs:
f_out_ko.write(pair[0].strip()+'\n')
f_out_zh.write(pair[1].strip()+'\n')
else:
for ko, zh in zip(ko_sentences_pre,zh_sentences):
f_out_ko.write(ko.strip()+'\n')
f_out_zh.write(zh.strip()+'\n')
n_count = n_count + 1
if numpy.mod(n_count, 100) == 0:
print('.', end='')
if numpy.mod(n_count, 10000) == 0:
print(n_count)
f_in_ko.close()
f_in_zh.close()
f_out_ko.close()
f_out_zh.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment