반응형

맥북에서 작업했고, 워드 파일을 이용했습니다.

 

워드 파일의 지문을 문장 단위로 분리해주는게 가능할까? 라는 의문에서 시작했습니다.

참고로 chatGPT 4를 이용해 코드를 생성했습니다.

 

예전 같으면 어떤 식으로 접근해야 할지 몰라 

구글 검색을 전전하면서 실마리를 찾지 못했을텐데, 

chatGPT의 등장으로  저 같은 코드 초보는 감히 상상할 수 없을만큼 큰 도움을 받고 있습니다. 

 

 

해결하고 싶은 문제는 다음과 같습니다.

예를 들어 다음과 같은 지문이 있다고 할때,

In the past there was little genetic pressure to stop people from becoming obese. Genetic mutations that drove people to consume fewer calories were much less likely to be passed on, because in an environment where food was scarcer and its hunting or gathering required considerable energy outlay, an individual with that mutation would probably die before they had a chance to reproduce. Mutations that in our environment of abundant food now drive us towards obesity, on the other hand, were incorporated into the population. Things are of course very different now but the problem is that evolutionary timescales are long. It's only in the last century or so, approximately 0.00004 per cent of mammalian evolutionary time, that we managed to tweak our environment to such a degree that we can pretty much eat whatever we want, whenever we want it. Evolution has another couple of thousand years to go before it can catch up with the current reality of online food shopping and delivery.

 

위 지문을 아래와 같이 문장 단위로 구분하는 일이 종종 필요합니다.

물론 수작업으로 해도 되지만 지문이 많아지면 그것도 쉽지 않아서,

혹시 코딩으로 가능한지 시도해 보았습니다. 

In the past there was little genetic pressure to stop people from becoming obese.

Genetic mutations that drove people to consume fewer calories were much less likely to be passed on, because in an environment where food was scarcer and its hunting or gathering required considerable energy outlay, an individual with that mutation would probably die before they had a chance to reproduce.

Mutations that in our environment of abundant food now drive us towards obesity, on the other hand, were incorporated into the population.

Things are of course very different now but the problem is that evolutionary timescales are long.

It's only in the last century or so, approximately 0.00004 per cent of mammalian evolutionary time, that we managed to tweak our environment to such a degree that we can pretty much eat whatever we want, whenever we want it.

Evolution has another couple of thousand years to go before it can catch up with the current reality of online food shopping and delivery.

 

