| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
# Copyright (C) 2020 Jaume Ortolà <[email protected]> |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or |
| 6 |
# modify it under the terms of the GNU General Public License as |
| 7 |
# published by the Free Software Foundation; either version 2 of the |
| 8 |
# License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 17 |
# |
| 18 |
|
| 19 |
import sys, re |
| 20 |
import xml.etree.ElementTree as ET |
| 21 |
|
| 22 |
def XMLtoString(x): |
| 23 |
rough_string = ET.tostring(x, encoding="UTF-8", method="xml"); |
| 24 |
return rough_string.decode("UTF-8") |
| 25 |
|
| 26 |
def word(e): |
| 27 |
word = None |
| 28 |
# e.find("i") doesn't word. Bug?! |
| 29 |
for part in e: |
| 30 |
if part.tag == "i": |
| 31 |
word = part.text |
| 32 |
if word is None: |
| 33 |
p = e.find("p") |
| 34 |
if p: |
| 35 |
l = p.find("l") |
| 36 |
word = l.text |
| 37 |
if word is None: |
| 38 |
word = "" |
| 39 |
return word |
| 40 |
|
| 41 |
def isMultiword(e): |
| 42 |
for part in e: |
| 43 |
if part.tag == "i": |
| 44 |
b = part.find("b") |
| 45 |
if b: |
| 46 |
return True |
| 47 |
p = e.find("p") |
| 48 |
if p: |
| 49 |
l=p.find("l") |
| 50 |
if l: |
| 51 |
g = l.find("g") |
| 52 |
if g: |
| 53 |
return True |
| 54 |
b = l.find("b") |
| 55 |
if b is not None: |
| 56 |
return True |
| 57 |
r=p.find("r") |
| 58 |
if r: |
| 59 |
g = r.find("g") |
| 60 |
if g: |
| 61 |
return True |
| 62 |
b = r.find("b") |
| 63 |
if b is not None: |
| 64 |
return True |
| 65 |
return False |
| 66 |
|
| 67 |
source = sys.argv[1] |
| 68 |
target = sys.argv[2] |
| 69 |
|
| 70 |
tree = ET.ElementTree() |
| 71 |
tree.parse(source) |
| 72 |
|
| 73 |
pardefs = tree.find('pardefs') |
| 74 |
|
| 75 |
prefixes = {} |
| 76 |
|
| 77 |
for pardef in pardefs.iter(tag='pardef'): |
| 78 |
namepardef = pardef.get("n") |
| 79 |
if namepardef.startswith("prefixes_"): |
| 80 |
grammarclass = re.sub ("prefixes_([^_]+)$", "\\1", namepardef) |
| 81 |
prefixes[grammarclass]=namepardef |
| 82 |
|
| 83 |
mainsection = tree.find('.//section[@id="main"]') |
| 84 |
|
| 85 |
for e in mainsection.iter(tag='e'): |
| 86 |
if isMultiword(e): |
| 87 |
continue |
| 88 |
par = e.find('par') |
| 89 |
if par is None: |
| 90 |
p = e.find('p') |
| 91 |
if p: |
| 92 |
par = p.find('r').find('s') |
| 93 |
if par is None: |
| 94 |
i = e.find('i') |
| 95 |
if i: |
| 96 |
par = i.find('s') |
| 97 |
if par is None: |
| 98 |
continue |
| 99 |
parname = par.get("n") |
| 100 |
for prefix in prefixes.keys(): |
| 101 |
if parname == prefix: |
| 102 |
new = ET.Element('par') |
| 103 |
prefixtoadd = prefixes[prefix] |
| 104 |
new.set('n', prefixtoadd) |
| 105 |
e.insert(0, new) |
| 106 |
parname = par.get("n") |
| 107 |
for prefix in prefixes.keys(): |
| 108 |
if parname.endswith("__"+prefix): |
| 109 |
new = ET.Element('par') |
| 110 |
prefixtoadd = prefixes[prefix] |
| 111 |
new.set('n', prefixtoadd) |
| 112 |
e.insert(0, new) |
| 113 |
|
| 114 |
tree.write(target) |