<filename>grammar.py
"""Generative grammar for queries."""

from core import Atom, PhrasePattern, AtomNeed, QueryPattern, qcreq, qcopt


def get_atoms():

    return [

        Atom('athletes', 'athletes'),
        Atom('countries', 'countries'),

        Atom('athlete_word', 'athlete'),

#        Atom('what', 'what', True),
        Atom('what', 'which', True),

        Atom('sports', 'sports'),
        Atom('sports', 'events'),
        
        Atom('were', 'were', True),
        Atom('contested', 'contested'),
        Atom('contested', 'played'),

        Atom('won', 'won', True),
        Atom('win', 'win', True),
        Atom('did', 'did', True),

        Atom('how_many', 'how many', True),
        Atom('how_many', 'what number of'),

        Atom('the', 'the', True),
        Atom('most', 'most'),
        Atom('fewest', 'fewest'),
        Atom('any', 'any'),

        Atom('medals', 'medals'),
        Atom('medals', 'total medals'),
        Atom('medals', 'medals of all colors'),
        Atom('medals', 'medals of any color'),

        Atom('golds_word', 'golds'),
        Atom('golds_word', 'gold medals'),

        Atom('silvers_word', 'silvers'),
        Atom('silvers_word', 'silver medals'),

        Atom('bronzes_word', 'bronzes'),
        Atom('bronzes_word', 'bronze medals'),

        Atom('in_sport', 'in', True),
        Atom('in_sport', 'in the sport of'),

        Atom('in_year', 'in', True),
        Atom('in_year', 'in the year'),

        Atom('by', 'by', True),
        Atom('sport_word', 'sport'),
        Atom('year_word', 'year'),

        Atom('at', 'at', True),
        Atom('age_word', 'age'),

        Atom('after', 'after', True),
        Atom('before', 'before', True),
        
    ]


