Search code examples
pythonnltkvalueerror

Need help for ValueError: substring not found


I want to make the sentences as the following:

(N(Hace calor.)-(S(De todas formas, no salgo a casa.)))

(N(Además, va a venir Peter.)-(S(Sin embargo, no lo sé a qué hora llegará exactamente.)))

But the program can only gives me the first sentence and gives an error as ValueError: substring not found for the second sentence. Any one can help? Thanks!

Here is my code:

from nltk import tokenize
sent = 'Hace calor. De todas formas, no salgo a casa. Además, va a venir Peter. Sin embargo, no lo sé a qué hora llegará exactamente.'
Ant = ['De todas formas', 'Sin embargo']
sent = tokenize.sent_tokenize(sent)
for i in sent:
    for DMAnt in Ant:
        if DMAnt in i:
            sent = '(N(' + str(sent[sent.index(i)-1]) + ')-Antithesis-' +'(S(' + str(sent[sent.index(i)]) + '))'
    print(sent)

Solution

  • you are changing your sent. I recommend creating a new variable, it will solve the issue.

    import nltk
    nltk.download('punkt')
    from nltk import tokenize
    sent = 'Hace calor. De todas formas, no salgo a casa. Además, va a venir Peter. Sin embargo, no lo sé a qué hora llegará exactamente.'
    Ant = ['De todas formas', 'Sin embargo']
    sent = tokenize.sent_tokenize(sent)
    new=[]
    for i in sent:
        for DMAnt in Ant:
            if DMAnt in i:
                new.append('(N(' + str(sent[sent.index(i)-1]) + ')-Antithesis-' +'(S(' + str(sent[sent.index(i)]) + '))')
    
    print(new)   
    

    Output:

    ['(N(Hace calor.)-Antithesis-(S(De todas formas, no salgo a casa.))', '(N(Además, va a venir Peter.)-Antithesis-(S(Sin embargo, no lo sé a qué hora llegará exactamente.))']

    new variable will have your desirable output in the form of list.