Skip to content

Commit eea0a99

Browse files
authored
Merge branch 'develop' into feature/update_event
2 parents 97d6929 + b119281 commit eea0a99

39 files changed

+311
-4
lines changed

app/config.py.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,6 @@ email_conf = ConnectionConfig(
3333
MAIL_SSL=False,
3434
USE_CREDENTIALS=True,
3535
)
36+
37+
# PATHS
38+
STATIC_ABS_PATH = os.path.abspath("static")

app/routers/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import nltk
2+
3+
4+
nltk.download('punkt')

app/routers/event_images.py

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
from app import config
2+
from functools import lru_cache
3+
from nltk.tokenize import word_tokenize
4+
from typing import Optional
5+
from word_forms.lemmatizer import lemmatize
6+
7+
import re
8+
9+
10+
FLAIRS_EXTENSION = '.jpg'
11+
FLAIRS_REL_PATH = f'{config.STATIC_ABS_PATH}\\event_flairs'
12+
IMAGES_RELATED_WORDS_MAP = {
13+
'birthday': 'birthday',
14+
'coffee': 'coffee',
15+
'coffees': 'coffee',
16+
'concert': 'concert',
17+
'gig': 'concert',
18+
'concerts': 'concert',
19+
'gigs': 'concert',
20+
'bicycle': 'cycle',
21+
'cycling': 'cycle',
22+
'bike': 'cycle',
23+
'bicycles': 'cycle',
24+
'bikes': 'cycle',
25+
'biking': 'cycle',
26+
'dentist': 'dentist',
27+
'dentistry': 'dentist',
28+
'dental': 'dentist',
29+
'dinner': 'food',
30+
'dinners': 'food',
31+
'restaurant': 'food',
32+
'restaurants': 'food',
33+
'family meal': 'food',
34+
'lunch': 'food',
35+
'lunches': 'food',
36+
'luncheon': 'food',
37+
'cocktail': 'drank',
38+
'drinks': 'drank',
39+
'cocktails': 'drank',
40+
'golf': 'golf',
41+
'graduation': 'graduate',
42+
'gym': 'gym',
43+
'workout': 'gym',
44+
'workouts': 'gym',
45+
'haircut': 'haircut',
46+
'hair': 'haircut',
47+
'halloween': 'halloween',
48+
'helloween': 'halloween',
49+
"hallowe'en": 'halloween',
50+
'allhalloween': 'halloween',
51+
"all hallows' eve": 'halloween',
52+
"all saints' Eve": 'halloween',
53+
'hiking': 'hike',
54+
'hike': 'hike',
55+
'hikes': 'hike',
56+
'kayaking': 'kayak',
57+
'piano': 'music',
58+
'singing': 'music',
59+
'music class': 'music',
60+
'choir practice': 'music',
61+
'flute': 'music',
62+
'orchestra': 'music',
63+
'oboe': 'music',
64+
'clarinet': 'music',
65+
'saxophone': 'music',
66+
'cornett': 'music',
67+
'trumpet': 'music',
68+
'contrabass': 'music',
69+
'cello': 'music',
70+
'trombone': 'music',
71+
'tuba': 'music',
72+
'music ensemble': 'music',
73+
'string quartett': 'music',
74+
'guitar lesson': 'music',
75+
'classical music': 'music',
76+
'choir': 'music',
77+
'manicure': 'manicure',
78+
'pedicure': 'manicure',
79+
'manicures': 'manicure',
80+
'pedicures': 'manicure',
81+
'massage': 'massage',
82+
'back rub': 'massage',
83+
'backrub': 'massage',
84+
'massages': 'massage',
85+
'pills': 'pill',
86+
'medicines': 'pill',
87+
'medicine': 'pill',
88+
'drug': 'pill',
89+
'drugs': 'pill',
90+
'ping pong': 'pingpong',
91+
'table tennis': 'pingpong',
92+
'ping-pong': 'pingpong',
93+
'pingpong': 'pingpong',
94+
'plan week': 'plan',
95+
'plan quarter': 'plan',
96+
'plan day': 'plan',
97+
'plan vacation': 'plan',
98+
'week planning': 'plan',
99+
'vacation planning': 'plan',
100+
'pokemon': 'pokemon',
101+
'reading': 'read',
102+
'newspaper': 'read',
103+
'fridge repair': 'repair',
104+
'handyman': 'repair',
105+
'electrician': 'repair',
106+
'diy': 'repair',
107+
'jog': 'ran',
108+
'jogging': 'ran',
109+
'running': 'ran',
110+
'jogs': 'ran',
111+
'runs': 'ran',
112+
'sail': 'sail',
113+
'sailing': 'sail',
114+
'boat cruise': 'sail',
115+
'sailboat': 'sail',
116+
'santa claus': 'santa',
117+
'father christmas': 'santa',
118+
'skiing': 'ski',
119+
'ski': 'ski',
120+
'skis': 'ski',
121+
'snowboarding': 'ski',
122+
'snowshoeing': 'ski',
123+
'snow shoe': 'ski',
124+
'snow boarding': 'ski',
125+
'soccer': 'soccer',
126+
'swim': 'swam',
127+
'swimming': 'swam',
128+
'swims': 'swam',
129+
'tennis': 'tennis',
130+
'thanksgiving': 'thanksgiving',
131+
'wedding': 'wed',
132+
'wedding eve': 'wed',
133+
'wedding-eve party': 'wed',
134+
'weddings': 'wed',
135+
'christmas': 'christmas',
136+
'xmas': 'christmas',
137+
'x-mas': 'christmas',
138+
'yoga': 'yoga',
139+
}
140+
141+
142+
def generate_flare_link_from_lemmatized_word(lemmatized_word: str) -> str:
143+
"""Generate a link to a flair by a given lemmatized word.
144+
145+
Args:
146+
lemmatized_word (str): The lemmatized word.
147+
148+
Returns:
149+
str: The suitable link.
150+
"""
151+
return f'{FLAIRS_REL_PATH}\\{lemmatized_word}{FLAIRS_EXTENSION}'
152+
153+
154+
def remove_non_alphabet_chars(text: str) -> str:
155+
"""Remove non-alphabet chars from a given string
156+
157+
Args:
158+
text (str): The string to remove the non-alphabet chars from.
159+
160+
Returns:
161+
str: The string after the removal.
162+
"""
163+
regex = re.compile('[^a-zA-Z]')
164+
return regex.sub('', text)
165+
166+
167+
def get_image_name(related_word: str) -> Optional[str]:
168+
"""Search the key of a given value in IMAGES_RELATED_WORDS_MAP dictionary.
169+
170+
Args:
171+
related_word (str): The value to search its key.
172+
173+
Returns:
174+
str: The value's key in IMAGES_RELATED_WORDS_MAP dictionary.
175+
"""
176+
shrunken = remove_non_alphabet_chars(related_word).lower()
177+
return IMAGES_RELATED_WORDS_MAP.get(shrunken)
178+
179+
180+
@lru_cache(maxsize=32)
181+
def search_token_in_related_words(token: str) -> Optional[str]:
182+
"""Search a token in IMAGES_RELATED_WORDS_MAP dictionary.
183+
184+
Args:
185+
token (str): The token to search.
186+
187+
Returns:
188+
str: The link to the suitable image of the given token.
189+
"""
190+
key = get_image_name(token)
191+
if key:
192+
return generate_flare_link_from_lemmatized_word(key)
193+
194+
195+
def attach_image_to_event(event_content: str) -> str:
196+
"""Get a link to the suitable image of a given token content.
197+
198+
Args:
199+
event_content (str): The event content.
200+
201+
Returns:
202+
str: The link to the suitable image of a given token content.
203+
"""
204+
event_tokens = word_tokenize(event_content)
205+
for token in event_tokens:
206+
if token.isalnum():
207+
try:
208+
base_word = lemmatize(remove_non_alphabet_chars(token).lower())
209+
except ValueError:
210+
base_word = token
211+
if base_word in IMAGES_RELATED_WORDS_MAP.values():
212+
return generate_flare_link_from_lemmatized_word(base_word)
213+
link = search_token_in_related_words(token)
214+
if link:
215+
return link
216+
link = '#'
217+
return link

