This is the follow up for a question you can find here: replacing words with their abbreviations
The goal here is to compare two petential way of answering said question that was:
This particular function's goal is to replace words with their abbreviations by compairing each word with all of those in the config file and replacing whenever a match is found, the goal is to keep a general idea of what the string meant so its not a problem that a word once replaced could ressemble an other as long as they refer to roughly the same thing.
String is always upper case and free of special characters.
The two functions are:
From @Maarten Fabré:
def shorten_words(abbreviations, line, max_length=38): replacements = set() while len(line) > max_length: for word in line.split(" "): if ( word[-1] == "S" and word not in abbreviations and word[:-1] in abbreviations ): word = word[:-1] if word not in replacements and word in abbreviations: line = line.replace(word, abbreviations[word]) if word == abbreviations[word]: replacements.add(word) break return line
def shorten_words(abbreviations_file, sentence): """Shorten string `sentence` using the dictionnary_like object `abbreviations`.""" abbreviations = set(abbreviations_file) word_list = sentence.split(' ') size = len(sentence) resultat =  for word in word_list: if word[-1] == "S" and word not in abbreviations and word[:-1] in abbreviations: word = word[:-1] if word.lower() not in abbreviations or size <= POSTAL_LINE_LENGTH: resultat.append(word) else: resultat.append(abbreviations_file[word]) size -= len(word) - len(abbreviations_file[word]) return ' '.join(resultat)
The two main point of comparison are performance and readablility, those answer may still be reviewed as usual.
here is a sample of the configfile (some lines have key and value equal this is meant to remove plural):
[abbreviation] AVANCEE = AVANC COMPOSANT = COMPO VERT = VERT AGRAIRE = AGRAIR MECANIQUE = MECA CARROSSERIE = CARROS SIGNALISATION = SIGNAL FOURNITURE = FOURNI LAITIERE = LAIT INTERPROFESSIONNEL = INTRPRO ATLANTIQUE = ATLAN REALISATION = REAL INCENDIE = INCEND MARBRERIE = MARB FUNEBRE = FUNEBR POMPE = POMPE ANTICIPATION = ANTICIP OBJET = OBJET ANTIQUITE = ANTIQ MOBILITE = MOBIL ASSOCIATIF = ASSO ANCIENNE = ANC TELECOMMUNICATION = TELECOM RESEAUX = RESEAU LOCALE = LOCAL RESPIRE = RESPI QUAND = QND CHRETIENNE = CHRET OUVRIERE = OUVRI JEUNESSE = JEUNE INTERCULTUREL = INTRCULT VALORISATION = VALOR ALIMENTAIRE = ALIMEN COMMUNALE = COMMUNE LAIQUE = LAIQ CASSATION = CASS TRAVAUX = TRAVAU ONCOLOGIE = ONCO RELIGION = RELIG PLURALISME = PLURAL FLOTTANTE = FLOT EOLIENNE = EOLIEN HUMAINE = HUMAIN POTENTIEL = POTENT AMELIORATION = AMELIO MUSIQUE = MUSIQ MUNICIPALE = MUNI EVANGELIQUE = EVANG BIOLOGISTE = BIOLOG REPUBLICAIN = REPU SYMPATHISANT = SYMPAT ELU = ELU INTERCONNEXION = INTRCONN CONSULTANT = CONSULT ORGANIZATION = ORGA OLYMPIQUE = OLYMP CAPACITE = CAPA RENFORCEMENT = RENFOR CLEF = CLEF FRIGORIFIQUE = FRIGO ENTREPOSAGE = ENTREPO COLLABORATIF = COLLAB TROUBLE = TROUBL ENTRAIDE = ENTRAID REPRESENTANT = REPRESENT ADHERENT = ADHER FOLKLORIQUE = FOLKLO STADE = STAD AMI = AMI EMPEREURS = EMPER CONFRERIE = CONFRER SOUTENUE = SOUTENU LISTE = LIST ELECTION = ELECT ELECTORALE = ELECT FINANCEMENT = FINANC CATHOLIQUE = CATHO HARMONIE = HARMO DEBOUT = DEBOU VENT = VENT CERCLE = CERCL FOOTBALL = FOOT IMPROVISATION = IMPROV POPULAIRE = POPU SECOURS = SECOUR ART = ART DRAMATURGIE = DRAMA POETIQUE = POET TRAVAILLANT = TRAVAIL SYNCHRONISEE = SYNCHRO NATATION = NATA LOCATAIRES = LOCAT AMICALE = AMICA DEPARTEMENT = DEPART INDISCIPLINEE = INDISCIPL PARTAGE = PARTA MEDIATION = MEDIAT CITOYEN = CITOY CULTIVONS = CULTIV QUARTIER = QUART DOMICILE = DOMI ADMINIS = ADMIN APPLIQUEE = APPLI SOPHROLOGIE = SOPHRO SPECTACLE = SPECTA ABANDONNE = ABANDON COMMUNAUTAIRE = COMMUN PARTICULIER = PARTICUL METALLIQUE = METAL COOPERATION = COOP PROGRAMMATION = PROGRAM KINESITHERAPEUTE = KINESITHERAP ENVIRON = ENVIRON ARTISAN = ARTIS COMMUNICATION = COM TRANSMISSION = TRANSMIS APPROVISIONNEMENT = APPRO IMAGERIE = IMAGE MANAGEMENT = MANAG ASSOCIEE = ASSO INFIRMIERE = INFIRM FONDS = FOND EMBOUTISSAGE = EMBOUTISS DECOUPAGE = DECOUP OUTILLAGE = OUTIL TERRASSEMENT = TERRASS DEMOLITION = DEMOLIT BILINGUE = BILINGU ECOLE = ECOL HABITAT = HABITA PRODUCTION = PROD DURABLE = DURABL PRATIQUE = PRATIQ TRANSPORT = TRANSPOR ASSOCIATIVE = ASSO CRECHE = CRECH SPECIALISEE = SPECIAL COUVERTURE = COUVERT ETANCHEITE = ETANCH TOITURE = TOIT