File size: 2,920 Bytes
bbe57f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
from typing import List

def optimize_content(content: str, platform: str) -> str:
    """
    Optimize content for a specific platform
    """
    # Apply platform-specific optimizations
    if platform == "twitter":
        # Truncate to Twitter's character limit
        if len(content) > 280:
            content = content[:277] + "..."
    elif platform == "instagram":
        # Add emojis and make more visual
        content = content.replace(".", ". ✨ ")
    elif platform == "linkedin":
        # Make more professional
        content = content.replace("cool", "effective").replace("awesome", "valuable")
    
    return content

def extract_keywords(text: str) -> List[str]:
    """
    Extract keywords from text (simplified implementation)
    """
    # Remove punctuation and split into words
    words = re.findall(r'\b\w+\b', text.lower())
    
    # Filter out common stop words and short words
    stop_words = {'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'could', 'should', 'may', 'might', 'must', 'can', 'this', 'that', 'these', 'those', 'i', 'you', 'he', 'she', 'it', 'we', 'they', 'me', 'him', 'her', 'us', 'them', 'my', 'your', 'his', 'its', 'our', 'their', 'mine', 'yours', 'hers', 'ours', 'theirs'}
    
    keywords = [word for word in words if len(word) > 3 and word not in stop_words]
    
    # Count occurrences and return top 10
    word_count = {}
    for word in keywords:
        word_count[word] = word_count.get(word, 0) + 1
    
    sorted_words = sorted(word_count.items(), key=lambda x: x[1], reverse=True)
    return [word for word, count in sorted_words[:10]]

def calculate_readability_score(text: str) -> float:
    """
    Calculate a basic readability score
    """
    sentences = len(re.split(r'[.!?]+', text))
    words = len(text.split())
    syllables = sum(count_syllables(word) for word in text.split())
    
    if sentences == 0 or words == 0:
        return 0.0
    
    # Simplified Flesch Reading Ease calculation
    avg_sentence_length = words / sentences
    avg_syllables_per_word = syllables / words
    
    score = 206.835 - (1.015 * avg_sentence_length) - (84.6 * avg_syllables_per_word)
    return max(0, min(100, score))

def count_syllables(word: str) -> int:
    """
    Count syllables in a word (simplified)
    """
    word = word.lower()
    vowels = "aeiouy"
    syllable_count = 0
    prev_was_vowel = False
    
    for char in word:
        if char in vowels:
            if not prev_was_vowel:
                syllable_count += 1
            prev_was_vowel = True
        else:
            prev_was_vowel = False
    
    # Adjust for silent 'e' at the end
    if word.endswith('e') and syllable_count > 1:
        syllable_count -= 1
    
    return max(1, syllable_count)