Skip to content

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
merged 30 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 Jan 17, 2021
74c9442
fix: correct minor typos in code
nir9696 Jan 17, 2021
3327d86
fix: correct minor typos in code
nir9696 Jan 17, 2021
7479db1
fix: change the flairs names to lemmatized named, discard IMAGES_LINK…
nir9696 Jan 17, 2021
2ecc875
fix: correct minor typos in code
nir9696 Jan 17, 2021
9eee27d
fix: correct minor typos in code
nir9696 Jan 18, 2021
faa42c9
fix: change the route of the feature test.
nir9696 Jan 18, 2021
a853198
fix: changed path of the program and the flair images.
nir9696 Jan 19, 2021
da3c582
fix: correct minor typos in code
nir9696 Jan 19, 2021
002b7ee
fix: correct minor typos in code
nir9696 Jan 19, 2021
a5b8361
Revert "fix: correct minor typos in code"
nir9696 Jan 19, 2021
8fe45d4
Revert "fix: correct minor typos in code"
nir9696 Jan 19, 2021
cb533f5
fix: correct minor typos in code
nir9696 Jan 19, 2021
13cdd3b
fix: correct minor typos in code
nir9696 Jan 19, 2021
ea0b7a4
fix: correct minor typos in code
nir9696 Jan 21, 2021
7fe1d3b
fix: update requirements.txt
nir9696 Jan 21, 2021
f770b4d
fix: correct minor typos in code
nir9696 Jan 21, 2021
c57fe57
fix: add config
nir9696 Jan 21, 2021
1c7a409
fix: correct minor typos in code
nir9696 Jan 22, 2021
3b79d09
Merge branch 'develop' into feature/event_images
nir9696 Jan 23, 2021
6cc9850
fix: add config.py.example
nir9696 Jan 24, 2021
3c83639
Merge branch 'feature/event_images' of https://github.com/nir9696/cal…
nir9696 Jan 24, 2021
26c7db2
Merge branch 'develop' into feature/event_images
nir9696 Jan 24, 2021
9491fed
fix: correct minor typos in code
nir9696 Jan 24, 2021
94c8a65
Merge branch 'feature/event_images' of https://github.com/nir9696/cal…
nir9696 Jan 24, 2021
8eb70d7
Merge branch 'develop' into feature/event_images
yammesicka Jan 26, 2021
e58433e
fix: update environment and thus requirements.txt
nir9696 Jan 26, 2021
24a70ef
fix: correct minor typos in code
nir9696 Jan 26, 2021
ca63267
fix: correct minor typos in code
nir9696 Jan 26, 2021
122962f
fix: correct minor typos in code
nir9696 Jan 26, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ email_conf = ConnectionConfig(
MAIL_SSL=False,
USE_CREDENTIALS=True,
)

# PATHS
STATIC_ABS_PATH = os.path.abspath("static")
4 changes: 4 additions & 0 deletions app/routers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import nltk


nltk.download('punkt')
217 changes: 217 additions & 0 deletions app/routers/event_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
from app import config
from functools import lru_cache
from nltk.tokenize import word_tokenize
from typing import Optional
from word_forms.lemmatizer import lemmatize

import re


