Skip to content

Instantly share code, notes, and snippets.

@vbkaisetsu
Last active December 22, 2015 07:29
Show Gist options
  • Save vbkaisetsu/6438282 to your computer and use it in GitHub Desktop.
Save vbkaisetsu/6438282 to your computer and use it in GitHub Desktop.
poファイルを一括修正するためのスクリプト(主に長音表記化用) データファイルはここ: https://gist.github.com/vbkaisetsu/6438295
#!/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 <sam@hocevar.net>
#
# 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