8000 Readme: Demo and Logo Placement by lgiesen · Pull Request #257 · MensaToday/mensa-today · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Readme: Demo and Logo Placement #257

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[![Python 3.9.7](https://img.shields.io/badge/python-3.9-orange.svg)](https://www.python.org/downloads/release/python-390/) [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0) ![GitHub version](https://img.shields.io/github/v/release/erikzimmermann/mensa-today?color=green&include_prereleases)
<img align="right" height="72px" src="https://raw.githubusercontent.com/MensaToday/mensa-today/development/frontend/src/assets/logo.png" />
# MensaToday - Your Dish Recommender in Münster

# <img src="https://github.com/erikzimmermann/mensa-today/blob/development/frontend/src/assets/logo.png" height="24" style="margin-right:5px;"/><span>MensaToday - your dish recommender in Münster</span>

The University of Münster (Westfälische Wilhelms-Universität Münster) is a distributed across the city with various canteens and bistros that serve different ranges of food which change weekly. As a student who eats at those places frequently, you have to look through all dishes of every canteen to find a meal that serves your needs. The idea this recommender system is to suggest mensa meals based on various different factors, such as your eating habits, location (based on semester schedule), weather and many more.
The University of Münster is a distributed across the city with various canteens and bistros that serve different ranges of food which change weekly. As a student who eats at those places frequently, you have to look through all dishes of every canteen to find a meal that serves your needs. The idea this recommender system is to suggest mensa meals based on various different factors, such as your eating habits, location (based on semester schedule), weather and many more. You can get a demo [here](https://leogiesen.de/projects/MensaToday/MensaToday-Demo-2023-02-23.mp4) from February 23, 2023.

![Poster](poster.svg)
## Contributing
Expand Down
1 change: 1 addition & 0 deletions backend/mensa/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
path('mensa/get_week_recommendation', views.get_week_recommendation),
path('mensa/get_quiz_dishes', views.get_quiz_dishes),
path('mensa/save_user_side_dishes', views.save_user_side_dishes),
path('mensa/mensa_data', views.get_mensa_data),
]
37 changes: 35 additions & 2 deletions backend/mensa/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from rest_framework.response import Response

from mensa.models import Dish, DishPlan, UserDishRating, Category, Allergy, \
UserSideSelection
UserSideSelection, Mensa
from mensa_recommend.serializers import DishPlanSerializer, \
UserDishRatingSerializer, DishSerializer
UserDishRatingSerializer, DishSerializer, MensaSerializer
from mensa_recommend.source.computations.date_computations import \
get_last_monday
from mensa_recommend.source.computations.transformer import transform_rating
Expand Down Expand Up @@ -560,3 +560,36 @@ def save_user_side_dishes(request):
else:
return Response("Not all fields provided",
status=status.HTTP_406_NOT_ACCEPTABLE)


@api_view(['GET'])
@permission_classes((permissions.AllowAny,))
def get_mensa_data(request):
"""Get all relevant information about each mensa in the system

Route: api/v1/mensa/mensa_data
Authorization: AllowAny
Methods: Get

Output
-------
[
{
"id": 1,
"name": "Bistro Denkpause",
"city": "Münster",
"street": "Corrensstr.",
"houseNumber": "25",
"zipCode": 48149,
"startTime": "11:30:00",
"endTime": "14:15:00",
"lat": "51.96836000",
"lon": "7.59485300"
},
...
]
"""

mensa_qs = Mensa.objects.all()
mensa_serialized = MensaSerializer(mensa_qs, many=True).data
return Response(mensa_serialized)
11 changes: 7 additions & 4 deletions backend/mensa_recommend/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_translated_name(self, obj: mensa_model.Dish) -> str:
class MensaSerializer(serializers.ModelSerializer):
class Meta:
fields = ["id", "name", "city", "street",
"houseNumber", "zipCode", "startTime", "endTime"]
"houseNumber", "zipCode", "startTime", "endTime", "lat", "lon"]
model = mensa_model.Mensa


Expand Down Expand Up @@ -124,10 +124,13 @@ def __init__(self, *args, **kwargs):
del self.fields['side_selected']

def get_ext_ratings(self, obj):
ext_ratings = mensa_model.ExtDishRating.objects.filter(
mensa=obj.mensa, dish=obj.dish).latest("date")
try:
ext_ratings = mensa_model.ExtDishRating.objects.filter(
mensa=obj.mensa, dish=obj.dish).latest("date")

return ExtRatingsSerializer(ext_ratings, read_only=True).data
return ExtRatingsSerializer(ext_ratings, read_only=True).data
except mensa_model.ExtDishRating.DoesNotExist:
return None

def get_popular_side_dish(self, obj):
popular_side = side_dish_recommender.predict(obj)
Expand Down
146 changes: 146 additions & 0 deletions backend/mensa_recommend/source/computations/lsh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import django.db.models as Model
from mensa.models import Dish, DishPlan
from django.db.models.query import QuerySet
from datasketch import MinHash, MinHashLSH
from abc import ABC, abstractmethod

from typing import Tuple


class LSH(ABC):
"""Class to search for duplicates in a database queryset and
optionally remove the duplicate entries
"""

def __init__(self, queryset: QuerySet = None, num_perm: int = 128, threshold: float = 0.7):
"""Constructor

Parameters
----------
queryset : QuerySet (optional)
A django Queryset Object. If it is None only the remove function can be used
which is based on the ids to be deleted and the model object
num_perm : int
Number of min hash permuatations (default = 128)
threshold: float
Threshold at which a tuple is seen as similar
"""

self.num_perm = num_perm
self.threshold = threshold
self.queryset = queryset

if self.queryset is not None:
self.set_list = self._create_sets()
self.min_hashes, self.lsh = self._initialize_lsh()

def get_duplicates(self) -> set[Tuple[int]]:
"""Searches for duplicates based on minhashes

Return
------
duplicates : set[Tuple[int]]
A set of tuples with duplicate values. A set was chosen as the return type,
since only one duplicate tuple should be returned for every unique duplicate
detection. The integers represent the ids.
"""

if (self.queryset is None):
raise ("To use this function a queryset has to be defined.")

duplicates = []

# Iterate over each min hash and find duplicate values
for hash in self.min_hashes:
neighbors = self.lsh.query(hash[1])
duplicates.append(neighbors)

return set(tuple(i) for i in duplicates if len(i) > 1)

@abstractmethod
def _create_sets(self) -> list[Tuple[int, set[str]]]:
"""Abstract method to generate a list of sets over the search strings/documents.
Because this process is unique for every queryset this function has to be overwritten
by the concrete implementation.

Return
------
return : list[Tuple[int, set[str]]]
A list of tuples. Each tuple consists of a tuple id and the set of an input string/document.
"""
pass

@abstractmethod
def fuse_duplicates(self, duplicates: set[Tuple[int]]):
"""Abstract method to fuse objects together based on a set of duplicate tuples with ids.
Because the fuse process is handle individually for each database table this method has to be
overwritten.

Parameters
----------
duplicates : set[Tuple[int]]
A set of tuples with the ids of duplicate objects
"""
pass

def _initialize_lsh(self) -> Tuple[list[MinHash], MinHashLSH]:
"""Create a list of minhashes for each input set and also initalize the lsh object.

Return
------
return : Tuple[list[MinHash], MinHashLSH]
A list of minhashes and a lsh object
"""

if self.set_list is None:
raise ("method _create_sets has to be executed first")

# Create LSH index
lsh = MinHashLSH(threshold=0.7, num_perm=self.num_perm)

min_hashes = []
for id, string_set in self.set_list:

m = MinHash(num_perm=self.num_perm)

for d in string_set:
m.update(d.encode("utf8"))

min_hashes.append((id, m))

lsh.insert(id, m)

return (min_hashes, lsh)


class DishLSH(LSH):

def _create_sets(self) -> list[Tuple[int, set[str]]]:

set_list = []

for dish in self.queryset:
string_set = set(dish.name.split())

set_list.append((dish.id, string_set))

return set_list

def fuse_duplicates(self, duplicates: set[Tuple[int]]):
# This function only replaces the ids in the dishplan but does not delete the dishes
# in the Dish table
for duplicate in duplicates:

# When only one element is in the tuple jump to next element
if len(duplicate) == 1:
continue

reference_index = duplicate[0]
dish = Dish.objects.get(id=reference_index)

for i in range(1, len(duplicate)):
dishplans = DishPlan.objects.filter(dish=duplicate[i]).all()

for dishplan in dishplans:
dishplan.dish = dish
dishplan.save()
9 changes: 9 additions & 0 deletions backend/mensa_recommend/source/data_collection/imensa.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from mensa.models import Category, Allergy, Additive, Dish, DishCategory, \
DishAllergy, DishAdditive, DishPlan, Mensa, \
ExtDishRating
from mensa_recommend.source.computations.lsh import DishLSH
from . import static_data
from . import utils
from .utils import NoAuthURLCollector
Expand Down Expand Up @@ -72,6 +73,14 @@ def post_processing():
# Get all dishes
dishes = Dish.objects.all()

# Get duplicate dishes and fuse them
# @TODO further investigate the quality of duplicate detection
# For now it should not be applied

#dish_lsh = DishLSH(queryset=dishes)
#duplicates = dish_lsh.get_duplicates()
# dish_lsh.fuse_duplicates(duplicates)

# Iteratate over each dish
for dish in tqdm(dishes):

Expand Down
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ djangorestframework-simplejwt==5.2.2
Google-Images-Search==1.4.6
pycryptodome==3.14.1
numpy==1.23.5
sentence_transformers==2.2.2
sentence_transformers==2.2.2
datasketch == 1.5.9
Loading
0