개발이야기
TXT 에서 유효한 문장 분리하기 ( NLTK)
donny.00
2024. 10. 25. 19:57
반응형
txt 에서 유효한 문장별로 분리하는 로직을 짜느라 진짜 고생 많이 했었다.
그런데,,, 찾아보니 자연처 처리 툴킷이 있었음. 아오 -_-'
shell > pip install nltk
import nltk
nltk.download('punkt')
from nltk.tokenize import sent_tokenize
# 파일 읽기
with open('/mnt/data/b.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
# 문장별로 분리 후 출력
with open('/mnt/data/output.txt', 'w', encoding='utf-8') as output_file:
for line in lines:
sentences = sent_tokenize(line) # 문장 단위로 분리
for sentence in sentences:
output_file.write(sentence + '\n') # 한 문장씩 새 줄에 저장
print("문장별로 분리된 파일이 'output.txt'에 저장되었습니다.")
나는 pip 설치가 잘 안되서 아래와 같이 처리 했다.
> python3 -m venv path/to/venv
> source path/to/venv/bin/activate
> python3 -m pip install nltk
> 위 의 파이썬 코드 실행
이거 실행하다가 nltk 다운로드 어쩌고 오류가 발생하면 그냥 콘솔에서 처리해줌
> python3
> import nltk
> nltk.download('punkt')
> nltk.download('punkt_tab')
이거 까지 하고 나서 다시 실행해보니 진짜 문장 잘 발라내줌.
100% 만족할수는 없었지만... 95% 이상 만족함.
반응형