1

I am trying to make a job that takes in a text file, then counts the number of syllables in each word, then ultimately returns the top 10 words with the most syllables. I believe I have most of it down, but I am getting an error:

File "top_10_syllable_count.py", line 84, in get_syllable_count_pair return (syllables(word), word, ) TypeError: 'module' object is not callable.

Here is my code:

import re
from sys import stderr
from mrjob.job import MRJob
from mrjob.step import MRStep
WORD_RE = re.compile(r"[\w']+")

import syllables

class MRMostUsedWordSyllables(MRJob):    
    
    def steps(self):
        return [
            MRStep(mapper=self.word_splitter_mapper,
                   reducer=self.sorting_word_syllables),
            MRStep(mapper=self.get_syllable_count_pair),
            MRStep(reducer=self.get_top_10_reducer)
        ]
    
    def word_splitter_mapper(self, _, line):
        #for word in line.split():
        for word in WORD_RE.findall(line):
            yield(word.lower(), None)
        
    def sorting_word_syllables(self, word, count):
        count = 0
        vowels = 'aeiouy'
        word = word.lower().strip()
        if word in vowels:
            count +=1
        for index in range(1,len(word)):
            if word[index] in vowels and word[index-1] not in vowels:
                count +=1
        if word.endswith('e'):
            count -= 1
        if word.endswith('le'):
            count+=1
        if count == 0:
            count +=1
        yield None, (int(count), word)
        
        
        
    def get_syllable_count_pair(self, _, word):
        return (syllables(word), word, )
                
    def get_top_10_reducer(self, count, word):
        assert count == None  # added for a guard
        with_counts = [get_syllable_count_pair(w) for w in word]
        # Sort the words by the syllable count
        sorted_counts = sorted(syllables_counts, reverse=True, key=lambda x: x[0])
        # Slice off the first ten
        for t in sorted_counts[:10]: 
            yield t
            

if __name__ == '__main__':
    import time
    start = time.time()
    MRMostUsedWordSyllables.run()
    end = time.time()
    print(end - start)

I believe my issue has to do with calling syllables in the get_syllable_count_pair function, but not sure how to correct it.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Tony M
  • 13
  • 4
  • You never declared `syllables` anywhere as a function or a method. On a side note, take a look at https://pypi.org/project/pyflakes/ and https://pypi.org/project/pycodestyle/ These are called "linters". You should run your code through these two linters before anything else. If you're using an IDE, most of the decent ones can plugin these two linters so that you get a warning from the linter right as soon as you type something that is an error. – Utkonos Jul 02 '22 at 22:29
  • @Utkonos Thank you, I will try those linters. For syllables, I guess I am trying to return the syllable count along with the word. I thought I was calling the syllable package that estimates the syllables in a word. Do you have any suggestion for implementing this? – Tony M Jul 02 '22 at 22:57
  • I missed that import at the top of your code somehow. I have provided an answer that should fix your problem. Take a look. – Utkonos Jul 02 '22 at 23:38
  • Ahh yes, that makes sense. For some reason I'm getting an error now " File "/opt/conda/lib/python3.7/re.py", line 215, in split return _compile(pattern, flags).split(string, maxsplit) TypeError: expected string or bytes-like object" – Tony M Jul 03 '22 at 01:57
  • That is a different question than you asked above. I would create a new question and ask separately. Also, before you ask, try using `pdb` to drop into the python debugger. You will be able to examine the contents of all the variables around a line of code. To fix type errors, you want to use `print(type(something))` where `something` is the object that you want to determine the type of. To drop into the debugger, just add `import pdb; pdb.set_trace()` right before the line of code you want to examine. Documentation on how to use the debugger is here: https://docs.python.org/3/library/pdb.html – Utkonos Jul 04 '22 at 01:37

1 Answers1

1

The syllables package has one function according to the documentation. You would call it like so.

syllables.estimate(word)

Your code would be like so:

return (syllables.estimate(word), word, )
Utkonos
  • 631
  • 6
  • 21