【问题标题】:Convert the word " I'm" to "I am" using python [closed]使用python将单词“I'm”转换为“I am”[关闭]
【发布时间】:2018-02-27 11:13:48
【问题描述】:

使用 python 将单词" I'm" 转换为"I am" 并类似地将"How're" 转换为"How are"。我为此使用了词形还原,但没有得到足够的结果。我使用了以下代码:

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)

【问题讨论】:

标签: python string nlp nltk


【解决方案1】:

我会使用更易读、更容易维护的东西:

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.

如果需要,您可以添加更多案例。

【讨论】:

  • 如果're -> were怎么办?
  • 如果's -> 's 代表所有物或's -> has 怎么办?
  • 如果'll -> shall怎么办?
  • 如果'd -> had怎么办?
  • 撇号的正则表达式,你可以试试这个
猜你喜欢
  • 1970-01-01
  • 2011-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多