def get_phrase_patterns():

    return [

        # actor
        PhrasePattern('actor', [AtomNeed('what', 'what'), AtomNeed('athletes', 'athletes')],
                      { 'english' : '$what $athletes', 'dims' : 'athlete' }),
        PhrasePattern('actor', [AtomNeed('what', 'what'), AtomNeed('countries', 'countries')], 
                      { 'english' : '$what $countries', 'dims' : 'country' }),

        # what_sports
        PhrasePattern('what_sports', [AtomNeed('what', 'what'), AtomNeed('sports', 'sports')],
                      { 'english' : '$what $sports', 'dims' : 'sport' }),

        # were contested
        PhrasePattern('were_contested', [AtomNeed('were', 'were'), AtomNeed('contested', 'contested')],
                      { 'english' : '$were $contested' }),

        # specific actor
        PhrasePattern('specific_actor', [AtomNeed('athlete', 'athlete')], 
                      { 'english' : '$athlete', 'dims' : 'athlete', 'conditions' : "athlete = '$athlete' COLLATE NOCASE" }),
        PhrasePattern('specific_actor', [AtomNeed('country', 'country')], 
                      { 'english' : '$country', 'dims' : 'country', 'conditions' : "country = '$country' COLLATE NOCASE" }),

        # won
        PhrasePattern('won', [AtomNeed('won', 'won')], { 'english' : '$won' }),

        # win
        PhrasePattern('win', [AtomNeed('win', 'win')], { 'english' : '$win' }),

        # did
        PhrasePattern('did', [AtomNeed('did', 'did')], { 'english' : '$did' }),

        # what
        PhrasePattern('what', [AtomNeed('what', 'what')], { 'english' : '$what' }),

        # how_many
        PhrasePattern('how_many', [AtomNeed('how_many', 'how_many')], { 'english' : '$how_many' }),
        
        # superlatives
        PhrasePattern('superlative', [AtomNeed('the', 'the'), AtomNeed('most', 'most')], 
                      { 'english' : '$the $most', 'orderbys' : 'num_medals desc', 'limit' : '20' }),
        PhrasePattern('superlative', [AtomNeed('the', 'the'), AtomNeed('fewest', 'fewest')], 
                      { 'english' : '$the $fewest', 'orderbys' : 'num_medals asc', 'limit' : '20' }),
        PhrasePattern('superlative', [AtomNeed('any', 'any')],
                      { 'english' : '$any' }),

        # medal counts
        PhrasePattern('medal_count', [AtomNeed('medals', 'medals')], 
                      { 'english' : '$medals', 'stats' : 'sum(total_medals) as num_medals', 'conditions' : 'total_medals > 0' }),
        PhrasePattern('medal_count', [AtomNeed('golds_word', 'golds')],
                      { 'english' : '$golds', 'stats' : "sum(golds) as num_medals", 'conditions' : 'golds > 0' }),
        PhrasePattern('medal_count', [AtomNeed('silvers_word', 'silvers')],
                      { 'english' : '$silvers', 'stats' : "sum(silvers) as num_medals", 'conditions' : 'silvers > 0' }),
        PhrasePattern('medal_count', [AtomNeed('bronzes_word', 'bronzes')],
                      { 'english' : '$bronzes', 'stats' : "sum(bronzes) as num_medals", 'conditions' : 'bronzes > 0' }),

        # in sport, year
        PhrasePattern('in_sport', [AtomNeed('in_sport', 'in'), AtomNeed('sport', 'sport')],
                      { 'english' : '$in $sport', 'conditions' : "sport = '$sport'" }),
        PhrasePattern('in_year', [AtomNeed('in_year', 'in'), AtomNeed('year', 'year')],
                      { 'english' : '$in $year', 'conditions' : "year = $year" }),

        # by sport, year, athlete
        PhrasePattern('by_sport', [AtomNeed('by', 'by'), AtomNeed('sport_word', 'sport')],
                      { 'english' : '$by $sport', 'dims' : 'sport' }),
        PhrasePattern('by_year', [AtomNeed('by', 'by'), AtomNeed('year_word', 'year')],
                      { 'english' : '$by $year', 'dims' : 'year' }),
        PhrasePattern('by_athlete', [AtomNeed('by', 'by'), AtomNeed('athlete_word', 'athlete')],
                      { 'english' : '$by $athlete', 'dims' : 'athlete' }),

        # age constraint
        PhrasePattern('age_constraint', [AtomNeed('at', 'at'), AtomNeed('age_word', 'age_word'), AtomNeed('age', 'age')],
                      { 'english' : '$at $age_word $age', 'conditions' : 'age = $age' }),
        PhrasePattern('age_constraint', [AtomNeed('by', 'by'), AtomNeed('age_word', 'age_word'), AtomNeed('age', 'age')],
                      { 'english' : '$by $age_word $age', 'conditions' : 'age <= $age'}),
        PhrasePattern('age_constraint', [AtomNeed('after', 'after'), AtomNeed('age_word', 'age_word'), AtomNeed('age', 'age')],
                      { 'english' : '$after $age_word $age', 'conditions' : 'age > $age'}),
        PhrasePattern('age_constraint', [AtomNeed('before', 'before'), AtomNeed('age_word', 'age_word'), AtomNeed('age', 'age')],
                      { 'english' : '$before $age_word $age', 'conditions' : 'age < $age'}),

        # by age
        PhrasePattern('by_age', [AtomNeed('by', 'by'), AtomNeed('age_word', 'age_word')],
                      { 'english' : '$by $age_word', 'dims' : 'age' })
    ]


def get_query_patterns():
    return [
        QueryPattern('medals',
                     [qcreq('actor'),        # who
                      qcreq('won'),          # won
                      qcopt('superlative'),  # the most
                      qcreq('medal_count'),  # golds
                      qcopt('in_sport'),     # [in swimming]
                      qcopt('age_constraint'),       # [at age 20]
                      qcopt('in_year')]),      # [in 2008]
        
        QueryPattern('medals',
                     [qcreq('how_many'),       # how many
                      qcreq('medal_count'),    # golds
                      qcreq('did'),            # did
                      qcreq('specific_actor'), # <NAME>
                      qcreq('win'),            # win
                      qcopt('age_constraint'), # [at age 20]
                      qcopt('in_sport'),       # [in swimming]
                      qcopt('in_year'),        # [in 2008]
                      qcopt('by_sport'),       # [by sport]?
                      qcopt('by_year'),        # [by year]?
                      qcopt('by_age'),
                      qcopt('by_athlete')]),       # [by age]?
        
        QueryPattern('medals',
                     [qcreq('what_sports'),
                      qcreq('were_contested'),
                      qcopt('in_year')])       # [in 2008]?
                     

    ]

<filename>grammar.py
"""Generative grammar for queries."""

from core import Atom, PhrasePattern, AtomNeed, QueryPattern, qcreq, qcopt


