使用 Python 扩展英语的缩略语
Expanding contractions in the English language using Python
我试图通过定义一个包含应该使用的替换的字典来替换推文中的所有缩写,但不明白为什么这不起作用:
tweet = "I luv <3 my iphone & you’re awsm apple. DisplayIsAwesome, sooo happppppy http://www.apple.com"
APPOSTOPHES = {"'s": " is", "'re":" are"}
sentence_list = tweet.split()
print(sentence_list)
new_sentence = []
for word in sentence_list:
for candidate_replacement in APPOSTOPHES:
if candidate_replacement in word:
word = word.replace(candidate_replacement, APPOSTOPHES[candidate_replacement])
new_sentence.append(word)
rfrm = " ".join(new_sentence)
print(rfrm)
我试过用最常见的缩略语来修改字典,但没用。
最后输出的句子和输入的完全一样
注意:在此之前,推文经过 html 解析,但我怀疑这会影响什么。
您的输入字符串 tweet
包含不可打印字符 ’
而不是单引号 '
。
在最简单的情况下,您可以将 APPOSTOPHES
字典扩展为以下内容:
...
APPOSTOPHES = {"'s": " is", "’s": " is", "'re":" are", "’re":" are"}
然后,你会得到预期的结果:
I luv <3 my iphone & you are awsm apple. DisplayIsAwesome, sooo happppppy http://www.apple.com
很简单。您在字典 APPOSTOPHES 中使用了错误的符号。
"’re" != "'re"
试试:
APPOSTOPHES = {"’s": " is", "’re": " are"}
我试图通过定义一个包含应该使用的替换的字典来替换推文中的所有缩写,但不明白为什么这不起作用:
tweet = "I luv <3 my iphone & you’re awsm apple. DisplayIsAwesome, sooo happppppy http://www.apple.com"
APPOSTOPHES = {"'s": " is", "'re":" are"}
sentence_list = tweet.split()
print(sentence_list)
new_sentence = []
for word in sentence_list:
for candidate_replacement in APPOSTOPHES:
if candidate_replacement in word:
word = word.replace(candidate_replacement, APPOSTOPHES[candidate_replacement])
new_sentence.append(word)
rfrm = " ".join(new_sentence)
print(rfrm)
我试过用最常见的缩略语来修改字典,但没用。
最后输出的句子和输入的完全一样
注意:在此之前,推文经过 html 解析,但我怀疑这会影响什么。
您的输入字符串 tweet
包含不可打印字符 ’
而不是单引号 '
。
在最简单的情况下,您可以将 APPOSTOPHES
字典扩展为以下内容:
...
APPOSTOPHES = {"'s": " is", "’s": " is", "'re":" are", "’re":" are"}
然后,你会得到预期的结果:
I luv <3 my iphone & you are awsm apple. DisplayIsAwesome, sooo happppppy http://www.apple.com
很简单。您在字典 APPOSTOPHES 中使用了错误的符号。
"’re" != "'re"
试试:
APPOSTOPHES = {"’s": " is", "’re": " are"}