繁体   English   中英

Python - 查找音节数排名前十的单词

[英]Python - Finding Top Ten Words Syllable Count

我正在尝试制作一个接收文本文件的工作,然后计算每个单词中的音节数,然后最终返回音节最多的前 10 个单词。 我相信我已经完成了大部分工作,但是我收到了一个错误:

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

这是我的代码:

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 function 中的音节有关,但不知道如何纠正它。

根据文档, syllables package 有一个 function。 你会这样称呼它。

syllables.estimate(word)

你的代码会是这样的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM