File size: 7,704 Bytes
379f35a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import gradio as gr
import spacy
from transformers import pipeline
import json

# Load models once at startup
nlp = spacy.load("en_core_web_sm")
classifier = pipeline("zero-shot-classification")

def analyze_sentence_purposes(script):
    purposes = []
    doc = nlp(script)
    for sent in doc.sents:
        if any(token.text in ['?', 'how', 'why', 'what'] for token in sent):
            purposes.append(("QUESTION", "Engages curiosity"))
        elif any(token.lemma_ in ['show', 'teach', 'explain'] for token in sent):
            purposes.append(("INSTRUCTION", "Provides guidance")) 
        elif any(token.lemma_ in ['discover', 'find', 'reveal'] for token in sent):
            purposes.append(("REVELATION", "Shares discovery"))
        elif any(token.lemma_ in ['struggle', 'problem', 'difficult'] for token in sent):
            purposes.append(("CONFLICT", "Presents challenge"))
        elif any(token.lemma_ in ['result', 'success', 'transform'] for token in sent):
            purposes.append(("RESOLUTION", "Shows payoff"))
        else:
            purposes.append(("CONTEXT", "Sets scene"))
    return purposes

def map_emotional_arc(script):
    emotional_phases = []
    doc = nlp(script)
    emotional_indicators = {
        'struggle': ['frustrat', 'difficult', 'struggle', 'problem', 'failed'],
        'curiosity': ['wonder', 'curious', 'question', 'mystery', 'secret'],
        'discovery': ['find', 'discover', 'learn', 'realize', 'figure out'],
        'transformation': ['change', 'transform', 'better', 'improve', 'success'],
        'satisfaction': ['happy', 'proud', 'excited', 'amazing', 'awesome']
    }
    for sent in doc.sents:
        sentence_emotion = "NEUTRAL"
        max_strength = 0
        for emotion, keywords in emotional_indicators.items():
            strength = sum(1 for token in sent if token.lemma_ in keywords)
            if strength > max_strength:
                max_strength = strength
                sentence_emotion = emotion.upper()
        emotional_phases.append(sentence_emotion)
    return emotional_phases

def detect_narrative_pattern(semantic_analysis):
    purposes = [p[0] for p in semantic_analysis['purposes']]
    emotions = semantic_analysis['emotional_arc']
    pattern_signatures = {
        'TRANSFORMATION': [
            ['CONFLICT', 'REVELATION', 'RESOLUTION'],
            ['struggle', 'discovery', 'transformation']
        ],
        'INVESTIGATION': [
            ['QUESTION', 'REVELATION', 'RESOLUTION'], 
            ['curiosity', 'discovery', 'satisfaction']
        ],
        'STORYTELLING': [
            ['CONTEXT', 'CONFLICT', 'RESOLUTION'],
            ['neutral', 'struggle', 'satisfaction']
        ],
        'EXPLANATION': [
            ['QUESTION', 'INSTRUCTION', 'RESOLUTION'],
            ['curiosity', 'neutral', 'satisfaction']
        ]
    }
    best_pattern = None
    highest_match = 0
    for pattern_name, signature in pattern_signatures.items():
        purpose_match = sum(1 for i, purpose in enumerate(purposes) 
                          if i < len(signature[0]) and purpose == signature[0][i])
        emotion_match = sum(1 for i, emotion in enumerate(emotions)
                          if i < len(signature[1]) and emotion.lower() == signature[1][i])
        total_match = purpose_match + emotion_match
        if total_match > highest_match:
            highest_match = total_match
            best_pattern = pattern_name
    return best_pattern, highest_match / (len(purposes) + len(emotions))

