Python Forum
function for a single string number of syllables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function for a single string number of syllables
#1
I have a function who takes a single string as argument and returns the number of syllables of some string e.g. str = "I eat pancakes"
But i do not get the correct number of syllables what do I do wrong here? 
I use regular expression module and already got a pattern that describes the syllables in some text
def count_syllables(str):
    pattern = re.compile("[aiouy]+e*|e(?!d\b|ly)[aiouye]?|[td]ed|le\b")

    match_new = pattern.match(str.lower())
    if match_new:
        print len(string)
Reply
#2
Please make sure to post your real code and its desired vs actual output; this code results in a NameError, which doesn't seem to be your question.

As to solving your problem, you need to start simple and build from there. Handle the simple case, don't worry about y. Verify that that works, create tests cases that correctly pass and fail. Once you're confident about that, then create test cases for y and modify your code to handle those cases, while also continuing to handle the old tests as well.
Reply
#3
You may want to look at the tokenize section of NLTK here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Extracting Version Number from a String britesc 2 1,105 May-31-2023, 10:20 AM
Last Post: britesc
  Need help on how to include single quotes on data of variable string hani_hms 5 2,059 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  python sql query single quote in a string mg24 1 1,083 Nov-18-2022, 08:01 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,893 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,558 May-13-2022, 07:28 PM
Last Post: mikepy
  Checking the number of arguments a function takes Chirumer 3 2,170 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,231 May-02-2021, 03:45 PM
Last Post: Anldra12
  Parse String between 2 Delimiters and add as single list items lastyle 5 3,406 Apr-11-2021, 11:03 PM
Last Post: lastyle
  cursor.execute: How to insert dynamic number in a string? stoeberhai 2 3,534 Mar-18-2021, 12:55 PM
Last Post: stoeberhai
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,433 Jan-15-2021, 04:39 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020