У меня есть кое-что довольно сложное, но я написал это в спешке, и на данный момент он делает свою работу.
Обратите внимание, что:
Я добавил 'во Франции'after Между тем, это студийный альбом 7 британской поп-группы 10cc
, который изменен только British
' 1978 'в немецкой группой Genesis 8, выпущенной в 1978 году. не изменяется, тогда как '8' изменяется.
Вот почему это сложно.
Но я боюсь, что, несмотря на это осложнение, оно не будет точным для всех возможных предложений.
Необходимо усовершенствовать, чтобы idi всегда было правильным названием музыкальной группы, а не всегда первым, как это в настоящем решении.Но это тяжелая работа, не зная, что именно вы хотите
ss ='''british 7
German 8
France 90'''
text = '''<s id="69-7">...Meanwhile is the studio 7 album by British pop band 10cc in France.</s>
<s id="15-8">...And Then There Were Three... is the ninth studio album by the german band Genesis 8 and was released in 1978.</s>
<s id="1990-2">Magnum Nitro Express is a France centerfire fire rifle cartridge 90.</s>
'''
import re
regx = re.compile('^(.+?)[ \t]+(\d+)',re.MULTILINE)
dico = dict((a.lower(),b) for (a,b) in regx.findall(ss))
print 'dico==',dico
print '\n\n'
rogx = re.compile('(<s id="[\d-]+">|</s>\r?\n)')
splitted = rogx.split(text)
print 'splitted==\n',splitted
print '=================\n'
def repl(mat):
idi = (b for (a,b) in the if b).next().lower()
x,y = mat.groups()
if x:
if dico[idi.lower()]==x:
return '<w2>%s</w2>' % x
else:
return x
if y :
if y.lower()==idi:
return '<w1>%s</w1>' % y
else:
return y
rigx = re.compile('(\d+)|(' + '|'.join(dico.keys()) + ')',re.IGNORECASE)
for i,el in enumerate(splitted[0::2]):
if el:
print '-----------------------------'
print '* index in splitted==',2*i
print '\n* el==\n',repr(el)
print '\n* rigx.findall(el)==\n',rigx.findall(el)
the = rigx.findall(el)
print '\n* modified el:\n',rigx.sub(repl,el)
splitted[2*i] = rigx.sub(repl,el)
print '\n\n##################################\n\n'
print 'modified splitted==\n',splitted
print
print ''.join(splitted)
результат
dico== {'german': '8', 'british': '7', 'france': '90'}
splitted==
['', '<s id="69-7">', '...Meanwhile is the studio 7 album by British pop band 10cc in France.', '</s>\n', '', '<s id="15-8">', '...And Then There Were Three... is the ninth studio album by the german band Genesis 8 and was released in 1978.', '</s>\n', '', '<s id="1990-2">', 'Magnum Nitro Express is a France centerfire fire rifle cartridge 90.', '</s>\n', '']
=================
-----------------------------
* index in splitted== 2
* el==
'...Meanwhile is the studio 7 album by British pop band 10cc in France.'
* rigx.findall(el)==
[('7', ''), ('', 'British'), ('10', ''), ('', 'France')]
* modified el:
...Meanwhile is the studio <w2>7</w2> album by <w1>British</w1> pop band 10cc in France.
-----------------------------
* index in splitted== 6
* el==
'...And Then There Were Three... is the ninth studio album by the german band Genesis 8 and was released in 1978.'
* rigx.findall(el)==
[('', 'german'), ('8', ''), ('1978', '')]
* modified el:
...And Then There Were Three... is the ninth studio album by the <w1>german</w1> band Genesis <w2>8</w2> and was released in 1978.
-----------------------------
* index in splitted== 10
* el==
'Magnum Nitro Express is a France centerfire fire rifle cartridge 90.'
* rigx.findall(el)==
[('', 'France'), ('90', '')]
* modified el:
Magnum Nitro Express is a <w1>France</w1> centerfire fire rifle cartridge <w2>90</w2>.
##################################
modified splitted==
['', '<s id="69-7">', '...Meanwhile is the studio <w2>7</w2> album by <w1>British</w1> pop band 10cc in France.', '</s>\n', '', '<s id="15-8">', '...And Then There Were Three... is the ninth studio album by the <w1>german</w1> band Genesis <w2>8</w2> and was released in 1978.', '</s>\n', '', '<s id="1990-2">', 'Magnum Nitro Express is a <w1>France</w1> centerfire fire rifle cartridge <w2>90</w2>.', '</s>\n', '']
<s id="69-7">...Meanwhile is the studio <w2>7</w2> album by <w1>British</w1> pop band 10cc in France.</s>
<s id="15-8">...And Then There Were Three... is the ninth studio album by the <w1>german</w1> band Genesis <w2>8</w2> and was released in 1978.</s>
<s id="1990-2">Magnum Nitro Express is a <w1>France</w1> centerfire fire rifle cartridge <w2>90</w2>.</s>
РЕДАКТИРОВАНИЕ 1
Я исключил replmodel ()
repl() принимает значение rigx.findall (el)
Я добавил строку the = rigx.findall (el) для этого