Python Contraction Function

Question:

(I already know there is a library that handles contractions and am able to use it)

BUT….

As a beginner I am trying to also learn how to program in Python, reading text etc. Here I am trying to do is remove "" and expand contractions. The removal of the slashes is working just fine. It is the expanding of contractions I am not having luck with. See my code below

contractions = {
"ain't" :"am not",
"aren't": "are not",
"can't": "cannot have",
"'cause": "because",
"could've": "could have",
"couldn't": "could not have",
"didn't": "did not",
"doesn't": "does not",
"don't": "do not",
"hadn't": "had not",
"hadn't've": "had not have",
"hasn't": "has not",    
"haven't": "have not",
"he'd": "he would",
"he'd've": "he would have",
"he'll": "he will",
"he'll've": "he will have",
"he's": "he is",
"how'd": "how did",
"how'd'y": "how do you",
"how'll": "how will",
"how's": "how does",
"i'd": "i would",
"i'd've": "i would have",
"i'll": "i will",
"i'll've": "i will have",
"i'm": "i am",
"i've": "i have",
"isn't": "is not",
"it'd": "it would",
"it'd've": "it would have",
"it'll": "it will",
"it'll've": "it will have",
"it's": "it is",
"let's": "let us",
"ma'am": "madam",
"mayn't": "may not",
"might've": "might have",
"mightn't": "might not",
"must've": "must have",
"mustn't": "must not",
"musn't've": "must not have",
"needn't": "need not",
"needn't've": "need not have",
"o'clock": "of the clock",
"oughtn't": "ought not",
"oughtn't've": "ought not have",
"shan't": "shall not",
"sha'n't": "shall not have",
"she'd": "she would",
"she'd've": "she would have",
"she'll": "she will",
"she'll've": "she will have",
"she's": "she is",
"should've": "should have",
"shouldn't": "should not",
"shouldn't've": "should not have",
"so've": "so have",
"so's": "so is",
"that'd": "that would",
"that'd've": "that would have",
"that's": "that is",
"there'd": "there would",
"they'd":  "they would",
"they'd've": "they would have",
"they'll": "they will",    
"they'll've": "they will have",
"they're": "they are",
"they've": "they have",
"to've": "to have",
"wasn't": "was not",
" u ": "you",
" ur ": "your",
" n ": " and "
}
print(contractions)

def cont_to_exp(x):
    if type(x) is str:
        x= x.replace('\','')
        for key in contractions:
            value = contractions[key]
            x= x.replace(key,value)
            return x
        else:
            return x
    x="don't"

    print(cont_to_exp(x))    

Appreciate any suggestions.

Asked By: ACE

||

Answers:

You must let the whole loop run before returning.

print(contractions)

def cont_to_exp(x):
    if type(x) is str:
        x= x.replace('\','')
        for key,value in contractions.items():
            x = x.replace(key,value)
        return x
x="don't"
print(cont_to_exp(x)) 
Answered By: Tim Roberts

for key in contractions – this is not how you use a dictionary object, in general. Personally, i’d rectify the needless replace statements in the loop, since that can get expensive presumably when the string is overly long.

Though honestly, I’m not sure that you need a loop either – so maybe something like this.

def cont_to_exp(x):
    assert type(x) is str, f"help me, I got a {type(x)}, wanted `str`"
    x= x.replace('\','')
    for word in x.split():
        if word in contractions:
            x = x.replace(word, contractions[word])

    return x

x="don't hurt me please, haven't you any mercy?"
print(cont_to_exp(x))  # do not hurt me please, have not you any mercy?
Answered By: rv.kvetch
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.