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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
| #s!/usr/bin/env python
# This code only works with Python 3.
# CODE TO CLEAN AND SIMPLIFY !!!!!!
import re # For cleaning the spaces between digits.
import difflib # For finding closest number names to a mistaped word.
import config_fr
# We don't keep 70, 80 and 90 because we are going to transform "soixante dix" to "70"...
# We need to do that so have to be more flexible.
SPECIAL_NAMES_NUMBERS = dict([ (val, key) for (key, val) in config_fr.SPECIAL_NUMBERS_NAMES.items() \
if key not in ['80', '90', '70'] ])
NAMES_TEN_POWERS = {}
THE_BIGGEST_NUMBER = {}
THE_TEN_POWERS = {}
for oneConvention in config_fr.TEN_POWERS_NAMES:
NAMES_TEN_POWERS[oneConvention] = {}
THE_TEN_POWERS[oneConvention] = ['100']
for (key, val) in config_fr.TEN_POWERS_NAMES[oneConvention].items():
NAMES_TEN_POWERS[oneConvention][val] = '1' + '0'*key
THE_TEN_POWERS[oneConvention].append('1' + '0'*key)
# We have to sort the powers from the smaller to the bigger.
THE_TEN_POWERS[oneConvention] = sorted( THE_TEN_POWERS[oneConvention],
key=(lambda x: len(x)) )
THE_BIGGEST_NUMBER[oneConvention] = THE_TEN_POWERS[oneConvention][-1]
# We must import integerToWords_fr after config_fr because
# SPECIAL_NUMBERS_NAMES is changed in integerToWords_fr.
import integerToWords_fr # We need this to indicate errors...
###############
# THE FUNCTIONS
def wellFormattedTextWithDigits(text):
# Source : http://www.developpez.net/forums/d867083/autres-langages/python-zope/general-python/nettoyer-espaces-regex
return list( map(lambda x: x.replace(' ',''),re.findall('(\d[\d ]*|[^ \d-]+)',text)) )
def printer(text, allowMistype = False, allowNumber = False, convention = "everyday"):
"""
If sympaMode = True, then we try to find the closest word possible
when mistyping are done. For example 'katr" will be seen as "quatre".
**NOTE :** this functionnality is the work of the module 'difflib'.
"""
if convention not in NAMES_TEN_POWERS:
raise ValueError('convention = "' + str(convention) + '" is unknown.')
text = text.lower()
if allowNumber:
# "3456 7890" must become "34567890".
theWords = wellFormattedTextWithDigits(text)
else:
theWords = text.replace('-', ' ').split()
# We verify that the names used are allowed.
names_ten_powers = NAMES_TEN_POWERS[convention]
for index, oneWord in enumerate(theWords):
# "cents", "millions",... must be seen as "cent", "million",...
#
# WARNING ! "trois" must stay "trois"
if oneWord[-1] == 's' and oneWord != 'trois':
oneWord = oneWord[:-1]
weCleanAnS = True # Only for error detected.
else:
weCleanAnS = False
# WARNING ! "trente et un milliards de milliards"
# "deux millions de milliards"
if oneWord in ['et', 'de']:
pass
# We convert each word to digit numbers.
elif oneWord in SPECIAL_NAMES_NUMBERS:
theWords[index] = SPECIAL_NAMES_NUMBERS[oneWord]
elif oneWord in names_ten_powers:
theWords[index] = names_ten_powers[oneWord]
else:
integerCleaned = integerToWords_fr.cleanInteger(oneWord)
if integerCleaned:
if allowNumber:
theWords[index] = integerCleaned
else:
raise ValueError( 'The integer "{0}" must be written using words !.'.format(str(oneWord)) )
elif allowMistype:
# 'difflib' does all the job !
nearestWords = difflib.get_close_matches(oneWord, SPECIAL_NAMES_NUMBERS)
if len(nearestWords) == 1:
theWords[index] = SPECIAL_NAMES_NUMBERS[nearestWords[0]]
else:
if weCleanAnS:
oneWord += 's'
raise ValueError( '"{0}" does not look closer to a name numbers with the "{1}" convention.'.format(str(oneWord), convention) )
else:
if weCleanAnS:
oneWord += 's'
raise ValueError( '"{0}" is not used to name numbers with the "{1}" convention.'.format(str(oneWord), convention) )
# For the moment we have ['4', '20'] instead of '80'.
# We have a similar problem with '90'.
#
# We have to treat that...
i = 0
lookFor = '4' in theWords
while lookFor:
i = theWords.index('4', i)
if i < len(theWords) - 1:
if theWords[i + 1] == '20':
theWords.pop(i + 1)
if i == len(theWords) - 1:
theWords[i] = '80'
elif theWords[i + 1] == '10':
theWords.pop(i + 1)
theWords[i] = '90'
else:
theWords[i] = '80'
try:
i = theWords.index('4', i + 1)
lookFor = True
except:
lookFor = False
# We look for a good use of the word "et".
while('et' in theWords):
i = theWords.index('et')
if i == 0:
raise ValueError('"et" can not be at the beggining of the name of the number.')
if i == len(theWords) - 1:
raise ValueError('"et" can not be at the end of the name of the number.')
beforeET = theWords[i-1]
afterET = theWords[i+1]
oneError = False
if afterET == '1':
if not beforeET in ['20', '30', '40', '50', '60', '80']:
oneError = True
else:
theWords[i-1] = theWords[i-1][0] + '1'
elif afterET == '11':
if not beforeET == '60':
oneError = True
else:
theWords[i-1] = str( int(theWords[i-1][0]) + 1 ) + '1'
else:
oneError = True
if oneError:
beforeET = integerToWords_fr.printer( number = beforeET,
convention = convention )
afterET = integerToWords_fr.printer( number = afterET,
convention = convention )
raise ValueError( '"{0}" can not be used to name numbers with the "{1}" convention.'.format(str(beforeET +'-et-' + afterET), convention) )
theWords.pop(i)
theWords.pop(i)
# We look for a good use of the word "de".
the_biggest_number = THE_BIGGEST_NUMBER[convention]
the_ten_powers = THE_TEN_POWERS[convention]
while('de' in theWords):
i = theWords.index('de')
if i == 0:
raise ValueError('"de" can not be at the beggining of the name of the number.')
if i == len(theWords) - 1:
raise ValueError('"de" can not be at the end of the name of the number.')
beforeDE = theWords[i - 1]
afterDE = theWords[i + 1]
if (beforeDE not in the_ten_powers and beforeDE != '*') or afterDE != the_biggest_number:
beforeDE = integerToWords_fr.printer( number = beforeDE,
convention = convention )
afterDE = integerToWords_fr.printer( number = afterDE,
convention = convention )
raise ValueError( '"{0}" can not be used to name numbers with the "{1}" convention.'.format(str(beforeDE +' de ' + afterDE.replace('un ', '')), convention) )
# '*' means '.... * 10^max * ...'
if beforeDE == the_biggest_number:
theWords[i] = '*'
theWords.pop(i + 1)
else:
theWords.pop(i)
# We build the two digits numbers like "30-5 == > 35", "80-15 == > 95"...
i = 0
while i < len(theWords):
addOne = True
# ERRORS TO FIND : "4-30" or "5-6" !!!!
if len(theWords[i]) <= 2 and i > 0 and theWords[i] != '*':
if len(theWords[i - 1]) == 1 and theWords[i-1] != '*':
theWords[i - 1] = integerToWords_fr.printer( number = theWords[i - 1],
convention = convention )
theWords[i] = integerToWords_fr.printer( number = theWords[i],
convention = convention )
raise ValueError( '"{0}" can not be used to name numbers with the "{1}" convention.'.format(str(theWords[i - 1] + ' ' + theWords[i]), convention) )
if len(theWords[i]) == 2:
if i < len(theWords) - 1:
if theWords[i + 1] != '*':
# "30-5 == > 35"
if len(theWords[i + 1]) == 1:
if theWords[i][1] != '0':
theWords[i] = integerToWords_fr.printer( number = theWords[i],
convention = convention )
theWords[i + 1] = integerToWords_fr.printer( number = theWords[i+1],
convention = convention )
raise ValueError( '"{0}" can not be used to name numbers with the "{1}" convention.'.format(str(theWords[i] + ' ' + theWords[i + 1]), convention) )
theWords[i] = theWords[i][0] + theWords[i + 1]
theWords.pop(i + 1)
addOne = False
# "80-15 == > 95"
elif len(theWords[i + 1]) == 2:
if theWords[i] not in ['60', '80'] or theWords[i + 1][0] != '1':
theWords[i] = integerToWords_fr.printer( number = theWords[i],
convention = convention )
theWords[i + 1] = integerToWords_fr.printer( number = theWords[i+1],
convention = convention )
raise ValueError( '"{0}" can not be used to name numbers with the "{1}" convention.'.format(str(theWords[i] + ' ' + theWords[i + 1]), convention) )
theWords[i] = str(int(theWords[i][0]) + 1) + theWords[i + 1][1]
theWords.pop(i + 1)
addOne = False
# ERRORS TO FIND : "30-100" !!!!
elif theWords[i + 1] == '100':
theWords[i] = integerToWords_fr.printer( number = theWords[i],
convention = convention )
theWords[i + 1] = integerToWords_fr.printer( number = theWords[i+1],
convention = convention )
raise ValueError( '"{0}" can not be used to name numbers with the "{1}" convention.'.format(str(theWords[i] + ' ' + theWords[i + 1]), convention) )
if addOne:
i += 1
# We build the mutiples of 100, 1 000, 1 000 000 and 10^p for p > 6 .
#
# Suppose that we have theWords equal to
# ['100', '34', '1000', '7', '100', '95', '1000000000', '*']
# The following procedures will produce first
# ['134', '1000', '795', '1000000000', '*']
# and then
# ['134 795', '1000000000', '*']
# We do the analyse from the smaller power of ten to the bigger one.
#
# The treatment of [..., '*'] will be done after.
for currentTenPower in the_ten_powers:
# The forbidden writing "trente cents" have been treated before.
nbOfDigitsAllowed = len(currentTenPower) - 1
# SPECIAL RULES ===> ?00 "un cent" NO !
# ??? 000 "un mille" NO !
# ??? 000 000 "un million" YES !
# The same is true for the other "big" powers of ten.
if currentTenPower in ['100', '1000']:
oneAllowed = False
else:
oneAllowed = True
i = 0
while i < len(theWords):
addOne = True
if theWords[i] == currentTenPower:
if i < len(theWords) - 1:
if theWords[i + 1][0] != '*':
# "?00-45 == > ?45"
if len(theWords[i + 1]) <= nbOfDigitsAllowed:
theWords[i + 1] = '0'*(nbOfDigitsAllowed - len(theWords[i + 1])) + theWords[i + 1]
theWords[i] = theWords[i][0] + theWords[i + 1][-nbOfDigitsAllowed:]
theWords.pop(i + 1)
addOne = False
# "5-1?? == > 5??"
if i > 0:
if theWords[i - 1][0] != '*':
if len(theWords[i - 1]) <= nbOfDigitsAllowed:
theWords[i - 1] = theWords[i - 1] + theWords[i][1:]
theWords.pop(i)
addOne = False
if addOne:
i += 1
# A single word ?
if len(theWords) == 1:
return theWords[0]
# ERRORS TO FIND : "3456-1000" , "3456-6789" ,...
for i in range(len(theWords)-1):
if theWords[i + 1] != '*' \
and theWords[i + 1] != '*' \
and len(theWords[i]) >= len(theWords[i + 1]):
theWords[i] = integerToWords_fr.printer( number = theWords[i],
convention = convention )
theWords[i + 1] = integerToWords_fr.printer( number = theWords[i+1],
convention = convention )
raise ValueError( '"{0}" can not be used to name numbers with the "{1}" convention.'.format(str(theWords[i] + ' ' + theWords[i + 1]), convention) )
# To finish we have to treat things like "... milliars de milliards..."
#
# '*' means '.... * 10^max * ...'
# For example, we have :
# "un milliard de milliards"
# ---> ['1000000000', '*']
# "deux milliards de milliards deux cents"
# ---> ['2000000000', '*', '200']
answer = ''
maxiNbOfZeros = len(the_biggest_number) - 1
i = len(theWords) - 1
while i >= 0:
if theWords[i] == '*':
nbOfDE = 2
i -= 1
while theWords[i] == '*':
nbOfDE += 1
i -= 1
# "deux cent trois milliards de milliards quatre-vingt"
# ---> ['203000000000', '*', '80']
theWords[i] = theWords[i][:-maxiNbOfZeros]
# We have then : ['203', '000000080']
if len(answer) > maxiNbOfZeros*nbOfDE:
answer = integerToWords_fr.printer( number = answer,
convention = convention )
the_biggest_number = integerToWords_fr.printer( number = the_biggest_number,
convention = convention ).replace('un', '')
raise ValueError( 'You misuse "... {0}" before "{1}".'.format('de' + the_biggest_number, answer) )
answer = '0'*(maxiNbOfZeros*nbOfDE - len(answer)) + answer
else:
answer = theWords[i] + answer
i -= 1
return answer
###############
###############
## ##
## FOR TESTS ##
## ##
###############
###############
if __name__ == '__main__':
myConvention ="everyday"
# myConvention ="rowlett"
# myConvention ="chuquet"
weAllowMistype = False
weAllowMistype = True
weAllowNumber = False
weAllowNumber = True
randomTest = True
randomTest = False
nMin = 0
nMax = 10**50
nbOfTest = 100
test = [
'quatre six',
'quatre cinquante',
'3456 mille',
'mille 7890',
'3456 sept mille 890',
'3456 7890',
'sans douse',
'troiiii cen douse',
'cinquante six',
'30et un mille quatre cents quatre-vingt-onze millions soixante-et-onze',
'deux 100 mille trois',
'trois millions de milliards',
'trente-quatre mille sept cents quatre-vingt-quinze milliards de milliards deux cents milliards',
'sept milliards de milliards de milliards deux cents milliards',
'sept milliards de milliards de milliards '
]
if randomTest:
import random
nMax += 1
for x in range(nbOfTest):
print()
oneNumber = str( random.randint(nMin, nMax) )
print( 'Test No {0} : {1}'.format(str(x+1), oneNumber) )
words = integerToWords_fr.printer( number = oneNumber,
convention = myConvention )
print(words)
print()
if oneNumber != printer( text = words,
allowMistype = weAllowMistype,
allowNumber = weAllowNumber,
convention = myConvention ):
print( printer( text = words,
allowMistype = weAllowMistype,
allowNumber = weAllowNumber,
convention = myConvention ) )
raise Exception('PB WITH : ' + '\n\t' + oneNumber + '\n\t' + printer(words) + '\n\t' + words)
print()
print('END OF THE RANDOM TESTS.')
else:
for oneNumber in test:
print()
print(str(oneNumber))
try:
print('\t' + printer( text = oneNumber,
allowMistype = weAllowMistype,
allowNumber = weAllowNumber,
convention = myConvention ))
except:
print('\tUNCORRECT NUMBER !') |
Partager