app/static/event_flairs/birthday.jpg

30.9 KB
Loading

app/static/event_flairs/christmas.jpg

15.7 KB
Loading

app/static/event_flairs/coffee.jpg

15.8 KB
Loading

app/static/event_flairs/concert.jpg

51.7 KB
Loading

app/static/event_flairs/cycle.jpg

38.2 KB
Loading

app/static/event_flairs/dentist.jpg

23.3 KB
Loading

app/static/event_flairs/drank.jpg

38.5 KB
Loading

app/static/event_flairs/food.jpg

107 KB
Loading

app/static/event_flairs/golf.jpg

48.1 KB
Loading

app/static/event_flairs/graduate.jpg

8.37 KB
Loading

app/static/event_flairs/gym.jpg

57 KB
Loading

app/static/event_flairs/haircut.jpg

41.1 KB
Loading

app/static/event_flairs/halloween.jpg

120 KB
Loading

app/static/event_flairs/hike.jpg

54.8 KB
Loading

app/static/event_flairs/kayak.jpg

95.5 KB
Loading

app/static/event_flairs/manicure.jpg

38 KB
Loading

app/static/event_flairs/massage.jpg

32.7 KB
Loading

app/static/event_flairs/music.jpg

187 KB
Loading

app/static/event_flairs/pill.jpg

