Skip to content

Commit

Permalink
New app: testing, interface for testing interactively algos and such
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Hélouis committed May 30, 2015
1 parent 26c3a94 commit 7b2f9b5
Show file tree
Hide file tree
Showing 25 changed files with 52,545 additions and 6 deletions.
3 changes: 1 addition & 2 deletions menugen/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'stdimage',

'menus',
'testing',
)

MIDDLEWARE_CLASSES = (
Expand Down
3 changes: 2 additions & 1 deletion menugen/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# url(r'^blog/', include('blog.urls')),

url(r'^', include('menus.urls')),
url(r'^', include('testing.urls')),
url(r'^admin/', include(admin.site.urls)),
)

if settings.DEBUG:
urlpatterns += patterns('',
(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
'document_root': settings.MEDIA_ROOT}))
'document_root': settings.MEDIA_ROOT}))
4 changes: 2 additions & 2 deletions menus/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
(0, 'Jamais'),
(1, 'De temps en temps'),
(2, 'Souvent'),
(3, 'Tous le temps'),
(3, 'Tout le temps'),
)


Expand Down Expand Up @@ -54,7 +54,7 @@ class Recipe(models.Model):
cook_time = models.IntegerField()
amount = models.IntegerField()
difficulty = models.IntegerField(choices=EASE)
price = models.IntegerField(choices=PRICE) # FIXME : exact quantification? -> check marmiton
price = models.IntegerField(choices=PRICE)
steps = models.TextField()
detail = models.TextField()
drink = models.TextField()
Expand Down
2 changes: 1 addition & 1 deletion menus/templates/_common_js.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
}
}
});
</script>
</script>
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ django-stdimage==1.2.2
Pillow==2.8.1
progressbar2==2.7.3
numpy
requests
beautifulsoup4
Empty file added testing/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions testing/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
3 changes: 3 additions & 0 deletions testing/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
Empty file.
48,170 changes: 48,170 additions & 0 deletions testing/recipe_engine/good_recipes.txt

Large diffs are not rendered by default.

114 changes: 114 additions & 0 deletions testing/recipe_engine/parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#! /usr/bin/env python
# coding: utf-8

class Ingredient:
""" Parse given string to extract a name, a unit and a quantity """

def __init__(self, s):
self.text = s
self.name = None
self.unit = None
self.quantity = None

word2num = {
"un demi" : 1/2,
"trois-quarts" : 3/4, "trois quarts" : 3/4,
"un" : 1, "une" : 1,
"deux" : 2,
"trois" : 3,
"quatre" : 4,
"cinq" : 5,
"six" : 6,
"sept" : 7,
"huit" : 8,
"neuf" : 9,
"dix" : 10, "une dizaine" : 10,
"onze" : 11, "une onzaine" : 11,
"douze" : 12, "une douzaine" : 12
}

word2unit = {
"quelques gouttes" : "1 g", "un peu" : "1 g", "une pincée" : "1 g", "un soupçon" : "1 g", "une goutte" : "1 g", "un nuage" : "1 g", "un zeste" : "1 g", "un fond" : "1 g",
"milligramme" : "mg", "milligrammes" : "mg",
"centigramme" : "cg", "centigrammes" : "cg",
"decigramme" : "dg", "decigrammes" : "dg",
"gramme" : "g", "grammes" : "g",
"kilogramme" : "kg", "kilogrammes" : "kg", "kilo" : "kg", "kilos" : "kg",
"litre" : "l",
"cuillères à soupe" : "cuillère à soupe", "cuillerée" : "cuillère à soupe",
"cuillères à café" : "cuillère à café",
"verres" : "verre",
"tranches" : "tranche",
"gousses" : "gousse"
}

units = [ "mg", "g", "cg", "kg", "l", "cl", "cuillère à soupe", "cuillère à café", "verre", "tranche" , "gousse" ]

useless = [ "de" ]

# step 1: numerize
for key, val in word2num.items():
s = s.replace(" " + key + " ", " " + str(val) + " ")
if s.startswith(key + " ") or s.endswith(" " + key):
s = s.replace(key, str(val))

# step 2: unify units
for key, val in word2unit.items():
s = s.replace(" " + key + " ", " " + val + " ")
if s.startswith(key + " ") or s.endswith(" " + key):
s = s.replace(key, val)

# step 3: remove useless words
for w in useless:
s = s.replace(" " + w, "")

# step 4: parse delicately
after_quantity = False
in_unit = False
for word in s.split(" "):
if in_unit:
self.unit += " " + word
self.name += " " + word
for u in units:
if self.unit == u:
in_unit = False
self.name = None
continue
if self.quantity:
after_quantity = True
try:
if self.quantity:
self.quantity = self.quantity * int(word)
self.name = None
else:
self.quantity = int(word)
self.name = None
except:
if self.quantity and (self.unit or self.name):
self.name = self.name + " " + word if self.name else word
continue
if "/" in word:
word = word.split("/")
try:
self.quantity = int(word[0]) / int(word[1])
except:
pass
else:
if word in units:
self.unit = word
else:
for u in units:
if u.split(" ")[0] == word:
self.unit = word
self.name = word # in case not a unit
in_unit = True
if after_quantity:
after_quantity = False
self.name = word
pass

if not self.quantity and not self.unit:
self.name = s

if self.name == self.unit:
self.unit = None
Loading

0 comments on commit 7b2f9b5

Please sign in to comment.