def get_atoms():

    return [

        Atom('athletes', 'athletes'),
        Atom('countries', 'countries'),

        Atom('athlete_word', 'athlete'),

#        Atom('what', 'what', True),
        Atom('what', 'which', True),

        Atom('sports', 'sports'),
        Atom('sports', 'events'),
        
        Atom('were', 'were', True),
        Atom('contested', 'contested'),
        Atom('contested', 'played'),

        Atom('won', 'won', True),
        Atom('win', 'win', True),
        Atom('did', 'did', True),

        Atom('how_many', 'how many', True),
        Atom('how_many', 'what number of'),

        Atom('the', 'the', True),
        Atom('most', 'most'),
        Atom('fewest', 'fewest'),
        Atom('any', 'any'),

        Atom('medals', 'medals'),
        Atom('medals', 'total medals'),
        Atom('medals', 'medals of all colors'),
        Atom('medals', 'medals of any color'),

        Atom('golds_word', 'golds'),
        Atom('golds_word', 'gold medals'),

        Atom('silvers_word', 'silvers'),
        Atom('silvers_word', 'silver medals'),

        Atom('bronzes_word', 'bronzes'),
        Atom('bronzes_word', 'bronze medals'),

        Atom('in_sport', 'in', True),
        Atom('in_sport', 'in the sport of'),

        Atom('in_year', 'in', True),
        Atom('in_year', 'in the year'),

        Atom('by', 'by', True),
        Atom('sport_word', 'sport'),
        Atom('year_word', 'year'),

        Atom('at', 'at', True),
        Atom('age_word', 'age'),

        Atom('after', 'after', True),
        Atom('before', 'before', True),
        
    ]


def get_phrase_patterns():

    return [

        # actor
        PhrasePattern('actor', [AtomNeed('what', 'what'), AtomNeed('athletes', 'athletes')],
                      { 'english' : '$what $athletes', 'dims' : 'athlete' }),
        PhrasePattern('actor', [AtomNeed('what', 'what'), AtomNeed('countries', 'countries')], 
                      { 'english' : '$what $countries', 'dims' : 'country' }),

        # what_sports
        PhrasePattern('what_sports', [AtomNeed('what', 'what'), AtomNeed('sports', 'sports')],
                      { 'english' : '$what $sports', 'dims' : 'sport' }),

        # were contested
        PhrasePattern('were_contested', [AtomNeed('were', 'were'), AtomNeed('contested', 'contested')],
                      { 'english' : '$were $contested' }),

        # specific actor
        PhrasePattern('specific_actor', [AtomNeed('athlete', 'athlete')], 
                      { 'english' : '$athlete', 'dims' : 'athlete', 'conditions' : "athlete = '$athlete' COLLATE NOCASE" }),
        PhrasePattern('specific_actor', [AtomNeed('country', 'country')], 
                      { 'english' : '$country', 'dims' : 'country', 'conditions' : "country = '$country' COLLATE NOCASE" }),

        # won
        PhrasePattern('won', [AtomNeed('won', 'won')], { 'english' : '$won' }),

        # win
        PhrasePattern('win', [AtomNeed('win', 'win')], { 'english' : '$win' }),

        # did
        PhrasePattern('did', [AtomNeed('did', 'did')], { 'english' : '$did' }),

        # what
        PhrasePattern('what', [AtomNeed('what', 'what')], { 'english' : '$what' }),

        # how_many
        PhrasePattern('how_many', [AtomNeed('how_many', 'how_many')], { 'english' : '$how_many' }),
        
        # superlatives
        PhrasePattern('superlative', [AtomNeed('the', 'the'), AtomNeed('most', 'most')], 
                      { 'english' : '$the $most', 'orderbys' : 'num_medals desc', 'limit' : '20' }),
        PhrasePattern('superlative', [AtomNeed('the', 'the'), AtomNeed('fewest', 'fewest')], 
                      { 'english' : '$the $fewest', 'orderbys' : 'num_medals asc', 'limit' : '20' }),
        PhrasePattern('superlative', [AtomNeed('any', 'any')],
                      { 'english' : '$any' }),

        # medal counts
        PhrasePattern('medal_count', [AtomNeed('medals', 'medals')], 
                      { 'english' : '$medals', 'stats' : 'sum(total_medals) as num_medals', 'conditions' : 'total_medals > 0' }),
        PhrasePattern('medal_count', [AtomNeed('golds_word', 'golds')],
                      { 'english' : '$golds', 'stats' : "sum(golds) as num_medals", 'conditions' : 'golds > 0' }),
        PhrasePattern('medal_count', [AtomNeed('silvers_word', 'silvers')],
                      { 'english' : '$silvers', 'stats' : "sum(silvers) as num_medals", 'conditions' : 'silvers > 0' }),
        PhrasePattern('medal_count', [AtomNeed('bronzes_word', 'bronzes')],
                      { 'english' : '$bronzes', 'stats' : "sum(bronzes) as num_medals", 'conditions' : 'bronzes > 0' }),

        # in sport, year
        PhrasePattern('in_sport', [AtomNeed('in_sport', 'in'), AtomNeed('sport', 'sport')],
                      { 'english' : '$in $sport', 'conditions' : "sport = '$sport'" }),
        PhrasePattern('in_year', [AtomNeed('in_year', 'in'), AtomNeed('year', 'year')],
                      { 'english' : '$in $year', 'conditions' : "year = $year" }),

        # by sport, year, athlete
        PhrasePattern('by_sport', [AtomNeed('by', 'by'), AtomNeed('sport_word', 'sport')],
                      { 'english' : '$by $sport', 'dims' : 'sport' }),
        PhrasePattern('by_year', [AtomNeed('by', 'by'), AtomNeed('year_word', 'year')],
                      { 'english' : '$by $year', 'dims' : 'year' }),
        PhrasePattern('by_athlete', [AtomNeed('by', 'by'), AtomNeed('athlete_word', 'athlete')],
                      { 'english' : '$by $athlete', 'dims' : 'athlete' }),

        # age constraint
        PhrasePattern('age_constraint', [AtomNeed('at', 'at'), AtomNeed('age_word', 'age_word'), AtomNeed('age', 'age')],
                      { 'english' : '$at $age_word $age', 'conditions' : 'age = $age' }),
        PhrasePattern('age_constraint', [AtomNeed('by', 'by'), AtomNeed('age_word', 'age_word'), AtomNeed('age', 'age')],
                      { 'english' : '$by $age_word $age', 'conditions' : 'age <= $age'}),
        PhrasePattern('age_constraint', [AtomNeed('after', 'after'), AtomNeed('age_word', 'age_word'), AtomNeed('age', 'age')],
                      { 'english' : '$after $age_word $age', 'conditions' : 'age > $age'}),
        PhrasePattern('age_constraint', [AtomNeed('before', 'before'), AtomNeed('age_word', 'age_word'), AtomNeed('age', 'age')],
                      { 'english' : '$before $age_word $age', 'conditions' : 'age < $age'}),

        # by age
        PhrasePattern('by_age', [AtomNeed('by', 'by'), AtomNeed('age_word', 'age_word')],
                      { 'english' : '$by $age_word', 'dims' : 'age' })
    ]