69.3 KB
Loading

app/static/event_flairs/pingpong.jpg

83.8 KB
Loading

app/static/event_flairs/plan.jpg

30.6 KB
Loading

app/static/event_flairs/pokemon.jpg

59 KB
Loading

app/static/event_flairs/ran.jpg

88.9 KB
Loading

app/static/event_flairs/read.jpg

29.5 KB
Loading

app/static/event_flairs/repair.jpg

23.1 KB
Loading

app/static/event_flairs/sail.jpg

172 KB
Loading

app/static/event_flairs/santa.jpg

37.4 KB
Loading

app/static/event_flairs/ski.jpg

65.3 KB
Loading

app/static/event_flairs/soccer.jpg

48.2 KB
Loading

app/static/event_flairs/swam.jpg

10.6 KB
Loading

app/static/event_flairs/tennis.jpg

42.5 KB
Loading
20.7 KB
Loading

app/static/event_flairs/wed.jpg

27.3 KB
Loading

app/static/event_flairs/yoga.jpg

29.5 KB
Loading

requirements.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ execnet==1.7.1
2121
Faker==5.6.2
2222
fakeredis==1.4.5
2323
fastapi==0.63.0
24-
fastapi_mail==0.3.3.1
25-
faker==5.6.2
24+
fastapi-mail==0.3.3.1
2625
frozendict==1.2
27-
smtpdfix==0.2.6
2826
h11==0.12.0
2927
h2==4.0.0
3028
hiredis==1.1.0
@@ -35,12 +33,15 @@ hyperframe==6.0.0
3533
icalendar==4.0.7
3634
idna==2.10
3735
importlib-metadata==3.3.0
36+
inflect==4.1.0
3837
iniconfig==1.1.1
3938
Jinja2==2.11.2
39+
joblib==1.0.0
4040
lazy-object-proxy==1.5.2
41-
MarkupSafe==1.1.1
4241
mypy==0.790
4342
mypy-extensions==0.4.3
43+
MarkupSafe==1.1.1
44+
nltk==3.5
4445
packaging==20.8
4546
Pillow==8.1.0
4647
pluggy==0.13.1
@@ -59,23 +60,28 @@ python-multipart==0.0.5
5960
pytz==2020.5
6061
PyYAML==5.3.1
6162
redis==3.5.3
63+
regex==2020.11.13
6264
requests==2.25.1
6365
requests-mock==1.8.0
6466
responses==0.12.1
67+
rfc3986==1.4.0
6568
six==1.15.0
6669
smtpdfix==0.2.6
6770
sniffio==1.2.0
71+
sortedcontainers==2.3.0
6872
soupsieve==2.1
6973
sortedcontainers==2.3.0
7074
SQLAlchemy==1.3.22
7175
starlette==0.13.6
7276
text-unidecode==1.3
7377
toml==0.10.2
78+
tqdm==4.56.0
7479
typed-ast==1.4.2
7580
typing-extensions==3.7.4.3
7681
urllib3==1.26.2
7782
uvicorn==0.13.3
7883
watchgod==0.6
7984
websockets==8.1
85+
word-forms==2.1.0
8086
wsproto==1.0.0
8187
zipp==3.4.0

