Download utils/text_processing.py from Ahmed766/auranexus: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/Ahmed766/auranexus/resolve/main/utils/text_processing.py
- Command line
-
hf download hf://Ahmed766/auranexus/utils/text_processing.py
-
curl -L -o text_processing.py https://huggingface.co/Ahmed766/auranexus/resolve/main/utils/text_processing.py
2.92 kB
| 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) |