Spaces:
Runtime error
Runtime error
SoSa123456
commited on
Commit
•
b77e4f5
1
Parent(s):
8de8d38
Create Utils.py
Browse files
Utils.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
Utility functions for the Climate Change Radio Script Generator
|
3 |
+
'''
|
4 |
+
import nltk
|
5 |
+
import string
|
6 |
+
# Download the necessary NLTK data
|
7 |
+
nltk.download('punkt')
|
8 |
+
# Function to clean the generated paragraph
|
9 |
+
def clean_paragraph(entry):
|
10 |
+
paragraphs = entry.split('\n')
|
11 |
+
for i in range(len(paragraphs)):
|
12 |
+
split_sentences = nltk.tokenize.sent_tokenize(paragraphs[i], language='english')
|
13 |
+
if i == len(paragraphs) - 1 and split_sentences[:1][-1] not in string.punctuation:
|
14 |
+
paragraphs[i] = " ".join(split_sentences[:-1])
|
15 |
+
return capitalize_first_char("\n".join(paragraphs))
|
16 |
+
# Function to capitalize the first character of a string
|
17 |
+
def capitalize_first_char(entry):
|
18 |
+
for i in range(len(entry)):
|
19 |
+
if entry[i].isalpha():
|
20 |
+
return entry[:i] + entry[i].upper() + entry[i + 1:]
|
21 |
+
return entry
|