To process Spanish text, you can implement a pipeline that cleans whitespace, handles punctuation (specifically ¿, —, and ¡), splits the text into sentences, and then applies es2ipa to each sentence.
Note: The implementation requires adding the path containing es_to_ipa to sys.path if it is not installed as a package.
import re
import sys
from es_to_ipa import es2ipa
def split_sentences_en(text, min_len=10):
# Replace newlines, tabs, and spaces with a single space
text = re.sub('[
]+', ' ', text)
# Add a delimiter after specific punctuation
text = re.sub('([¿—¡])', r'\1 $#!', text)
# Split sentences and strip whitespace
sentences = [s.strip() for s in text.split(' $#!')]
if len(sentences[-1]) == 0:
del sentences[-1]
new_sent = []
for ind, sent in enumerate(sentences):
if sent in ['¿', '—', '¡']:
new_sent.append(sent)
else:
new_sent.append(es2ipa(sent))
return ''.join(new_sent)
# Usage
text = '—¿Habéis estado casada alguna vez?'
print(split_sentences_en(text))