python-查找前十个单词音节计数

发布于 2025-02-12 21:47:00 字数 2168 浏览 0 评论 0原文

我正在尝试完成一个文本文件的作业,然后计算每个单词中的音节数量,然后最终以最多的音节返回前10个单词。我相信我的大部分都会下降,但是我遇到了一个错误:

文件“ top_10_syllable_count.py”,第84行,在get_syllable_count_count_pair return(syllables(syllables(word),word),word,word,)typeerror:'module'模块'对象是不可call的。

这是我的代码:

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)

我相信我的问题与get_syllable_count_pair函数中的调用音节有关,但不确定如何更正它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜柠檬 2025-02-19 21:47:00

音节软件包根据文档具有一个函数。您会这​​样称呼它。

syllables.estimate(word)

您的代码就像是这样:

return (syllables.estimate(word), word, )

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, )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文