normalize.py 5.7 KB
# Tomasz Olejniczak 2011, 2012
'''
Uwaga! Aby zachować odpowiedniość wyników musiałam we flagach re
(linie 69 i 80) dopisać re.ASCII. To niestety powoduje, że nie działa
dopasowanie np. "á" oraz "Á" (mimo re.IGNORECASE). Za to bez problemu
przechodzi reguła dopasowująca "ſ" (tylko do takiego znaku). Jeśli pominie
się re.ASCII i dopuści pracę na pełnym Unicode ten znak dopasowuje się
do "s" i proces się zapętla. Gdyby więc usunąć re.ASCII, to trzeba też
usunąć reguły 11 i 85 w new_rules.csv
'''
import re
import sys
import os
from optparse import OptionParser

re._MAXCACHE = 10000

rules = []
symbols = []
exceptions = {}
verbose = False
SEPARATOR = ";"

def readSymbol(els):
	global symbols
	res = "["
	chars = els[2].split(",")
	for c in chars:
		res += c.strip()
	res += "]"
	symbols.append((els[0], res))

def readExceptions(path):
	global exceptions
	f = open(path, encoding='utf-8')
	for line in f:
#		line = str(line, "utf-8")
		if line == "\n":
			continue
		line = line[:-1]
		els = line.split(SEPARATOR)
		exceptions.setdefault(els[0], els[1:])
	f.close()

def readRules(path):
	global rules, symbols
	f = open(path, encoding='utf-8')
	for line in f:
#		line = str(line, "utf-8")
		if line[0] == "#":
			continue
		if line == "\n":
			continue
		line = line[:-1]
		els = line.split(SEPARATOR)
		if len(els) < 5:
			if els[0] != "0":
				readSymbol(els[1:5])
		else:
			if els[1] != "0":
				for i in range(3, 7):
					if els[i] == "":
						els[i] = ".*"
				rules.append(els[3:7] + els[0:1])
	f.close()
	for i in range(0, len(rules)):
		for j in range(0, 3):
			rules[i][j] = rules[i][j].strip()
		for (s, d) in symbols:
			rules[i][0] = rules[i][0].replace(s, d)
			rules[i][2] = rules[i][2].replace(s, d)

def contextFind(rex, leftContext, rightContext, string):
	for m in re.finditer(rex, string, re.IGNORECASE|re.ASCII):
		#print(rex, string)
		if m.start() == 0:
			before = ""
		else:
			before = string[:m.start()]
		after = string[m.end():]
#		print("woyſka")
#		string = "woyſka"
#		print(".*" + leftContext + "$", before, rightContext, after)
#		print(re.match(".*" + leftContext + "$", before, re.IGNORECASE), re.match(rightContext, after, re.IGNORECASE))
		if re.match(".*" + leftContext + "$", before, re.IGNORECASE|re.ASCII) != None and re.match(rightContext, after, re.IGNORECASE|re.ASCII) != None:
			return (before, string[m.start():m.end()], after)
	return (None, None, None)

def processToken(token, eol=False):
	global rules, verbose, exceptions
	charmap = {i : [i] for i in range(len(token))}
	for (left, rex, right, text, idd) in rules:
		excepts = exceptions.get(idd)
		ignore = False
		if excepts != None:
			for ex in excepts:
				m = re.match(ex + ("\n" if eol else ""), token)
				if m != None and len(token) == m.end():
					ignore = True
					if verbose and contextFind(rex, left, right, token)[0] != None:
						print(token, "    #", idd, "ignored due to", ex)
		if ignore:
			continue
		(a, b, c) = contextFind(rex, left, right, token)
		while a != None:
			oldToken = token
			if b[0].isupper():
				token = a + text[0].upper() + text[1:] + c
			else:
				token = a + text + c

			beg = len(a)
			end = len(a)+len(text)
			oend = len(a)+len(b)
			newcharmap = {}
			for i in range(len(a)):
				newcharmap[i] = list(charmap[i])
			for i in range(len(c)):
				newcharmap[i+end] = list(charmap[i+oend])
			if len(b) >= len(text):
				for i in range(len(text)):
					newcharmap[i+beg] = list(charmap[i+beg])
				newcharmap[beg+len(text)-1] += [i for x in range(end, oend) for i in charmap[x]]
			else:
				for i in range(len(b)):
					newcharmap[i+beg] = list(charmap[i+beg])
				for i in range(oend, end):
					newcharmap[i] = list(charmap[oend-1])

			if verbose:
				print(oldToken, token, "   #", (idd + ":"), left, rex, right)
				print(repr(charmap), "->", repr(newcharmap))

			charmap = newcharmap
			(a, b, c) = contextFind(rex, left, right, token)
	#print(token)
	return token, charmap

def main(argv):
	global verbose, SEPARATOR
	usage = "%prog [OPTIONS] RULES_FILE INPUT_FILE OUTPUT_FILE"
	parser = OptionParser(usage = usage, version = "normalize.py 0.1")
	parser.add_option("-f", "--frequency-list", help="input file is frequency list", action="store_true", dest="freqlist", default=False)
	parser.add_option("-v", "--verbose", help="print information about normalized words", action="store_true", dest="verbose", default=False)
	parser.add_option("-e", "--exceptions", help="exception file", dest="exceptions", default=None)
	parser.add_option("-s", "--separator", help="CSV separator", dest="separator", default=";")
	(options, args) = parser.parse_args(argv)
	#print args
	if len(args) != 4:
		parser.print_help()
		exit()
	if options.verbose:
		verbose = True
	if options.exceptions:
		readExceptions(options.exceptions)
	SEPARATOR = options.separator
	if SEPARATOR == "\\t":
		SEPARATOR = "\t"
	readRules(args[1])
	f = open(args[2], encoding='utf-8')
	reses = []
	for line in f:
#		line = str(line, "utf-8")
		if line == "\n":
			continue
		if options.freqlist:
			els = line.split(" ")
			i = 0
			for j in range(0, len(els)):
				if els[j] != "":
					i = j
					break
			token = ""
			for j in range(i + 1, len(els)):
				token += els[j] + " "
			token = token.strip()
			res = " " * (7 - len(els[i])) + els[i] + " " + processToken(token) + "\n"
		else:
			token = line.strip()
			res = processToken(token + "\n", eol=True)
		reses.append(res)
	f.close()
	f = open(args[3], "w", encoding='utf-8')
	for res in reses:
#		f.write(res.encode("utf-8"))  # poprawka, aby można było transkrybować tekstowe dokumenty
		f.write(res[0])
	f.close()
	
if __name__ == '__main__': sys.exit(main(sys.argv))

# Copyright Formal Linguistics Department, University of Warsaw  2011,2012; klf@uw.edu.pl.
# Licence GNU General Public Lincense version 2 or later