복붙노트

[PYTHON] 파이썬에서 영어 수축 확장하기

PYTHON

파이썬에서 영어 수축 확장하기

영어에는 몇 가지 수축이 있습니다. 예를 들면 :

you've -> you have
he's -> he is

자연 언어 처리를 할 때 이러한 두통이 때때로 발생할 수 있습니다. 이러한 수축을 확장 할 수있는 Python 라이브러리가 있습니까?

해결법

  1. ==============================

    1.위키 피 디아 축소 / 축소 페이지를 파이썬 사전으로 만들었습니다 (아래 참조)

    위키 피 디아 축소 / 축소 페이지를 파이썬 사전으로 만들었습니다 (아래 참조)

    예상 할 수 있듯이 사전을 쿼리 할 때 반드시 큰 따옴표를 사용하고 싶습니다.

    또한 위키피디아 페이지에서와 같이 여러 옵션을 남겼습니다. 원하는대로 자유롭게 수정하십시오. 올바른 확장에 대한 모호성 제거는 까다로운 문제입니다.

    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"
    }
    
  2. ==============================

    2.당신은 라이브러리가 필요하지 않습니다, 예를 들어 regex로 할 수 있습니다.

    당신은 라이브러리가 필요하지 않습니다, 예를 들어 regex로 할 수 있습니다.

    >>> 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'
    
  3. ==============================

    3.위의 대답은 완벽하게 잘 작동하고 모호한 수축에 대해 더 나을 수 있습니다 (모호한 사례가별로 없다고 주장 하겠지만). 나는 더 읽기 쉽고 유지하기 쉬운 것을 사용할 것입니다.

    위의 대답은 완벽하게 잘 작동하고 모호한 수축에 대해 더 나을 수 있습니다 (모호한 사례가별로 없다고 주장 하겠지만). 나는 더 읽기 쉽고 유지하기 쉬운 것을 사용할 것입니다.

    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.
    

    그것은 내가 생각하지 않은 몇 가지 결점을 가지고 있을지도 모른다.

    내 다른 대답에서 재 게시

  4. ==============================

    4.나는 alko의 대답에 약간을 더하고 싶습니다. 위키 피디 어를 확인하면 언급 된 영어 수축의 수는 100 개 미만입니다. 실제 시나리오에서이 수는 그보다 많을 수 있습니다. 그러나 아직도, 나는 200-300 단어가 모두 영어 수축 단어에 대한 것임을 확신합니다. 자, 당신은 그것들을 위해 별도의 라이브러리를 갖고 싶습니까 (나는 당신이 실제로 찾고있는 것을 생각하지 않는다)?. 그러나, 당신은 쉽게 사전과 정규식을 사용하여이 문제를 해결할 수 있습니다. Native Language Toolkit으로 좋은 토크 나이저를 사용하는 것이 좋습니다. 나머지는 스스로 구현하는 데 아무런 문제가 없어야합니다.

    나는 alko의 대답에 약간을 더하고 싶습니다. 위키 피디 어를 확인하면 언급 된 영어 수축의 수는 100 개 미만입니다. 실제 시나리오에서이 수는 그보다 많을 수 있습니다. 그러나 아직도, 나는 200-300 단어가 모두 영어 수축 단어에 대한 것임을 확신합니다. 자, 당신은 그것들을 위해 별도의 라이브러리를 갖고 싶습니까 (나는 당신이 실제로 찾고있는 것을 생각하지 않는다)?. 그러나, 당신은 쉽게 사전과 정규식을 사용하여이 문제를 해결할 수 있습니다. Native Language Toolkit으로 좋은 토크 나이저를 사용하는 것이 좋습니다. 나머지는 스스로 구현하는 데 아무런 문제가 없어야합니다.

  5. ==============================

    5.이것은 목적으로 사용하기에 매우 시원하고 사용하기 쉬운 라이브러리입니다. https://pypi.python.org/pypi/pycontractions/1.0.1.

    이것은 목적으로 사용하기에 매우 시원하고 사용하기 쉬운 라이브러리입니다. 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)
    

    위의 pycontractions 링크에서 다운로드하려면 GoogleNews-vectors-negative300.bin 링크가 필요합니다. * python3의 예제 코드.

  6. ==============================

    6.이것이 낡은 질문 임에도 불구하고 내가 볼 수있는 한 실제로 해결책이 없기 때문에 나는 대답할만한 것으로 생각했다.

    이것이 낡은 질문 임에도 불구하고 내가 볼 수있는 한 실제로 해결책이 없기 때문에 나는 대답할만한 것으로 생각했다.

    관련 NLP 프로젝트에서이 작업을 수행해야했으며 여기서는 아무 것도없는 것처럼 문제를 해결하기로 결정했습니다. 관심이 있다면 내 확장기 github 저장소를 확인할 수 있습니다.

    NLTK, 별도로 다운로드해야 할 Stanford Core NLP 모델 및 이전 답변의 사전을 기반으로하는 상당히 나쁘게 최적화 된 프로그램입니다. 필요한 모든 정보는 README와 호화로운 주석 코드에 있어야합니다. 주석이 달린 코드가 죽은 코드라는 것을 알고 있지만, 이것은 내가 스스로를 명확하게 유지하는 방법입니다.

    expander.py의 입력 예는 다음과 같습니다.

        ["I won't let you get away with that",  # won't ->  will not
        "I'm a bad person",  # 'm -> am
        "It's his cat anyway",  # 's -> is
        "It's not what you think",  # 's -> is
        "It's a man's world",  # 's -> is and 's possessive
        "Catherine's been thinking about it",  # 's -> has
        "It'll be done",  # 'll -> will
        "Who'd've thought!",  # 'd -> would, 've -> have
        "She said she'd go.",  # she'd -> she would
        "She said she'd gone.",  # she'd -> had
        "Y'all'd've a great time, wouldn't it be so cold!", # Y'all'd've -> You all would have, wouldn't -> would not
        " My name is Jack.",   # No replacements.
        "'Tis questionable whether Ma'am should be going.", # 'Tis -> it is, Ma'am -> madam
        "As history tells, 'twas the night before Christmas.", # 'Twas -> It was
        "Martha, Peter and Christine've been indulging in a menage-à-trois."] # 've -> have
    

    출력은 다음과 같습니다.

        ["I will not let you get away with that",
        "I am a bad person",
        "It is his cat anyway",
        "It is not what you think",
        "It is a man's world",
        "Catherine has been thinking about it",
        "It will be done",
        "Who would have thought!",
        "She said she would go.",
        "She said she had gone.",
        "You all would have a great time, would not it be so cold!",
        "My name is Jack.",
        "It is questionable whether Madam should be going.",
        "As history tells, it was the night before Christmas.",
        "Martha, Peter and Christine have been indulging in a menage-à-trois."]
    

    그래서이 작은 테스트 문장 세트를 위해 몇 가지 가장자리 케이스를 테스트 해 보았습니다. 잘 작동합니다.

    이 프로젝트는 지금 중요성을 잃어 버렸기 때문에 더 이상 적극적으로 개발하지 않을 것입니다. 이 프로젝트에 대한 도움을 주시면 감사하겠습니다. 수행 할 작업이 TODO 목록에 작성됩니다. 또는 내 비단뱀을 개선하는 방법에 대한 조언이 있으면 매우 감사 할 것입니다.

  7. from https://stackoverflow.com/questions/19790188/expanding-english-language-contractions-in-python by cc-by-sa and MIT license