def analyze_creator_intent(script, semantic_analysis):
    doc = nlp(script)
    intent_indicators = {
        'EDUCATE': ['teach', 'explain', 'show', 'learn', 'understand'],
        'ENTERTAIN': ['funny', 'hilarious', 'entertain', 'enjoy', 'laugh'],
        'INSPIRE': ['motivate', 'inspire', 'empower', 'transform', 'change'],
        'PERSUADE': ['should', 'must', 'need to', 'recommend', 'best'],
        'DOCUMENT': ['experience', 'journey', 'story', 'happened', 'went']
    }
    intent_scores = {intent: 0 for intent in intent_indicators.keys()}
    for token in doc:
        for intent, keywords in intent_indicators.items():
            if token.lemma_ in keywords:
                intent_scores[intent] += 1
    if any(p[0] == 'INSTRUCTION' for p in semantic_analysis['purposes']):
        intent_scores['EDUCATE'] += 2
    if 'struggle' in semantic_analysis['emotional_arc']:
        intent_scores['INSPIRE'] += 2
    primary_intent = max(intent_scores.items(), key=lambda x: x[1])
    return primary_intent[0], intent_scores

def detect_contextual_domain(script):
    doc = nlp(script)
    domain_indicators = {
        'TECHNOLOGY': ['software', 'app', 'code', 'digital', 'algorithm'],
        'HEALTH': ['energy', 'health', 'body', 'mind', 'wellness'],
        'BUSINESS': ['money', 'business', 'market', 'profit', 'investment'],
        'CREATIVE': ['design', 'create', 'art', 'build', 'make'],
        'ACADEMIC': ['research', 'study', 'theory', 'data', 'analysis']
    }
    domain_scores = {domain: 0 for domain in domain_indicators.keys()}
    for token in doc:
        for domain, terms in domain_indicators.items():
            if token.lemma_ in terms:
                domain_scores[domain] += 1
    primary_domain = max(domain_scores.items(), key=lambda x: x[1])
    sentence_complexity = sum(len(list(sent.root.children)) for sent in doc.sents) / len(list(doc.sents))
    sophistication = "ADVANCED" if sentence_complexity > 5 else "INTERMEDIATE" if sentence_complexity > 3 else "BEGINNER"
    return primary_domain[0], sophistication, domain_scores

def analyze_script(script):
    """NLP-First Universal Script Analysis"""
    if not script.strip():
        return "❌ Please enter a script to analyze"
    
    try:
        # Layer 1: Semantic Understanding
        semantic_results = {
            'purposes': analyze_sentence_purposes(script),
            'emotional_arc': map_emotional_arc(script)
        }
        
        # Layer 2: Narrative Pattern Detection
        pattern, pattern_confidence = detect_narrative_pattern(semantic_results)
        
        # Layer 3: Intent & Domain Intelligence
        primary_intent, intent_scores = analyze_creator_intent(script, semantic_results)
        primary_domain, sophistication, domain_scores = detect_contextual_domain(script)
        
        # Format results
        result = f"""
🎯 **UNIVERSAL SCRIPT ANALYSIS**

🧠 **SEMANTIC UNDERSTANDING:**
- Sentence Purposes: {', '.join([p[0] for p in semantic_results['purposes']])}
- Emotional Arc: {', '.join(semantic_results['emotional_arc'])}

🎭 **NARRATIVE PATTERN:**
- Primary Pattern: {pattern} ({pattern_confidence:.1%} confidence)

🎯 **CREATOR INTENT:**
- Primary Intent: {primary_intent}
- Intent Breakdown: {', '.join([f'{k}:{v}' for k,v in intent_scores.items() if v > 0])}

🌍 **DOMAIN INTELLIGENCE:**
- Primary Domain: {primary_domain}
- Sophistication: {sophistication}
- Domain Scores: {', '.join([f'{k}:{v}' for k,v in domain_scores.items() if v > 0])}

πŸ“Š **CONTENT BLUEPRINT:** {pattern} + {primary_domain} Domain
"""
        return result
        
    except Exception as e:
        return f"❌ Error analyzing script: {str(e)}"

# Create Gradio interface
demo = gr.Interface(
    fn=analyze_script,
    inputs=gr.Textbox(lines=10, placeholder="Paste your script here...", label="Script"),
    outputs=gr.Textbox(label="Analysis Results"),
    title="πŸ€– Universal Script Analyzer Pro",
    description="NLP-First Analysis: Semantic Understanding β†’ Narrative Patterns β†’ Intent & Domain"
)

# For API access
def api_analyze(script):
    return analyze_script(script)

if __name__ == "__main__":
    demo.launch(share=True)