扩展Python中的英语语言收缩

Maa*_*ten 35 python text-processing nlp

英语有几个收缩.例如:

you've -> you have
he's -> he is
Run Code Online (Sandbox Code Playgroud)

当您进行自然语言处理时,这些有时会引起头痛.是否有一个Python库,可以扩展这些收缩?

art*_*omp 49

我把维基百科收缩到扩展页面变成了python字典(见下文)

请注意,正如您所料,在查询字典时,您肯定要使用双引号:

在此输入图像描述

另外,我在维基百科页面中留下了多个选项.随意修改它你想要的.请注意,消除对正确扩展的歧义将是一个棘手的问题!

contractions = { 
"ain't": "am not / are not / is not / has not / have not",
"aren't": "are not / am not",
"can't": "cannot",
"can't've": "cannot have",
"'cause": "because",
"could've": "could have",
"couldn't": "could not",
"couldn't've": "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 had / he would",
"he'd've": "he would have",
"he'll": "he shall / he will",
"he'll've": "he shall have / he will have",
"he's": "he has / he is",
"how'd": "how did",
"how'd'y": "how do you",
"how'll": "how will",
"how's": "how has / how is / how does",
"I'd": "I had / I would",
"I'd've": "I would have",
"I'll": "I shall / I will",
"I'll've": "I shall have / I will have",
"I'm": "I am",
"I've": "I have",
"isn't": "is not",
"it'd": "it had / it would",
"it'd've": "it would have",
"it'll": "it shall / it will",
"it'll've": "it shall have / it will have",
"it's": "it has / it is",
"let's": "let us",
"ma'am": "madam",
"mayn't": "may not",
"might've": "might have",
"mightn't": "might not",
"mightn't've": "might not have",
"must've": "must have",
"mustn't": "must not",
"mustn'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",
"shan't've": "shall not have",
"she'd": "she had / she would",
"she'd've": "she would have",
"she'll": "she shall / she will",
"she'll've": "she shall have / she will have",
"she's": "she has / she is",
"should've": "should have",
"shouldn't": "should not",
"shouldn't've": "should not have",
"so've": "so have",
"so's": "so as / so is",
"that'd": "that would / that had",
"that'd've": "that would have",
"that's": "that has / that is",
"there'd": "there had / there would",
"there'd've": "there would have",
"there's": "there has / there is",
"they'd": "they had / they would",
"they'd've": "they would have",
"they'll": "they shall / they will",
"they'll've": "they shall have / they will have",
"they're": "they are",
"they've": "they have",
"to've": "to have",
"wasn't": "was not",
"we'd": "we had / we would",
"we'd've": "we would have",
"we'll": "we will",
"we'll've": "we will have",
"we're": "we are",
"we've": "we have",
"weren't": "were not",
"what'll": "what shall / what will",
"what'll've": "what shall have / what will have",
"what're": "what are",
"what's": "what has / what is",
"what've": "what have",
"when's": "when has / when is",
"when've": "when have",
"where'd": "where did",
"where's": "where has / where is",
"where've": "where have",
"who'll": "who shall / who will",
"who'll've": "who shall have / who will have",
"who's": "who has / who is",
"who've": "who have",
"why's": "why has / why is",
"why've": "why have",
"will've": "will have",
"won't": "will not",
"won't've": "will not have",
"would've": "would have",
"wouldn't": "would not",
"wouldn't've": "would not have",
"y'all": "you all",
"y'all'd": "you all would",
"y'all'd've": "you all would have",
"y'all're": "you all are",
"y'all've": "you all have",
"you'd": "you had / you would",
"you'd've": "you would have",
"you'll": "you shall / you will",
"you'll've": "you shall have / you will have",
"you're": "you are",
"you've": "you have"
}
Run Code Online (Sandbox Code Playgroud)

  • 如何消除右边的歧义?我可以得到这个棘手问题的答案吗? (3认同)

alk*_*lko 17

您不需要库,例如可以使用reg exp.

>>> import re
>>> contractions_dict = {
...     'didn\'t': 'did not',
...     'don\'t': 'do not',
... }
>>> contractions_re = re.compile('(%s)' % '|'.join(contractions_dict.keys()))
>>> def expand_contractions(s, contractions_dict=contractions_dict):
...     def replace(match):
...         return contractions_dict[match.group(0)]
...     return contractions_re.sub(replace, s)
...
>>> expand_contractions('You don\'t need a library')
'You do not need a library'
Run Code Online (Sandbox Code Playgroud)

  • 这是一个好的开始,但我想有一些优势:"杰克是一个很好的游泳运动员"对比"杰克的房子很好.". (3认同)
  • @Maarten消除那些和其他情况歧义的工具将不是一个库,而是一个至少包含一个体面的PoS标记器和一个高级nlp模型的解决方案,例如[paraller corpora approach here](http:// www .zora.uzh.ch/47923/4/Volk_Sennrich_Contraction_ResolutionV.pdf),或 (3认同)

Yan*_*ois 9

上面的答案将非常有效,并且对于模糊的收缩可能更好(尽管我认为没有那么多模棱两可的案例).我会使用更易读和易于维护的东西:

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.
Run Code Online (Sandbox Code Playgroud)

它可能有一些我没想到的缺陷.

转自我的其他答案

  • 另一个缺陷:“这是艾米的房子”->“这是艾米的房子” (2认同)

Joe*_*008 7

这是一个非常酷且易于使用的库,用于 https://pypi.python.org/pypi/pycontractions/1.0.1

使用示例(链接中有详细信息):

from pycontractions import Contractions

# Load your favorite word2vec model
cont = Contractions('GoogleNews-vectors-negative300.bin')

# optional, prevents loading on first expand_texts call
cont.load_models()

out = list(cont.expand_texts(["I'd like to know how I'd done that!",
                            "We're going to the zoo and I don't think I'll be home for dinner.",
                            "Theyre going to the zoo and she'll be home for dinner."], precise=True))
print(out)
Run Code Online (Sandbox Code Playgroud)

您还需要GoogleNews-vectors-negative300.bin链接,以在上面的pycontractions链接中进行下载。* python3中的示例代码。

  • 是的,只要您可以安装其依赖项之一(语言检查)... (2认同)

Ham*_*san 6

我已经为此找到了一个库,contractions它非常简单。

import contractions
print(contractions.fix("you've"))
print(contractions.fix("he's"))
Run Code Online (Sandbox Code Playgroud)

输出:

you have
he is
Run Code Online (Sandbox Code Playgroud)

  • 值得注意的是,这个库不能很好地处理某些特殊字符,请参阅:https://github.com/kootenpv/contractions/issues/25 (3认同)