mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-29 08:34:37 +01:00
when reversing words, preserve capitalization (i.e. start don't end with a cap)
This commit is contained in:
parent
c06f0e157d
commit
2a3b8c7a74
1 changed files with 24 additions and 3 deletions
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
import sys, getopt, re
|
||||
import sys, getopt, re, os
|
||||
from lxml import etree
|
||||
|
||||
expr = '(' + \
|
||||
|
@ -17,6 +17,7 @@ expr = '(' + \
|
|||
')'
|
||||
|
||||
# Can't make these work...
|
||||
# r'[;:.]' , \
|
||||
# r'\(' , \
|
||||
# r'\)' , \
|
||||
|
||||
|
@ -35,10 +36,24 @@ def reverse( str ):
|
|||
for ii in range(len(split)):
|
||||
word = split[ii]
|
||||
if not re.match( FMT, word ):
|
||||
split[ii] = word[::-1]
|
||||
split[ii] = revcaps(word)
|
||||
result = ''.join(split)
|
||||
return result
|
||||
|
||||
def revcaps(word):
|
||||
newWord = list(word[::-1])
|
||||
nLetters = len(word)
|
||||
for indx in range( nLetters ):
|
||||
letter = newWord[indx]
|
||||
if word[indx].isupper():
|
||||
letter = letter.upper()
|
||||
else:
|
||||
letter = letter.lower()
|
||||
newWord[indx] = letter
|
||||
result = ''.join(newWord)
|
||||
# print 'revcaps(%s) => %s' % (word, result)
|
||||
return result
|
||||
|
||||
def usage():
|
||||
print "usage:", sys.argv[0], '-l ca_PS|ba_CK [-o outfile]'
|
||||
sys.exit(1)
|
||||
|
@ -78,7 +93,13 @@ def main():
|
|||
elem.text = func(text)
|
||||
|
||||
if '-' == outfile: out = sys.stdout
|
||||
else: out = open( outfile, "w" )
|
||||
else:
|
||||
dir = os.path.dirname( outfile )
|
||||
print 'making', dir
|
||||
try: os.makedirs( dir )
|
||||
except: pass
|
||||
|
||||
out = open( outfile, "w" )
|
||||
out.write( etree.tostring( doc, pretty_print=True, encoding="utf-8", xml_declaration=True ) )
|
||||
|
||||
##############################################################################
|
||||
|
|
Loading…
Add table
Reference in a new issue