tests/test_event_images.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from app.routers.event_images import attach_image_to_event,\
2+
generate_flare_link_from_lemmatized_word, get_image_name,\
3+
remove_non_alphabet_chars, search_token_in_related_words
4+
from app import config
5+
import pytest
6+
7+
8+
static = config.STATIC_ABS_PATH
9+
10+
11+
lemmatized_words = [
12+
("ran", f'{static}\\event_flairs\\ran.jpg'),
13+
("food", f'{static}\\event_flairs\\food.jpg'),
14+
("i", f'{static}\\event_flairs\\i.jpg'),
15+
("drank", f'{static}\\event_flairs\\drank.jpg'),
16+
]
17+
18+
19+
@pytest.mark.parametrize('lemmatized, link', lemmatized_words)
20+
def test_generate_flare_link_from_lemmatized_word(lemmatized, link):
21+
assert generate_flare_link_from_lemmatized_word(lemmatized) == link
22+
23+
24+
contents = [
25+
(r"it's my birthday!", r"itsmybirthday"),
26+
(r"iT's my birthday!!!", r"iTsmybirthday"),
27+
(r"its my birthday", r"itsmybirthday"),
28+
(r"it's-my-birthday!1990", r"itsmybirthday"),
29+
]
30+
31+
32+
@pytest.mark.parametrize('content, alphabet_only', contents)
33+
def test_remove_non_alphabet_chars(content, alphabet_only):
34+
assert remove_non_alphabet_chars(content) == alphabet_only
35+
36+
37+
values = [
38+
(r"backrub", r"massage"),
39+
(r"--MedicineS", r"pill"),
40+
(r"restaurants", r"food"),
41+
(r"pikachu", None),
42+
(r"Pokemon", r"pokemon"),
43+
]
44+
45+
46+
@pytest.mark.parametrize('related_word, key', values)
47+
def test_get_image_name(related_word, key):
48+
assert get_image_name(related_word) == key
49+
50+
51+
tokens = [
52+
(r"backrub", f'{static}\\event_flairs\\massage.jpg'),
53+
(r"--MedicineS", f'{static}\\event_flairs\\pill.jpg'),
54+
(r"restaurants", f'{static}\\event_flairs\\food.jpg'),
55+
(r"pikachu", None),
56+
(r"Pokemon", f'{static}\\event_flairs\\pokemon.jpg'),
57+
]
58+
59+
60+
@pytest.mark.parametrize('token, link', tokens)
61+
def test_search_token_in_related_words(token, link):
62+
assert search_token_in_related_words(token) == link
63+
64+
65+
event_contents = [
66+
(r"memo backrub and medicines!!", f'{static}\\event_flairs\\massage.jpg'),
67+
(r"Dont forget medicines & backrub!", f'{static}\\event_flairs\\pill.jpg'),
68+
(r"Its important to drink", f'{static}\\event_flairs\\drank.jpg'),
69+
(r"call Jim about tennis friday", f'{static}\\event_flairs\\tennis.jpg'),
70+
(r"have to check on pikachu", r'#'),
71+
(r"-~new pokemon episode 19:00~!", f'{static}\\event_flairs\\pokemon.jpg'),
72+
]
73+
74+
75+
@pytest.mark.parametrize('event_content, link', event_contents)
76+
def test_attach_image_to_event(event_content, link):
77+
assert attach_image_to_event(event_content) == link

0 commit comments

Comments
 (0)