def get_query_patterns():
    return [
        QueryPattern('medals',
                     [qcreq('actor'),        # who
                      qcreq('won'),          # won
                      qcopt('superlative'),  # the most
                      qcreq('medal_count'),  # golds
                      qcopt('in_sport'),     # [in swimming]
                      qcopt('age_constraint'),       # [at age 20]
                      qcopt('in_year')]),      # [in 2008]
        
        QueryPattern('medals',
                     [qcreq('how_many'),       # how many
                      qcreq('medal_count'),    # golds
                      qcreq('did'),            # did
                      qcreq('specific_actor'), # <NAME>
                      qcreq('win'),            # win
                      qcopt('age_constraint'), # [at age 20]
                      qcopt('in_sport'),       # [in swimming]
                      qcopt('in_year'),        # [in 2008]
                      qcopt('by_sport'),       # [by sport]?
                      qcopt('by_year'),        # [by year]?
                      qcopt('by_age'),
                      qcopt('by_athlete')]),       # [by age]?
        
        QueryPattern('medals',
                     [qcreq('what_sports'),
                      qcreq('were_contested'),
                      qcopt('in_year')])       # [in 2008]?
                     

    ]

Generative grammar for queries. #        Atom('what', 'what', True), # actor # what_sports # were contested # specific actor # won # win # did # what # how_many # superlatives # medal counts # in sport, year # by sport, year, athlete # age constraint # by age # who # won # the most # golds # [in swimming] # [at age 20] # [in 2008] # how many # golds # did # <NAME> # win # [at age 20] # [in swimming] # [in 2008] # [by sport]? # [by year]? # [by age]? # [in 2008]?