Last active
December 22, 2015 07:29
-
-
Save vbkaisetsu/6438282 to your computer and use it in GitHub Desktop.
poファイルを一括修正するためのスクリプト(主に長音表記化用)
データファイルはここ: https://gist.github.com/vbkaisetsu/6438295
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
# | |
# Author: Koichi Akabe <vbkaisetsu at gmail.com> | |
# | |
############################################################################## | |
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
# Version 2, December 2004 | |
# | |
# Copyright (C) 2004 Sam Hocevar <[email protected]> | |
# | |
# Everyone is permitted to copy and distribute verbatim or modified | |
# copies of this license document, and changing it is allowed as long | |
# as the name is changed. | |
# | |
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
# | |
# 0. You just DO WHAT THE FUCK YOU WANT TO. | |
# | |
############################################################################## | |
import sys | |
table = [] | |
fp = open("convtable", "r") | |
for line in fp: | |
table.append(line.strip().split("\t")) | |
table.sort(key=lambda x: -len(x[0])) | |
def msgconvert(orig, msg): | |
i = 0 | |
tmpmsg = msg[:] | |
while i < len(tmpmsg): | |
for item in table: | |
if len(item) == 3: | |
if not item[2] in orig: | |
continue | |
if tmpmsg[i:i+len(item[0])] == item[0] and tmpmsg[i:i+len(item[1])] != item[1]: | |
tmpmsg = tmpmsg[:i] + item[1] + tmpmsg[i+len(item[0]):] | |
sys.stderr.write("%s -> %s\n" % (msg, item[1])) | |
i += len(item[1]) - 1 | |
break | |
i += 1 | |
return tmpmsg | |
msgid = msgstr = None | |
for line in sys.stdin: | |
line = line.strip() | |
if line[:6] == "msgid ": | |
tmp = line[6:].strip() | |
if len(tmp) >= 2 and tmp[0] == tmp[-1] == "\"": | |
msgid = tmp[1:-1] | |
elif line[:7] == "msgstr ": | |
tmp = line[7:].strip() | |
if len(tmp) >= 2 and tmp[0] == tmp[-1] == "\"": | |
msgstr = tmp[1:-1] | |
elif len(line) >= 2 and line[0] == line[-1] == "\"": | |
if msgid is None: | |
print(line) | |
elif msgstr is None: | |
msgid += line[1:-1] | |
else: | |
msgstr += line[1:-1] | |
elif not line: | |
if msgid is None or msgstr is None: | |
continue | |
msgstr = msgconvert(msgid.split(), msgstr) | |
print("msgid \"" + "\\n\"\n\"".join(msgid.split("\\n")) + "\"") | |
print("msgstr \"" + "\\n\"\n\"".join(msgstr.split("\\n")) + "\"") | |
print("") | |
msgid = msgstr = None | |
else: | |
print(line) | |
if msgid and msgstr: | |
print("msgid \"" + "\\n\"\n\"".join(msgid.split("\\n")) + "\"") | |
print("msgstr \"" + "\\n\"\n\"".join(msgstr.split("\\n")) + "\"") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment