sxgbln
sxgbln

Reputation: 1

Check for substrings in sequential order using python

I’m currently developing a Blender Add-on that is a lip-sync tool for 2D and 3D animations, and this Add-on includes a Phoneme extractor tool that extracts phonemes from each word.

for example, the sentence I love pizza which is aɪ lʌv ˈpiːtsə. That’s the reason why I’m making a script that will evaluate each character looking for phonemes in each word (there are like 44 phonemes or something). But to put it simply:

say you have string = bcda

I need something like

*b detected, do something
*c detected, do something
*d detected, do something
*a detected, do something

and in case it is string = abcd

*a detected, do something
*b detected, do something
*c detected, do something
*d detected, do something

But whatever I do in python I always get abcd and I need the sequential order! And it’s even worst because I tried doing this in c# and I did succeed (and I tried using regex, text1 in text2 and .find)

text2 = "bca"

aString = "a"
bString = "b"
cString = "c"

if aString in text2:
    print("contains a")
if bString in text2:
    print("contains b")
if cString in text2:
    print("contains c")

I tried using .find, using text1 in text2, and even using regex and it works, but not in sequential order

Upvotes: 0

Views: 78

Answers (1)

35308
35308

Reputation: 652

You could do this just by looping through the string with a forloop and just having a massive switch statment. You could also have a dictionary of phonemes and their acording functions.

aString = "bca"
"""
it got a bit convoluted but the lambda: print("contains a") really just allows you
to call a function (print) with specific attributes, if you wanted your own
function you probably wouldn't need the lambda.
"""
functions = {"a":lambda: print("contains a"),"b":lambda: print("contains b"),"c":lambda: print("contains c")}

for c in aString: # just loops through each letter and calls the according function found in the dictionary.
    functions[c]() 

which produces:

contains b
contains c
contains a

If your looking for finding multiple letter occurences here's an approach:

aString = "ac" 
bString = "ba" 
cString = "cb" 
dString = "cba" 
sampleStr = "bcbabac" 
print("**** Iterate over each character using for loop****") 
for elem in range(len(sampleStr)): # Here rather than looping through the characters we loop through the index of each characters
    """
    To break down the if statments:
    The sampleStr[a:b] just takes a substring of sampleStr from a-b (not including b)
    Here the range is from elem to elem+len(aString) (so a substring the size of the phonemes) 
    And then the final min() just avoids situations where the phonemes would be too long and wrap back around.
    So in all we're looping through the String and taking substrings of the size of the phonemes we're comparing it to.
    """
    if aString == sampleStr[elem:min(elem+len(aString),len(sampleStr))]:
        print("detected " + sampleStr[elem:elem+len(aString)])
    if bString == sampleStr[elem:min(elem+len(bString),len(sampleStr))]: 
        print("detected " + sampleStr[elem:elem+len(bString)])
    if cString == sampleStr[elem:min(elem+len(cString),len(sampleStr))]:
        print("detected " + sampleStr[elem:elem+len(cString)]) 
    if dString == sampleStr[elem:min(elem+len(dString),len(sampleStr))]:
        print("detected " + sampleStr[elem:elem+len(dString)]) 

which produces:

**** Iterate over each character using for loop****
detected cb
detected cba
detected ba
detected ba
detected ac

Upvotes: 0

Related Questions