일단 챗GPT 사이트(https://chat.openai.com/chat)로 가서

아래 프롬프트를 이용해 코드를 생성하도록 했습니다.  

 

chatGPT 4 Prompt:

 

맥북 사용함.

파이선을 이용해 코드 작성.
선택한 텍스트를 문장으로 구분해주는 파이썬 코드 작성.
각 문장을 줄바꿈으로 구분하고 나열해줄 것.
각 문장은 완벽한 형태의 문장일 것.
물음표로 끝나는 문장도 구분해 줄 것.
마침표로 끝나는 것 중에 문장이 아닌 것이 많으므로 이것도 상세 구분히 가능해야 함.
코드의 주석을 최대로 달아줄 것.
실행 방법도 상세히 알려줄 것.

워드 파일을 불러와서 원본 아래 결과값을 출력하는 것으로 수정할 것.
워드 파일은 창을 열어 유저가 지정할 수 있게 해줄 것.

 

그리고 그 결과값입니다. 

한방에 원하는 결과가 나오지 않기 때문에

챗GPT가 잘 알아듣게 원하는 내용을 말해줘야 합니다. 

그리고 오류가 나오면 원하는 내용을 다시 문의하면서 수정했습니다. 

 

파이썬 코드를 실행해 본 적이 없다면 

엄청난 약간의 진입장벽을 느낄 수 있습니다ㅠ.ㅠ 

 

제가 설명을 남기면 좋은데 자신이 없네요. 

유튜브에 기본 자료를 참고하시면 좋을듯 싶고,

실행 방법에 대해 궁금한 사항이 있으면 댓글 주시면 최대한 설명 남기겠습니다. 

 


import os
import nltk
from nltk.tokenize import PunktSentenceTokenizer
from nltk.tokenize.punkt import PunktTrainer, PunktParameters
from docx import Document
import tkinter as tk
from tkinter import filedialog

nltk.download('punkt')

# 사용자 정의 줄임말 목록
custom_abbreviations = "etc. e.g. i.e."

def split_sentences(text):
    # PunktTrainer를 사용하여 사용자 정의 문장 분리 문자를 추가합니다.
    punkt_param = PunktParameters()
    punkt_param.abbrev_types = set(custom_abbreviations.split())
    trainer = PunktTrainer(text, punkt_param)
    tokenizer = PunktSentenceTokenizer(trainer.get_params())

    sentences = tokenizer.tokenize(text)
    return '\n\n'.join(sentences)

def main():
    # 파일 선택 창 띄우기
    root = tk.Tk()
    root.withdraw()
    file_path = filedialog.askopenfilename(filetypes=[("Word files", "*.docx")])

    if not file_path:
        print("No file selected.")
        return

    # 워드 파일 읽기
    document = Document(file_path)

    # 원본 텍스트 추출
    text = ""
    for paragraph in document.paragraphs:
        text += paragraph.text + "\n"

    # 문장 분리
    split_text = split_sentences(text)

    # 결과값을 원본 텍스트 아래에 추가
    document.add_paragraph("\n\n---\n\n")
    document.add_paragraph(split_text)

    # 결과를 새 파일에 저장
    output_file_path = os.path.splitext(file_path)[0] + "_output.docx"
    document.save(output_file_path)

    print(f"Processed file saved at: {output_file_path}")


if __name__ == "__main__":
    main()

 

저는 PyCharm을 이용해 실행했고 잘 작동하는 것을 확인했습니다.

코드를 실행하면 워드 파일의 위치를 지정하도록 물어보고, 

파일을 선택하면 워드 파일을 열어서 원문 뒤에 문장으로 구분된 결과값을 나열한 파일을 생성해 줍니다. 

내용이 긴 파일도 작성하는 데 몇초면 가능했습니다. 

 

결과값은 95% 이상 잘 작동하는듯 합니다.

완벽하진 않지만 이정도면 작업하는데 문제 없어서 만족스러웠습니다 :)

반응형

'파이썬' 카테고리의 다른 글

단진자 운동을 시각화한 파이썬 코드  (0) 2023.03.22
반응형

ChatGPT 4.0이 나왔다는 소식을 듣고,

성능이 궁금해 20달러 투척하고 유료 가입했습니다. 

 

평소 궁금했던 것들 이것저것 해보고 있는데,

이런 것도 가능한가 싶어서 단진자 운동을 시각화하는 파이썬 코드를 생성해달라고 했습니다.  

 

결과는 성공적 ㅎㅎ

ChatGPT Prompt : 

파이썬을 이용해 중력을 받는 단진자가 대략 70도 각도로 좌우로 움직이는 모습을 시각화하는 코드를 만들어줘. matplotlib과 FuncAnimation을 이용해서 작성해줘.

 

 

Python Code : 

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# 단진자의 파라미터
g = 9.81  # 중력 가속도 (m/s^2)
L = 1.0   # 단진자의 길이 (m)
theta_0 = np.deg2rad(45)  # 초기 각도 (45도를 라디안으로 변환)

# 시간 변수
t = np.linspace(0, 10, 1000)  # 시간 범위 (0초부터 10초까지)

# 단진자의 움직임을 계산하는 함수
def pendulum_motion(t, theta_0, L, g):
    return theta_0 * np.cos(np.sqrt(g / L) * t)

# 시각화를 위한 그림 및 축 설정
fig, ax = plt.subplots()
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_aspect('equal', adjustable='box')

# 단진자를 표현하는 선과 점
line, = ax.plot([], [], lw=2)
dot, = ax.plot([], [], 'o', markersize=10, color='red')

# 애니메이션을 위한 초기화 함수
def init():
    line.set_data([], [])
    dot.set_data([], [])
    return line, dot,

# 애니메이션을 위한 업데이트 함수
def update(frame):
    theta = pendulum_motion(t[frame], theta_0, L, g)
    x = L * np.sin(theta)
    y = -L * np.cos(theta)
    line.set_data([0, x], [0, y])
    dot.set_data(x, y)
    return line, dot,

# 애니메이션 실행
ani = FuncAnimation(fig, update, frames=len(t), init_func=init, blit=True, interval=20)
plt.show()
반응형

+ Recent posts