-
Notifications
You must be signed in to change notification settings - Fork 52
feat: gets a link to the suitable image of a given event content. #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yammesicka
merged 30 commits into
PythonFreeCourse:develop
from
nir9696:feature/event_images
Jan 27, 2021
Merged
Changes from 5 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
07982fd
feat: gets a link to the suitable image of a given event content.
nir9696 74c9442
fix: correct minor typos in code
nir9696 3327d86
fix: correct minor typos in code
nir9696 7479db1
fix: change the flairs names to lemmatized named, discard IMAGES_LINK…
nir9696 2ecc875
fix: correct minor typos in code
nir9696 9eee27d
fix: correct minor typos in code
nir9696 faa42c9
fix: change the route of the feature test.
nir9696 a853198
fix: changed path of the program and the flair images.
nir9696 da3c582
fix: correct minor typos in code
nir9696 002b7ee
fix: correct minor typos in code
nir9696 a5b8361
Revert "fix: correct minor typos in code"
nir9696 8fe45d4
Revert "fix: correct minor typos in code"
nir9696 cb533f5
fix: correct minor typos in code
nir9696 13cdd3b
fix: correct minor typos in code
nir9696 ea0b7a4
fix: correct minor typos in code
nir9696 7fe1d3b
fix: update requirements.txt
nir9696 f770b4d
fix: correct minor typos in code
nir9696 c57fe57
fix: add config
nir9696 1c7a409
fix: correct minor typos in code
nir9696 3b79d09
Merge branch 'develop' into feature/event_images
nir9696 6cc9850
fix: add config.py.example
nir9696 3c83639
Merge branch 'feature/event_images' of https://github.com/nir9696/cal…
nir9696 26c7db2
Merge branch 'develop' into feature/event_images
nir9696 9491fed
fix: correct minor typos in code
nir9696 94c8a65
Merge branch 'feature/event_images' of https://github.com/nir9696/cal…
nir9696 8eb70d7
Merge branch 'develop' into feature/event_images
yammesicka e58433e
fix: update environment and thus requirements.txt
nir9696 24a70ef
fix: correct minor typos in code
nir9696 ca63267
fix: correct minor typos in code
nir9696 122962f
fix: correct minor typos in code
nir9696 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
This file contains hidden or 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,133 @@ | ||
| from nltk.tokenize import word_tokenize | ||
| from typing import Optional | ||
| from word_forms.lemmatizer import lemmatize | ||
|
|
||
| import re | ||
|
|
||
|
|
||
| FLAIRS_EXTENSION = '.jpg' | ||
| FLAIRS_REL_PATH = r'event_flairs' | ||
| IMAGES_RELATED_WORDS_MAP = { | ||
| 'birthday': ['birthday'], | ||
| 'coffee': ['coffee', 'coffees'], | ||
| 'concert': ['concert', 'gig', 'concerts', 'gigs'], | ||
| 'cycle': ['bicycle', 'cycling', 'bike', 'bicycles', 'bikes', 'Biking'], | ||
| 'dentist': ['dentist', 'dentistry', 'dental'], | ||
| 'food': ['dinner', 'dinners', 'restaurant', 'restaurants', | ||
| 'Family meal', 'lunch', 'lunches', 'luncheon'], | ||
| 'drank': ['cocktail', 'drinks', 'cocktails'], | ||
| 'golf': ['golf'], | ||
| 'graduate': ['graduation'], | ||
| 'gym': ['gym', 'workout', 'workouts'], | ||
| 'haircut': ['haircut', 'hair'], | ||
| 'halloween': ['halloween', 'helloween', "hallowe'en", | ||
| 'Allhalloween', "All Hallows' Eve", "All Saints' Eve"], | ||
| 'hike': ['hiking', 'hike', 'hikes'], | ||
| 'kayak': ['kayaking'], | ||
| 'music': ['piano', 'singing', 'music Class', 'choir practice', | ||
| 'flute', 'orchestra', 'oboe', 'clarinet', 'saxophone', | ||
| 'cornett', 'trumpet', 'contrabass', 'cello', 'trombone', | ||
| 'tuba', 'music ensemble', 'string quartett', | ||
| 'guitar lesson', 'classical music', 'choir'], | ||
| 'manicure': ['manicure', 'pedicure', 'manicures', 'pedicures'], | ||
| 'massage': ['massage', 'back rub', 'backrub', 'massages'], | ||
| 'pill': ['pills', 'medicines', 'medicine', 'drug', 'drugs'], | ||
| 'pingpong': ['ping pong', 'table tennis', 'ping-pong', 'pingpong'], | ||
| 'plan': ['plan week', 'plan quarter', 'plan day', 'plan vacation', | ||
| 'week planning', 'vacation planning'], | ||
| 'pokemon': ['pokemon'], | ||
| 'read': ['reading', 'newspaper'], | ||
| 'repair': ['fridge repair', 'handyman', 'electrician', 'DIY'], | ||
| 'ran': ['jog', 'jogging', 'running', 'jogs', 'runs'], | ||
| 'sail': ['sail', 'sailing', 'boat cruise', 'sailboat'], | ||
| 'santa': ['Santa Claus', 'Father Christmas'], | ||
| 'ski': ['skiing', 'ski', 'skis', 'Snowboarding', 'snowshoeing', | ||
| 'snow shoe', 'snow boarding'], | ||
| 'soccer': ['soccer'], | ||
| 'swam': ['swim', 'swimming', 'swims'], | ||
| 'tennis': ['tennis'], | ||
| 'thanksgiving': ['thanksgiving'], | ||
| 'wed': ['wedding', 'wedding eve', 'wedding-eve party', 'weddings'], | ||
| 'christmas': ['christmas', 'xmas', 'x-mas'], | ||
| 'yoga': ['yoga'], | ||
| } | ||
|
|
||
|
|
||
| def generate_flare_link_from_lemmatized_word(lemmatized_word: str) -> str: | ||
| """Generate a link to a flair by a given lemmatized word. | ||
|
|
||
| Args: | ||
| lemmatized_word (str): The lemmatized word. | ||
|
|
||
| Returns: | ||
| str: The suitable link. | ||
| """ | ||
| return f'{FLAIRS_REL_PATH}\\{lemmatized_word}{FLAIRS_EXTENSION}' | ||
|
|
||
|
|
||
| def remove_non_alphabet_chars(s: str) -> str: | ||
| """Remove non-alphabet chars from a given string | ||
|
|
||
| Args: | ||
| s (str): The string to remove the non-alphabet chars from. | ||
|
|
||
| Returns: | ||
| str: The string after the removal. | ||
| """ | ||
| regex = re.compile('[^a-zA-Z]') | ||
| return regex.sub('', s) | ||
|
|
||
|
|
||
| def get_key(val: str) -> Optional[str]: | ||
| """Search the key of a given value in IMAGES_RELATED_WORDS_MAP dictionary. | ||
|
|
||
| Args: | ||
| val (str): The value to search its key. | ||
|
|
||
| Returns: | ||
| str: The value's key in IMAGES_RELATED_WORDS_MAP dictionary.. | ||
| """ | ||
| shrunken = remove_non_alphabet_chars(val).lower() | ||
| for key, values in IMAGES_RELATED_WORDS_MAP.items(): | ||
nir9696 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| shrunken_list = [remove_non_alphabet_chars(v).lower() for v in values] | ||
nir9696 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if shrunken in shrunken_list: | ||
nir9696 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return key | ||
|
|
||
|
|
||
| def search_token_in_related_words(token: str) -> Optional[str]: | ||
nir9696 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """Search a token in IMAGES_RELATED_WORDS_MAP dictionary. | ||
|
|
||
| Args: | ||
| token (str): The token to search. | ||
|
|
||
| Returns: | ||
| str: The link to the suitable image of the given token. | ||
| """ | ||
| key = get_key(token) | ||
| if key: | ||
| return generate_flare_link_from_lemmatized_word(key) | ||
|
|
||
|
|
||
| def attach_image_to_event(event_content: str) -> str: | ||
| """Get a link to the suitable image of a given token content. | ||
|
|
||
| Args: | ||
| event_content (str): The event content. | ||
|
|
||
| Returns: | ||
| str: The link to the suitable image of a given token content. | ||
| """ | ||
| event_tokens = word_tokenize(event_content) | ||
| for token in event_tokens: | ||
| if re.match(r'\w', token): | ||
nir9696 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| try: | ||
| base_word = lemmatize(remove_non_alphabet_chars(token).lower()) | ||
nir9696 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| except ValueError: | ||
| base_word = token | ||
| if base_word in IMAGES_RELATED_WORDS_MAP.keys(): | ||
| return generate_flare_link_from_lemmatized_word(base_word) | ||
| link = search_token_in_related_words(token) | ||
| if link: | ||
| return link | ||
| link = '#' | ||
| return link | ||
Empty file.
This file contains hidden or 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,73 @@ | ||
| from event_images import attach_image_to_event, get_key,\ | ||
nir9696 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| generate_flare_link_from_lemmatized_word,\ | ||
| remove_non_alphabet_chars, search_token_in_related_words | ||
| import pytest | ||
|
|
||
|
|
||
| lemmatized_words = [ | ||
| ("ran", r'event_flairs\ran.jpg'), | ||
| ("food", r'event_flairs\food.jpg'), | ||
| ("i", r'event_flairs\i.jpg'), | ||
| ("drank", r'event_flairs\drank.jpg'), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('lemmatized, link', lemmatized_words) | ||
| def test_generate_flare_link_from_lemmatized_word(lemmatized, link): | ||
| assert generate_flare_link_from_lemmatized_word(lemmatized) == link | ||
|
|
||
|
|
||
| contents = [ | ||
| (r"it's my birthday!", r"itsmybirthday"), | ||
| (r"iT's my birthday!!!", r"iTsmybirthday"), | ||
| (r"its my birthday", r"itsmybirthday"), | ||
| (r"it's-my-birthday!1990", r"itsmybirthday"), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('content, alphabet_only', contents) | ||
| def test_remove_non_alphabet_chars(content, alphabet_only): | ||
| assert remove_non_alphabet_chars(content) == alphabet_only | ||
|
|
||
|
|
||
| values = [ | ||
| (r"backrub", r"massage"), | ||
| (r"--MedicineS", r"pill"), | ||
| (r"restaurants", r"food"), | ||
| (r"pikachu", None), | ||
| (r"Pokemon", r"pokemon"), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('value, key', values) | ||
| def test_get_key(value, key): | ||
| assert get_key(value) == key | ||
|
|
||
|
|
||
| tokens = [ | ||
| (r"backrub", r'event_flairs\massage.jpg'), | ||
| (r"--MedicineS", r'event_flairs\pill.jpg'), | ||
| (r"restaurants", r'event_flairs\food.jpg'), | ||
| (r"pikachu", None), | ||
| (r"Pokemon", r'event_flairs\pokemon.jpg'), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('token, link', tokens) | ||
| def test_search_token_in_related_words(token, link): | ||
| assert search_token_in_related_words(token) == link | ||
|
|
||
|
|
||
| event_contents = [ | ||
| (r"Don't forget backrub and medicines!!!!", r'event_flairs\massage.jpg'), | ||
| (r"Don't forget medicines and backrub!!!!", r'event_flairs\pill.jpg'), | ||
| (r"It's important to drink", r'event_flairs\drank.jpg'), | ||
| (r"call Jim about tennis on friday", r'event_flairs\tennis.jpg'), | ||
| (r"have to check on pikachu", r'#'), | ||
| (r"---~ new pokemon episode at 19:00 ~~!!", r'event_flairs\pokemon.jpg'), | ||
| ] | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('event_content, link', event_contents) | ||
| def test_attach_image_to_event(event_content, link): | ||
| assert attach_image_to_event(event_content) == link | ||
This file contains hidden or 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,7 @@ | ||
| pytest: | ||
nir9696 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| C:\Users\nir\Desktop\week16\project\tests>python -m pytest | ||
|
|
||
| coverage: | ||
|
|
||
| C:\Users\nir\Desktop\week16\project\tests>pytest --cov=event_images | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.