Programming/python-nlp

문장분절을 위한 Python Library (Sentences Segmentation) - Spacy, kss

방황하는 데이터불도저 2022. 8. 23. 18:00

Spacy는 고급 자연어 처리를 도와주는 python 라이브러리이다.

 

Spacy 설치방법 : https://spacy.io/usage

 

Install spaCy · spaCy Usage Documentation

spaCy is a free open-source library for Natural Language Processing in Python. It features NER, POS tagging, dependency parsing, word vectors and more.

spacy.io

 

내가 설치하고자 하는 path에서 아래의 커맨드로 설치 - Linux에 pip으로 설치의 경우

pip install -U pip setuptools wheel
pip install -U spacy
python -m spacy download en_core_web_sm     #영어 자연어모델 다운로드

 

간단하게 설치 후, python에서 코드 5줄로 문장분절 끝

import spacy

model = spacy.load("en_core_web_sm")     #아까 다운로드 받은 모델을 로드함

text = "Hello! I am very pleased to be here in Busan International Film Festival. Thanks for the invitation! The title of this film is 'Top gun : Maverick'. This film is about the story of the best pilot."

doc = model(text)

sent_seg = list(doc.sents)

print(sent_seg[1])
# output : I am very pleased to be here in Busan International Film Festival.

 

 

한국어의 경우에는 오직 문장분절을 위한 툴킷인 kss가 있다.

https://github.com/hyunwoongko/kss

 

GitHub - hyunwoongko/kss: Kss: A Toolkit for Korean sentence segmentation

Kss: A Toolkit for Korean sentence segmentation. Contribute to hyunwoongko/kss development by creating an account on GitHub.

github.com

 

원하는 path에 아래의 커맨드로 설치 후,

pip install kss
pip install python-mecab-kor

 

spacy보다 더 간단하다. python 3줄

from kss import split_sentences

text = "안녕하세요! 여기 부산국제영화제에 오게되어 정말 기쁩니다. 초대해주셔서 감사드립니다. 이번 영화의 제목은 '탑건 : 메버릭'입니다. 이 영화는 최고의 파일럿의 삶에 대한 이야기입니다."

sent_seg = split_sentences(text)

# You can install `python-mecab-kor` for faster kss execution. Try to install it using `pip install python-mecab-kor`.
# 자동으로 실행이 더 빠른 패키지가 설치되었다.

print(sent_seg)
# output : ['안녕하세요!', '여기 부산국제영화제에 오게되어 정말 기쁩니다.', '초대해주셔서 감사드립니다.', "이번 영화의 제목은 '탑건 : 메버릭'입니다.", '이 영화는 최고의 파일럿의 삶에 대한 이야기입니다.']