Count number of syllables with LUA

0

I have a txt file that contains the following: pumpkin dices pencil harvest. I want to perform a function that tells me the number of syllables that each word in that text file has. How can I read the letter a of the alphabet again?

I have the following lines of code:

     local ejer = io.open("archivo.txt","r")
     local mejer={}
     local i=1
     for line in ejer:lines() do 
        mejer[i] = line
        i=i+1
      end
      print(mejer[1])


    abecedario={ "a", "e", "i", "o","u","á", "é", "í", "ó","ú","b","c","d","e","f","g","h","i","j","k","l","m","n","ñ","o","p","q","r","s","t","v","w","x","y","z"}  
    silabas={}
    local misilaba=""

    local m=1
    repeat
         local x=1
         local y=1
         local j=1
         repeat
            local letra=abecedario[x]
            local encontrar = string.find(mejer[y],letra)
            if (encontrar==m)then
                 silabas[j]=letra
                 misilaba=misilaba..silabas[j]
                 print(misilaba)
           end
           x=x+1
        until x==36
       m=m+1
       until m>12

with these lines I get the following result:

  • ca-la-ba-za
  • c
  • c a
  • c a l
  • c a l b
  • c a l b z
  • asked by Estefani_Sanchez 04.06.2017 в 21:48
    source

    1 answer

    0

    I do not know if the words come with the format you specify at the beginning of your post "ca-rro" in any case there would be a simple way if that were the case:

    local palabras = {"ca-la-ba-za", "da-dos", "lá-piz", "co-se-cha"}
    
    for _, pal in pairs(palabras) do
      print(pal..":",pal:len() - string.len(pal:gsub('[%p%c%s]', '')) + 1)
    end
    
    --[[
        Se imprime:
        ca-la-ba-za:    4       
        da-dos: 2       
        lá-piz: 2       
        co-se-cha:  3
    ]]
    

    Basically what pal:len() does is count the number of characters the word contained in the palabras has during the for loop, while the expression string.len(pal:gsub('[%p%c%s]', '')) removes the special characters "-" from the word and then its characters are counted with the function string.len() at the end the "+1" is added because a hyphen is needed to indicate two syllables.

        
    answered by 29.05.2018 в 15:02