hgzero/tools/aggregate_interview.py
2025-10-14 17:09:07 +09:00

191 lines
6.6 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
고객경험 인터뷰 결과 취합 스크립트
"""
import re
from collections import defaultdict
def read_interview_file(filepath):
"""인터뷰 파일 읽기"""
with open(filepath, 'r', encoding='utf-8') as f:
return f.read()
def extract_interviews(content):
"""인터뷰 내용을 단계별로 추출"""
# 9단계 정의
stages = [
"문제 인식",
"솔루션 탐색",
"도입 및 준비",
"회의 참여",
"회의록 작성",
"검토 및 보완",
"공유",
"활용 및 추적",
"성과 평가 및 개선"
]
# 단계별 데이터 저장
stage_data = defaultdict(lambda: {
'actions': [],
'positive_feelings': [],
'negative_feelings': [],
'thoughts': []
})
# 각 단계별로 내용 추출
for stage in stages:
# 해당 단계의 모든 내용 찾기
pattern = rf"### \d+단계: {re.escape(stage)}(.*?)(?=### \d+단계:|## 인터뷰|\Z)"
matches = re.findall(pattern, content, re.DOTALL)
for match in matches:
# 행동 추출
action_pattern = r"\*\*행동:\*\*(.*?)(?=\*\*생각:|\*\*긍정적 느낌:|\Z)"
action_match = re.search(action_pattern, match, re.DOTALL)
if action_match:
action = action_match.group(1).strip()
if action and action not in stage_data[stage]['actions']:
stage_data[stage]['actions'].append(action)
# 생각 추출 (전반적 의견으로 사용)
thought_pattern = r"\*\*생각:\*\*(.*?)(?=\*\*긍정적 느낌:|\*\*부정적 느낌:|\Z)"
thought_match = re.search(thought_pattern, match, re.DOTALL)
if thought_match:
thought = thought_match.group(1).strip()
# 따옴표 제거
thought = thought.strip('"').strip('"').strip('"')
if thought and thought not in stage_data[stage]['thoughts']:
stage_data[stage]['thoughts'].append(thought)
# 긍정적 느낌 추출
pos_pattern = r"\*\*긍정적 느낌:\*\*(.*?)(?=\*\*부정적 느낌:|\-\-\-|\Z)"
pos_match = re.search(pos_pattern, match, re.DOTALL)
if pos_match:
positive = pos_match.group(1).strip()
if positive and positive not in stage_data[stage]['positive_feelings']:
stage_data[stage]['positive_feelings'].append(positive)
# 부정적 느낌 추출
neg_pattern = r"\*\*부정적 느낌:\*\*(.*?)(?=\-\-\-|\Z)"
neg_match = re.search(neg_pattern, match, re.DOTALL)
if neg_match:
negative = neg_match.group(1).strip()
if negative and negative not in stage_data[stage]['negative_feelings']:
stage_data[stage]['negative_feelings'].append(negative)
return stages, stage_data
def aggregate_similar_content(items):
"""유사한 내용 통합 (간단한 키워드 기반)"""
if not items:
return []
# 중복 제거 및 정리
unique_items = []
for item in items:
# 앞뒤 공백, 개행 정리
cleaned = ' '.join(item.split())
if cleaned and cleaned not in unique_items:
unique_items.append(cleaned)
return unique_items
def generate_markdown_table(stages, stage_data):
"""마크다운 표 생성"""
md_content = "# 고객경험 인터뷰 결과 취합\n\n"
for stage in stages:
md_content += f"## {stage}\n\n"
md_content += "| 구분 | 내용 |\n"
md_content += "|------|------|\n"
data = stage_data[stage]
# 행동
actions = aggregate_similar_content(data['actions'])
if actions:
for i, action in enumerate(actions, 1):
if i == 1:
md_content += f"| **행동** | {action} |\n"
else:
md_content += f"| | {action} |\n"
else:
md_content += "| **행동** | - |\n"
# 긍정적 느낌
pos_feelings = aggregate_similar_content(data['positive_feelings'])
if pos_feelings:
for i, feeling in enumerate(pos_feelings, 1):
if i == 1:
md_content += f"| **긍정적 느낌** | {feeling} |\n"
else:
md_content += f"| | {feeling} |\n"
else:
md_content += "| **긍정적 느낌** | - |\n"
# 부정적 느낌
neg_feelings = aggregate_similar_content(data['negative_feelings'])
if neg_feelings:
for i, feeling in enumerate(neg_feelings, 1):
if i == 1:
md_content += f"| **부정적 느낌** | {feeling} |\n"
else:
md_content += f"| | {feeling} |\n"
else:
md_content += "| **부정적 느낌** | - |\n"
# 전반적 의견 (생각)
thoughts = aggregate_similar_content(data['thoughts'])
if thoughts:
for i, thought in enumerate(thoughts, 1):
if i == 1:
md_content += f"| **전반적 의견** | {thought} |\n"
else:
md_content += f"| | {thought} |\n"
else:
md_content += "| **전반적 의견** | - |\n"
md_content += "\n"
return md_content
def main():
"""메인 실행 함수"""
input_file = '/Users/adela/home/workspace/HGZero/define/고객경험인터뷰결과.md'
output_file = '/Users/adela/home/workspace/HGZero/define/고객경험인터뷰결과취합.md'
# 파일 읽기
print("파일 읽는 중...")
content = read_interview_file(input_file)
# 인터뷰 추출
print("인터뷰 내용 추출 중...")
stages, stage_data = extract_interviews(content)
# 마크다운 표 생성
print("마크다운 표 생성 중...")
md_output = generate_markdown_table(stages, stage_data)
# 파일 저장
print(f"결과 파일 저장 중: {output_file}")
with open(output_file, 'w', encoding='utf-8') as f:
f.write(md_output)
print("완료!")
# 통계 출력
print("\n=== 추출 통계 ===")
for stage in stages:
data = stage_data[stage]
print(f"{stage}:")
print(f" - 행동: {len(data['actions'])}")
print(f" - 긍정적 느낌: {len(data['positive_feelings'])}")
print(f" - 부정적 느낌: {len(data['negative_feelings'])}")
print(f" - 전반적 의견: {len(data['thoughts'])}")
if __name__ == "__main__":
main()