How to traverse the string by words or by letters?

Asked

Viewed 895 times

-3

Code:

while True:
    regras, numero_frases = map(int, input().split())
    if regras == 0 and numero_frases == 0:
        break
    dici = {}
    for r in range(regras):
        subs = input().replace(' ', '').split('->')
        dici[subs[0]] = subs[1]
        if subs[0] == 0 and subs[1] == 0:
            break
    for f in range(numero_frases):
        frase = input().split()
        for p in frase:
            if p in dici:
                frase[frase.index(p)] = dici[p]
        print(*frase)

Entree:

2 3 (número de regras e frases)
Rat -> Rato (substituições)
Rome -> Roma
O Rat roeu ( A saída deve ser a frase baseada na troca da regra)
a roupa do rei
de Rome
1 1
e -> i
e o vento levou
0 0

exit:

O Rato roeu
a roupa do rei
de Roma
i o vinto livou

The code corrects words or single letters, but I want it to also work with letters in the words, grateful!

  • I know the words get organized into trie and if any word in a sentence cannot be found in the distance Levenshtein to determine candidates for replacement within the trie. Now this way what is doing is against producent because in addition to inform the correct words you in thesis would have to inform all the possibility of spelling errors for each word inserted. Insert two substitution rules is simple the hard is to create a 1000 word dictionary.

1 answer

0


Well, I don’t understand exactly what your doubt is, but I believe that if you’re just going through the string for words, you can use the method "split()" for the string to become a word list. For example:

nossaString = "Olá, Marcio! Tudo bem, amigo?"

for j in nossaString.split():
    print(j)

Exit:

Olá,
Marcio!
Tudo
bem,
amigo?

If it is letter by letter, you can use the method "list()", for example:

nossaString = "Eu gosto de sorvete"

for i in list(nossaString):
    print(i)

Exit:

E
u

g
o
s
t
o

d
e

s
o
r
v
e
t
e

Browser other questions tagged

You are not signed in. Login or sign up in order to post.