-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from AYLIEN/implement-semantic-filtering
adds semantic filters base implementation
- Loading branch information
Showing
4 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.7.0 | ||
0.7.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import Any | ||
|
||
|
||
class SemanticFilter: | ||
|
||
def __call__(self, item: Any) -> bool: | ||
raise NotImplementedError("Subclasses must implement this method") | ||
|
||
@property | ||
def name(self) -> str: | ||
return self.__class__.__name__ | ||
|
||
|
||
class StoryKeywordMatchFilter(SemanticFilter): | ||
|
||
def __init__(self, keywords: list[str]): | ||
self.keywords = keywords | ||
|
||
def __call__(self, item: dict) -> bool: | ||
return any(kw in item['title'] for kw in self.keywords) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import os | ||
from pathlib import Path | ||
|
||
from news_signals.test_signals import SignalTest | ||
from news_signals.semantic_filters import StoryKeywordMatchFilter | ||
from news_signals.log import create_logger | ||
|
||
|
||
logger = create_logger(__name__) | ||
|
||
path_to_file = Path(os.path.dirname(os.path.abspath(__file__))) | ||
resources = Path(os.environ.get( | ||
'RESOURCES', path_to_file / '../resources/test')) | ||
|
||
|
||
class TestFilterSignal(SignalTest): | ||
|
||
def test_filter_signal(self): | ||
example_signal = self.aylien_signals()[0] | ||
orig_stories_per_tick = [len(tick) for tick in example_signal['stories']] | ||
|
||
keywords = ['Million'] | ||
filter_model = StoryKeywordMatchFilter(keywords=keywords) | ||
filtered_signal = example_signal.filter_stories(filter_model=filter_model) | ||
filtered_stories_per_tick = [len(tick) for tick in filtered_signal['stories']] | ||
assert(sum(orig_stories_per_tick) > sum(filtered_stories_per_tick)) | ||
for tick_stories in filtered_signal['stories']: | ||
for s in tick_stories: | ||
assert any(kw in s['title'] for kw in keywords) | ||
|
||
# test don't delete filtered | ||
example_signal = self.aylien_signals()[0] | ||
filtered_signal = example_signal.filter_stories(filter_model=filter_model, delete_filtered=False) | ||
filtered_stories_per_tick = [len(tick) for tick in filtered_signal['stories']] | ||
assert(sum(orig_stories_per_tick) == sum(filtered_stories_per_tick)) | ||
|
||
|
||
|
||
|
||
|