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
   | #! /usr/bin/env python3
 
import string
 
CHARACTERS_TO_KEEP = [x for x in string.ascii_uppercase] + \
                     [
# A
                      'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ',
# C
                      'Ç',
# E
                      'È', 'É', 'Ê', 'Ë',
# I
                      'Ì', 'Í', 'Î', 'Ï',
# N
                      'Ñ',
# O
                      'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '', 
# U
                      'Ù', 'Ú', 'Û', 'Ü',
# Y
                      'Ý', ''
                     ]
 
 
def cleanExotic(text):
    answer = ''
    for oneChar in text:
        if oneChar.upper() in CHARACTERS_TO_KEEP:
            answer += oneChar
        else:
            answer += ' '
    return answer
 
def containAllWord(text, words):
    return all( oneWord in cleanExotic(text).split() for oneWord in words )
 
def containAtLeastOneWord(text, words):
    return any( oneWord in cleanExotic(text).split() for oneWord in words  )
 
if __name__ == '__main__':
    tests = [
              ( """Ceci est une
                phrase de       test.""", ["est", "une", "de"] ),
              ( "Ceci est une autre phrase de test.", ["es", "une", "de"] ),
              ( "Des mots les unsdanslesautres.", ["unsd", "sda"] ),
              ( "Ceci est une autre phrase de test.", ["test"] ),
              ( "ponctuation = string.punctuation", ["string"]),
              ( "ponctuation=string.punctuation", ["string"])
           ]
 
    for oneTest in tests:
        print('')
 
        for oneFunction in [containAllWord, containAtLeastOneWord]:
            print( oneFunction.__name__ + '( """' + oneTest[0] + '""", ' + str(oneTest[1]) +' )',
                   '\t===> ' + str( oneFunction(oneTest[0], oneTest[1]) ),
                   sep = '\n' ) | 
Partager