usr bin python2 coding utf-8 import codecs from lxml import etree from

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python2
# coding: utf-8
import codecs
from lxml import etree
from copy import deepcopy
from random import choice
filename = "Beser_xml.xml"
letters = u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'
letters += u'абсгдеёжзийклмнопрстуфхцчшщъыьЭЮЯАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'
def morph_word(word):
l = []
for i in xrange(len(word)):
if word[i] in letters:
l.append(i)
if 0 in l:
l.remove(0)
if l:
for k in xrange(3):
i = choice(l)
j = choice(l)
if i > j: i, j = j, i
if i != j:
word = word[:i] + word[j] + word[i+1:j] + word[i] + word[j+1:]
return word
def morph_sentence(text):
new_text = ''
for word in text.split(' '):
new_text += morph_word(word) + ' '
#return new_text.strip()
return new_text[:-1]
f = codecs.open(filename, 'r', 'utf-8')
tree = etree.parse(f)
f.close()
document = tree.getroot()
Dictionary = document.find('Dictionary')
Language = Dictionary.find('Language')
for Lemma in Language.findall('Lemma'):
Lemma.set('LemmaSign', morph_sentence(Lemma.get('LemmaSign')))
for PSBlock in Lemma.findall('PSBlock'):
for Value in PSBlock.findall('value'):
if Value.get('ValTr'):
Value.set('ValTr', morph_sentence(Value.get('ValTr')))
f = codecs.open('new_%s'%(filename), 'w', 'utf-8')
f.write(etree.tostring(tree, encoding='unicode'))
f.close()