-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathword_duration.py
More file actions
120 lines (102 loc) · 3.72 KB
/
Copy pathword_duration.py
File metadata and controls
120 lines (102 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import pandas as pd
import numpy as np
def calculate_word_count(text):
"""Calculates word count for a given transcript string."""
if pd.isna(text) or not str(text).strip():
return 0
return len(str(text).split())
def assign_duration_tertile(duration):
"""
Duration tertile logic:
- Short: <= 211.8 seconds
- Medium: > 211.8 and <= 498 seconds
- Long: > 498 seconds
- Missing: NaN / NaT / None
"""
if pd.isna(duration):
return "Missing"
try:
dur = float(duration)
if dur <= 211.8:
return "Short"
elif dur <= 498.0:
return "Medium"
else:
return "Long"
except (ValueError, TypeError):
return "Missing"
def assign_word_tertile(word_count):
"""
Word count tertile logic:
- Short: <= 100 words
- Medium: > 100 and <= 187 words
- Long: > 187 words
"""
if pd.isna(word_count):
return "Short"
wc = int(word_count)
if wc <= 100:
return "Short"
elif wc <= 187:
return "Medium"
else:
return "Long"
def map_complexity(row):
"""
Maps (Duration_Tertile, Word_Tertile) combination to
Designation (Simple, Moderate, Complex) and Numeric Score (1, 2, 3).
"""
dur = row['Duration_Tertile']
words = row['Word_Tertile']
# --- COMPLEX CATEGORY (3) ---
if (dur == "Long" and words == "Long") or \
(dur == "Long" and words == "Medium") or \
(dur == "Medium" and words == "Long") or \
(dur == "Missing" and words == "Long"):
return "Complex", 3
# --- MODERATE CATEGORY (2) ---
elif (dur == "Medium" and words == "Medium") or \
(dur == "Short" and words == "Medium") or \
(dur == "Short" and words == "Long") or \
(dur == "Missing" and words == "Medium"):
return "Moderate", 2
# --- SIMPLE CATEGORY (1) ---
elif (dur == "Short" and words == "Short") or \
(dur == "Long" and words == "Short") or \
(dur == "Medium" and words == "Short") or \
(dur == "Missing" and words == "Short"):
return "Simple", 1
# Catch-all fallback if any unhandled edge case occurs
else:
return "Simple", 1
def process_transcripts(input_csv_path, output_csv_path):
print(f"Loading data from {input_csv_path}...")
df = pd.read_csv(input_csv_path)
# 1. Calculate Word Count
print("Calculating word counts...")
df['Word_Count'] = df['Transcript'].apply(calculate_word_count)
# 2. Assign Tertiles
print("Assigning duration and word count tertiles...")
df['Duration_Tertile'] = df['Duration'].apply(assign_duration_tertile)
df['Word_Tertile'] = df['Word_Count'].apply(assign_word_tertile)
# 3. Apply Decision Matrix for Complexity Mapping
print("Mapping complexity designations and numeric codes...")
complexity_results = df.apply(map_complexity, axis=1)
# Split returned tuple into designated columns
df['Complexity_Designation'] = [res[0] for res in complexity_results]
df['Complexity_Score'] = [res[1] for res in complexity_results]
# Save output
df.to_csv(output_csv_path, index=False)
print(f"Success! Processed {len(df)} records. Saved results to {output_csv_path}\n")
# Display Breakdown Summary
print("=== COMPLEXITY BREAKDOWN SUMMARY ===")
summary = df['Complexity_Designation'].value_counts()
for category, count in summary.items():
pct = (count / len(df)) * 100
print(f" - {category}: {count} ({pct:.2f}%)")
if __name__ == "__main__":
# Update these paths to match your local file names
INPUT_FILE = "UA_Master_Combined_Rev.csv"
OUTPUT_FILE = "UA_Master_complexity.csv"
# Run the pipeline
process_transcripts(INPUT_FILE, OUTPUT_FILE)