0

我正在运行一个过滤故事并返回过滤故事数的函数。代码在下面 - 测试中的错误在下面。这些错误似乎只是错误地计算了过滤器故事的数量 - 简单的错误,但在尝试解决它时遇到了问题。过滤的故事数不匹配。请帮忙。

我的代码:

def filterStories(stories, triggerlist):

   newstories = []
   for story in stories:
       for trigger in triggerlist:
           if trigger.evaluate(story) == True:
               newstories.append(story)

       stories = newstories
   return stories

class WordTrigger(Trigger):
    def __init__(self,word):
        self.word = word.lower()

    def changeChars(self,text):
        final = ""
        for c in text:
            if c in string.punctuation:
                final += " "
            else:
                final += c
        return final

    def isWordIn(self,text):
        comp = self.changeChars(text)
        list = comp.lower().split(" ")
        return self.word in list

# TODO: TitleTrigger
class TitleTrigger(WordTrigger):
    def evaluate(self,story):
        return self.isWordIn(story.getTitle())

# TODO: SubjectTrigger
class SubjectTrigger(WordTrigger):
    def evaluate(self,story):
        return self.isWordIn(story.getSubject())

# TODO: SummaryTrigger
class SummaryTrigger(WordTrigger):
    def evaluate(self,story):
        return self.isWordIn(story.getSummary())

class PhraseTrigger(Trigger):
    def __init__(self,phrase):
        self.phrase = phrase

    def evaluate(self,story):
        return self.phrase in story.getTitle() or self.phrase in story.getSubject() or self.phrase in story.getSummary() 

-----------------来自测试的错误消息----------------------------- --------------------

Test: filterStories 3
Your output:
Trigger 1: TrueTrigger() - always returns True
Trigger 2: TrueTrigger() - always returns True
Story with title: "MIT and Russia Partner with Skolkovo Tech" and subject: "New school located near Moscow"
In filteredStories?  True
Story with title: "Russian dignitaries Visit USA" and subject: "Obama meets with Russian President Vladimir Putin"
In filteredStories?  True
Story with title: "Russia Overrun With Friendly Tigers" and subject: "Putin poses for photo op with cuddly felines"
In filteredStories?  True
Story with title: "Mitt Romney Defeated" and subject: "Obama edges win over Republican challenger Mitt Romney"
In filteredStories?  True
Number of filtered stories:  8
Story titles in the list of filtered stories your code generated:
['MIT and Russia Partner with Skolkovo Tech', 'MIT and Russia Partner with Skolkovo Tech', 'Mitt Romney Defeated', 'Mitt Romney Defeated', 'Russia Overrun With Friendly Tigers', 'Russia Overrun With Friendly Tigers', 'Russian dignitaries Visit USA', 'Russian dignitaries Visit USA']
Correct output:
Trigger 1: TrueTrigger() - always returns True
Trigger 2: TrueTrigger() - always returns True
Story with title: "MIT and Russia Partner with Skolkovo Tech" and subject: "New school located near Moscow"
In filteredStories?  True
Story with title: "Russian dignitaries Visit USA" and subject: "Obama meets with Russian President Vladimir Putin"
In filteredStories?  True
Story with title: "Russia Overrun With Friendly Tigers" and subject: "Putin poses for photo op with cuddly felines"
In filteredStories?  True
Story with title: "Mitt Romney Defeated" and subject: "Obama edges win over Republican challenger Mitt Romney"
In filteredStories?  True
Number of filtered stories:  4
Story titles in the list of filtered stories your code generated:
['MIT and Russia Partner with Skolkovo Tech', 'Mitt Romney Defeated', 'Russia Overrun With Friendly Tigers', 'Russian dignitaries Visit USA']
Test: filterStories 4
Your output:
Trigger 1: TitleTrigger("Russia")
Trigger 2: SubjectTrigger("Putin")
Trigger 3: TitleTrigger("MIT")
Story with title: "MIT and Russia Partner with Skolkovo Tech" and subject: "New school located near Moscow"
In filteredStories?  True
Story with title: "Russian dignitaries Visit USA" and subject: "Obama meets with Russian President Vladimir Putin"
In filteredStories?  True
Story with title: "Russia Overrun With Friendly Tigers" and subject: "Putin poses for photo op with cuddly felines"
In filteredStories?  True
Story with title: "Mitt Romney Defeated" and subject: "Obama edges win over Republican challenger Mitt Romney"
In filteredStories?  False
Number of filtered stories:  5
Story titles in the list of filtered stories your code generated:
['MIT and Russia Partner with Skolkovo Tech', 'MIT and Russia Partner with Skolkovo Tech', 'Russia Overrun With Friendly Tigers', 'Russia Overrun With Friendly Tigers', 'Russian dignitaries Visit USA']
Correct output:
Trigger 1: TitleTrigger("Russia")
Trigger 2: SubjectTrigger("Putin")
Trigger 3: TitleTrigger("MIT")
Story with title: "MIT and Russia Partner with Skolkovo Tech" and subject: "New school located near Moscow"
In filteredStories?  True
Story with title: "Russian dignitaries Visit USA" and subject: "Obama meets with Russian President Vladimir Putin"
In filteredStories?  True
Story with title: "Russia Overrun With Friendly Tigers" and subject: "Putin poses for photo op with cuddly felines"
In filteredStories?  True
Story with title: "Mitt Romney Defeated" and subject: "Obama edges win over Republican challenger Mitt Romney"
In filteredStories?  False
Number of filtered stories:  3
Story titles in the list of filtered stories your code generated:
['MIT and Russia Partner with Skolkovo Tech', 'Russia Overrun With Friendly Tigers', 'Russian dignitaries Visit USA']
4

1 回答 1

0

只需在您的函数filterstories中添加以下测试,然后再将您的故事插入filterlist

   for trigger in triggerlist:
       if trigger.evaluate(story) == True:
           if story not in newstories:
               newstories.append(story)

   stories = newstories

应该管用。

实际上,您正在复制导致测试失败的条目。

于 2013-04-01T16:43:07.493 回答