[python] 파이썬으로 단어가 영어 단어인지 확인하는 방법?

단어가 영어 사전에 있는지 파이썬 프로그램에서 확인하고 싶습니다.

나는 nltk wordnet 인터페이스가 갈 길이라고 생각하지만 그런 간단한 작업에 그것을 사용하는 방법에 대한 실마리는 없습니다.

def is_english_word(word):
    pass # how to I implement is_english_word?

is_english_word(token.lower())

앞으로, 단어의 단수형이 사전에 있는지 확인하고 싶을 수도 있습니다 (예 : 속성-> 속성-> 영어 단어). 어떻게하면 되나요?



답변

더 많은 기능과 유연성을 위해와 같은 전용 맞춤법 검사 라이브러리를 사용하십시오 PyEnchant. 거기의 튜토리얼은 , 또는 당신은 단지에 바로 뛰어들 수있다 :

>>> import enchant
>>> d = enchant.Dict("en_US")
>>> d.check("Hello")
True
>>> d.check("Helo")
False
>>> d.suggest("Helo")
['He lo', 'He-lo', 'Hello', 'Helot', 'Help', 'Halo', 'Hell', 'Held', 'Helm', 'Hero', "He'll"]
>>>

PyEnchant몇 가지 사전 (en_GB, en_US, de_DE, fr_FR)이 제공되지만 더 많은 언어를 원할 경우 OpenOffice를 사용할 수 있습니다.

이라는 복수형 라이브러리가있는 inflect것 같지만 그것이 좋은지 전혀 모르겠습니다.


답변

WordNet에 모든 영어 단어가 포함되어 있지 않기 때문에 WordNet에서는 제대로 작동하지 않습니다. 마법이없는 NLTK를 기반으로 한 또 다른 가능성은 NLTK의 말 코퍼스입니다.

>>> from nltk.corpus import words
>>> "would" in words.words()
True
>>> "could" in words.words()
True
>>> "should" in words.words()
True
>>> "I" in words.words()
True
>>> "you" in words.words()
True


답변

NLTK 사용 :

from nltk.corpus import wordnet

if not wordnet.synsets(word_to_test):
  #Not an English Word
else:
  #English Word

wordnet 설치에 문제가 있거나 다른 방법을 시도 하려면 이 기사를 참조하십시오 .


답변

단어 목록을 찾기 위해 세트를 사용하면 단어 목록이 더 빠르기 때문에 저장합니다.

with open("english_words.txt") as word_file:
    english_words = set(word.strip().lower() for word in word_file)

def is_english_word(word):
    return word.lower() in english_words

print is_english_word("ham")  # should be true if you have a good english_words.txt

질문의 두 번째 부분에 대답하기 위해 복수형은 이미 좋은 단어 목록에 있지만 어떤 이유로 목록에서 특정 단어를 구체적으로 제외하려면 실제로 처리 할 수있는 함수를 작성할 수 있습니다. 그러나 영어 복수형 규칙은 까다로워서 단어 목록에 복수형을 포함시킬 것입니다.

영어 단어 목록을 찾을 수있는 곳은 Googling “영어 단어 목록”으로 여러 단어를 찾았습니다. 다음은 하나입니다. http://www.sil.org/linguistics/wordlists/english/wordlist/wordsEn.txt 이러한 방언 중 하나를 원하는 경우 영국식 또는 미국식 영어를 Google에 사용할 수 있습니다.


답변

더 빠른 NLTK 기반 솔루션의 경우 선형 검색을 피하기 위해 단어 세트를 해시 할 수 있습니다.

from nltk.corpus import words as nltk_words
def is_english_word(word):
    # creation of this dictionary would be done outside of 
    #     the function because you only need to do it once.
    dictionary = dict.fromkeys(nltk_words.words(), None)
    try:
        x = dictionary[word]
        return True
    except KeyError:
        return False


답변

문제를 해결하기위한 3 가지 패키지 기반 솔루션이 있습니다. 그들은 pyenchant, wordnet 및 corpus (자체 정의 또는 ntlk)입니다. Pychant는 win64에서 py3으로 쉽게 설치할 수 없습니다 . 말뭉치가 완전하지 않기 때문에 Wordnet은 잘 작동하지 않습니다. 그래서 나를 위해 @Sadik 의해 답변 된 솔루션을 선택하고 ‘set (words.words ())’를 사용하여 속도를 높입니다.

먼저:

pip3 install nltk
python3

import nltk
nltk.download('words')

그때:

from nltk.corpus import words
setofwords = set(words.words())

print("hello" in setofwords)
>>True


답변

pyEnchant.checker SpellChecker로 :

from enchant.checker import SpellChecker

def is_in_english(quote):
    d = SpellChecker("en_US")
    d.set_text(quote)
    errors = [err.word for err in d]
    return False if ((len(errors) > 4) or len(quote.split()) < 3) else True

print(is_in_english('“办理美国加州州立大学圣贝纳迪诺分校高仿成绩单Q/V2166384296加州州立大学圣贝纳迪诺分校学历学位认证'))
print(is_in_english('“Two things are infinite: the universe and human stupidity; and I\'m not sure about the universe.”'))

> False
> True