FLAIRS_EXTENSION = '.jpg'
FLAIRS_REL_PATH = f'{config.STATIC_ABS_PATH}\\event_flairs'
IMAGES_RELATED_WORDS_MAP = {
'birthday': 'birthday',
'coffee': 'coffee',
'coffees': 'coffee',
'concert': 'concert',
'gig': 'concert',
'concerts': 'concert',
'gigs': 'concert',
'bicycle': 'cycle',
'cycling': 'cycle',
'bike': 'cycle',
'bicycles': 'cycle',
'bikes': 'cycle',
'biking': 'cycle',
'dentist': 'dentist',
'dentistry': 'dentist',
'dental': 'dentist',
'dinner': 'food',
'dinners': 'food',
'restaurant': 'food',
'restaurants': 'food',
'family meal': 'food',
'lunch': 'food',
'lunches': 'food',
'luncheon': 'food',
'cocktail': 'drank',
'drinks': 'drank',
'cocktails': 'drank',
'golf': 'golf',
'graduation': 'graduate',
'gym': 'gym',
'workout': 'gym',
'workouts': 'gym',
'haircut': 'haircut',
'hair': 'haircut',
'halloween': 'halloween',
'helloween': 'halloween',
"hallowe'en": 'halloween',
'allhalloween': 'halloween',
"all hallows' eve": 'halloween',
"all saints' Eve": 'halloween',
'hiking': 'hike',
'hike': 'hike',
'hikes': 'hike',
'kayaking': 'kayak',
'piano': 'music',
'singing': 'music',
'music class': 'music',
'choir practice': 'music',
'flute': 'music',
'orchestra': 'music',
'oboe': 'music',
'clarinet': 'music',
'saxophone': 'music',
'cornett': 'music',
'trumpet': 'music',
'contrabass': 'music',
'cello': 'music',
'trombone': 'music',
'tuba': 'music',
'music ensemble': 'music',
'string quartett': 'music',
'guitar lesson': 'music',
'classical music': 'music',
'choir': 'music',
'manicure': 'manicure',
'pedicure': 'manicure',
'manicures': 'manicure',
'pedicures': 'manicure',
'massage': 'massage',
'back rub': 'massage',
'backrub': 'massage',
'massages': 'massage',
'pills': 'pill',
'medicines': 'pill',
'medicine': 'pill',
'drug': 'pill',
'drugs': 'pill',
'ping pong': 'pingpong',
'table tennis': 'pingpong',
'ping-pong': 'pingpong',
'pingpong': 'pingpong',
'plan week': 'plan',
'plan quarter': 'plan',
'plan day': 'plan',
'plan vacation': 'plan',
'week planning': 'plan',
'vacation planning': 'plan',
'pokemon': 'pokemon',
'reading': 'read',
'newspaper': 'read',
'fridge repair': 'repair',
'handyman': 'repair',
'electrician': 'repair',
'diy': 'repair',
'jog': 'ran',
'jogging': 'ran',
'running': 'ran',
'jogs': 'ran',
'runs': 'ran',
'sail': 'sail',
'sailing': 'sail',
'boat cruise': 'sail',
'sailboat': 'sail',
'santa claus': 'santa',
'father christmas': 'santa',
'skiing': 'ski',
'ski': 'ski',
'skis': 'ski',
'snowboarding': 'ski',
'snowshoeing': 'ski',
'snow shoe': 'ski',
'snow boarding': 'ski',
'soccer': 'soccer',
'swim': 'swam',
'swimming': 'swam',
'swims': 'swam',
'tennis': 'tennis',
'thanksgiving': 'thanksgiving',
'wedding': 'wed',
'wedding eve': 'wed',
'wedding-eve party': 'wed',
'weddings': 'wed',
'christmas': 'christmas',
'xmas': 'christmas',
'x-mas': 'christmas',
'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(text: str) -> str:
"""Remove non-alphabet chars from a given string

Args:
text (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('', text)


def get_image_name(related_word: str) -> Optional[str]:
"""Search the key of a given value in IMAGES_RELATED_WORDS_MAP dictionary.

Args:
related_word (str): The value to search its key.

Returns:
str: The value's key in IMAGES_RELATED_WORDS_MAP dictionary.
"""
shrunken = remove_non_alphabet_chars(related_word).lower()
return IMAGES_RELATED_WORDS_MAP.get(shrunken)


@lru_cache(maxsize=32)
def search_token_in_related_words(token: str) -> Optional[str]:
"""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_image_name(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 token.isalnum():
try:
base_word = lemmatize(remove_non_alphabet_chars(token).lower())
except ValueError:
base_word = token
if base_word in IMAGES_RELATED_WORDS_MAP.values():
return generate_flare_link_from_lemmatized_word(base_word)
link = search_token_in_related_words(token)
if link:
return link
link = '#'
return link
Binary file added app/static/event_flairs/birthday.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/christmas.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/coffee.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/concert.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/cycle.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/dentist.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/drank.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/food.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/golf.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/graduate.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/gym.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/haircut.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/halloween.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/hike.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/kayak.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/manicure.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/massage.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/music.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/pill.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/pingpong.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/plan.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/pokemon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/ran.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/read.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/repair.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/event_flairs/sail.jpg
Binary file added app/static/event_flairs/santa.jpg
Binary file added app/static/event_flairs/ski.jpg
Binary file added app/static/event_flairs/soccer.jpg
Binary file added app/static/event_flairs/swam.jpg
Binary file added app/static/event_flairs/tennis.jpg
Binary file added app/static/event_flairs/thanksgiving.jpg
Binary file added app/static/event_flairs/wed.jpg
Binary file added app/static/event_flairs/yoga.jpg
30 changes: 27 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
aiofiles==0.6.0
aioredis==1.3.1
aiosmtpd==1.2.2
aiosmtplib==1.1.4
apipkg==1.5
arrow==0.17.0
async-timeout==3.0.1
atomicwrites==1.4.0
atpublic==2.1.2
attrs==20.3.0
beautifulsoup4==4.9.3
blinker==1.4
certifi==2020.12.5
chardet==4.0.0
click==7.1.2
colorama==0.4.4
coverage==5.3.1
dnspython==2.1.0
email-validator==1.1.2
execnet==1.7.1
Faker==5.6.2
fakeredis==1.4.5
fastapi==0.63.0
fastapi_mail==0.3.3.1
faker==5.6.2
fastapi-mail==0.3.3.1
frozendict==1.2
smtpdfix==0.2.6
h11==0.12.0
h2==4.0.0
hiredis==1.1.0
hpack==4.0.0
httpcore==0.12.2
httpx==0.16.1
hyperframe==6.0.0
icalendar==4.0.7
idna==2.10
importlib-metadata==3.3.0
inflect==4.1.0
iniconfig==1.1.1
Jinja2==2.11.2
joblib==1.0.0
lazy-object-proxy==1.5.2
MarkupSafe==1.1.1
nltk==3.5
packaging==20.8
Pillow==8.1.0
pluggy==0.13.1
Expand All @@ -42,18 +57,27 @@ python-dotenv==0.15.0
python-multipart==0.0.5
pytz==2020.5
PyYAML==5.3.1
redis==3.5.3
regex==2020.11.13
requests==2.25.1
requests-mock==1.8.0
responses==0.12.1
rfc3986==1.4.0
six==1.15.0
smtpdfix==0.2.6
sniffio==1.2.0
sortedcontainers==2.3.0
soupsieve==2.1
SQLAlchemy==1.3.22
starlette==0.13.6
text-unidecode==1.3
toml==0.10.2
tqdm==4.56.0
typing-extensions==3.7.4.3
urllib3==1.26.2
uvicorn==0.13.3
watchgod==0.6
websockets==8.1
word-forms==2.1.0
wsproto==1.0.0
zipp==3.4.0
77 changes: 77 additions & 0 deletions tests/test_event_images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from app.routers.event_images import attach_image_to_event,\
generate_flare_link_from_lemmatized_word, get_image_name,\
remove_non_alphabet_chars, search_token_in_related_words
from app import config
import pytest


static = config.STATIC_ABS_PATH


lemmatized_words = [
("ran", f'{static}\\event_flairs\\ran.jpg'),
("food", f'{static}\\event_flairs\\food.jpg'),
("i", f'{static}\\event_flairs\\i.jpg'),
("drank", f'{static}\\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('related_word, key', values)
def test_get_image_name(related_word, key):
assert get_image_name(related_word) == key


tokens = [
(r"backrub", f'{static}\\event_flairs\\massage.jpg'),
(r"--MedicineS", f'{static}\\event_flairs\\pill.jpg'),
(r"restaurants", f'{static}\\event_flairs\\food.jpg'),
(r"pikachu", None),
(r"Pokemon", f'{static}\\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"memo backrub and medicines!!", f'{static}\\event_flairs\\massage.jpg'),
(r"Dont forget medicines & backrub!", f'{static}\\event_flairs\\pill.jpg'),
(r"Its important to drink", f'{static}\\event_flairs\\drank.jpg'),
(r"call Jim about tennis friday", f'{static}\\event_flairs\\tennis.jpg'),
(r"have to check on pikachu", r'#'),
(r"-~new pokemon episode 19:00~!", f'{static}\\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