polyglot 주요 기능 — 언어 감지부터 음역까지
polyglot 주요 기능
polyglot은 하나의 Text 객체만 만들면 언어 감지, 토큰화, 품사 태깅, 개체명 인식, 감성 분석, 형태소 분석, 단어 임베딩, 음역까지 이어서 쓸 수 있도록 설계됐어요. 각 기능이 어떻게 호출되는지 순서대로 살펴볼게요.
언어 감지 (Language Detection)
텍스트 언어를 코드와 이름으로 돌려줘요.
text = Text("Bonjour, Mesdames.")
print("Language Detected: Code={}, Name={}
".format(text.language.code, text.language.name))
Language Detected: Code=fr, Name=French
토큰화 (Tokenization)
.words는 단어 목록, .sentences는 문장 목록을 줘요.
zen = Text("Beautiful is better than ugly. "
"Explicit is better than implicit. "
"Simple is better than complex.")
print(zen.words)
print(zen.sentences)
품사 태깅 (Part of Speech Tagging)
.pos_tags로 단어마다 품사 태그를 붙여요.
text = Text(u"O primeiro uso de desobediência civil em massa ocorreu em setembro de 1906.")
for word, tag in text.pos_tags:
print(u"{:<16}{:>2}".format(word, tag))
개체명 인식 (Named Entity Recognition)
.entities로 장소·사람 같은 개체를 찾아요.
text = Text(u"In Großbritannien war Gandhi mit dem westlichen Lebensstil vertraut geworden")
print(text.entities)
[I-LOC([u'Großbritannien']), I-PER([u'Gandhi'])]
감성 분석 (Polarity)
단어마다 극성(polarity) 값을 줘요.
for w in zen.words[:6]:
print(u"{:<16}{:>2}".format(w, w.polarity))
단어 임베딩
Word 객체의 .neighbors로 유사어를, .vector로 벡터를 얻어요.
word = Word("Obama", language="en")
print("Neighbors (Synonms) of {}".format(word))
for w in word.neighbors:
print(u"{:<16}".format(w))
print("The first 10 dimensions out the {} dimensions".format(word.vector.shape[0]))
print(word.vector[:10])
형태소 분석 (Morphology)
.morphemes로 단어를 형태소로 쪼개요.
word = Text("Preprocessing is an essential step.").words[0]
print(word.morphemes)
# [u'Pre', u'process', u'ing']
음역 (Transliteration)
Transliterator로 스크립트를 다른 스크립트로 옮겨요.
from polyglot.transliteration import Transliterator
transliterator = Transliterator(source_lang="en", target_lang="ru")
print(transliterator.transliterate(u"preprocessing"))
# препрокессинг