-3

Convert the word " I'm" to "I am" and similarly "How're" to "How are" using python. I used lemmatization for this but did not get adequate results. I used the following code:

sen="You are great, My Lord. I'm studying with co-workers. How're you?" 
all_words=regexp_tokenize(sen, "[\w']+")
lemmatiser = WordNetLemmatizer()
all_words_lem=[]
for i in all_words:
x=lemmatiser.lemmatize(i, pos="v")
all_words_lem.append(x)
kiyah
  • 1,502
  • 2
  • 18
  • 27
Simar
  • 13
  • 2
  • 1
    Possible duplicate of [How to input a regex in string.replace?](https://stackoverflow.com/questions/5658369/how-to-input-a-regex-in-string-replace) – Nicolás Carrasco-Stevenson Feb 27 '18 at 11:38
  • In what way were your results inadequate? – halfer Mar 03 '18 at 14:23
  • For other people searching, you can find more complete answers to this question here (which is luckily still open, and had a chance for answers): https://stackoverflow.com/questions/43018030/replace-apostrophe-short-words-in-python – Hello Lili May 29 '22 at 12:11

1 Answers1

2

I would use something that is more readable and easier to maintain:

import re

def decontracted(phrase):
    # specific
    phrase = re.sub(r"won't", "will not", phrase)
    phrase = re.sub(r"can\'t", "can not", phrase)

    # general
    phrase = re.sub(r"n\'t", " not", phrase)
    phrase = re.sub(r"\'re", " are", phrase)
    phrase = re.sub(r"\'s", " is", phrase)
    phrase = re.sub(r"\'d", " would", phrase)
    phrase = re.sub(r"\'ll", " will", phrase)
    phrase = re.sub(r"\'t", " not", phrase)
    phrase = re.sub(r"\'ve", " have", phrase)
    phrase = re.sub(r"\'m", " am", phrase)
    return phrase


test = "Hey I'm Yann, how're you and how's it going ? That's 
interesting: I'd love to hear more about it."
print(decontracted(test))
# Hey I am Yann, how are you and how is it going ? That is interesting: I would love to hear more about it.

You can add more cases if you need.

ankur09011
  • 453
  • 3
  • 12