# -*- coding: UTF-8 -*-
import re
_prepare = lambda s: re.compile(
u'(?:(^ищу\s*)|(?P<male>(?:парня|парень))|(?P<female>(?:девушк[ау]|женщин[ау]))|((?P<age>[\d]{1,2})'
u'(?:\s*года?|\s*лет))|(?:страна\s*(?P<country>\S+))|(?:город\s*(?P<city>\S+)))', re.I | re.S
).sub(lambda x: x.group('age') and 'age %s' % x.group('age') or (
x.group('male') and 'sex male' or x.group('female') and 'sex female'
) or x.group('country') and 'country %s' % x.group('country').lower() or x.group('city') and 'city %s' % x.group('city').lower(), s)
s = u'ищу девушку страна казахстан 23 года', u'20 лет город москва страна россия', u'девушка страна россия город питер'
def getFilters(query):
q = _prepare(
query
)
f, i = dict(), iter(q.split())
for l in i:
try:
if l in ('sex', 'city', 'country'):
f[l] = i.next().lower()
elif l in ('age', ):
f[l] = int(i.next())
except StopIteration:
break
return f
for i in s:
print getFilters(i)