|
|
import re |
|
|
from typing import List |
|
|
|
|
|
def optimize_content(content: str, platform: str) -> str: |
|
|
""" |
|
|
Optimize content for a specific platform |
|
|
""" |
|
|
|
|
|
if platform == "twitter": |
|
|
|
|
|
if len(content) > 280: |
|
|
content = content[:277] + "..." |
|
|
elif platform == "instagram": |
|
|
|
|
|
content = content.replace(".", ". ✨ ") |
|
|
elif platform == "linkedin": |
|
|
|
|
|
content = content.replace("cool", "effective").replace("awesome", "valuable") |
|
|
|
|
|
return content |
|
|
|
|
|
def extract_keywords(text: str) -> List[str]: |
|
|
""" |
|
|
Extract keywords from text (simplified implementation) |
|
|
""" |
|
|
|
|
|
words = re.findall(r'\b\w+\b', text.lower()) |
|
|
|
|
|
|
|
|
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] |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
if word.endswith('e') and syllable_count > 1: |
|
|
syllable_count -= 1 |
|
|
|
|
|
return max(1, syllable_count) |