Ejemplo n.º 1
0
def expand_sections(m):
    tokentext = m.group("section")
    print("expand_sections():",
          m.groups(),
          tokentext.encode("utf-8"),
          file=debug)
    w = []
    for tok in tokentext.split(".")[:-1]:
        w.extend(num_expand1(int(tok)).split())
        w.append("punt")
    w.extend(num_expand1(int(tokentext.split(".")[-1])).split())
    return [w]
Ejemplo n.º 2
0
def expand_curr(m):
    print("expand_currency():", m.groups(), file=debug)
    expansions = []
    if not "mult" in m.groupdict():
        denom = m.group("denom").lower()
        curr = int(m.group("curr").replace(" ", ""))
        try:
            cent = m.group("cent")  #can be None
        except IndexError:
            cent = None
        denoms = [denom_map_curr[denom]]
        currs = num_expand(curr)
        cents = []
        if cent is not None:
            i = int(cent)
            if i:
                cent = num_expand1(i)
                cents.extend([
                    "en {cent} sent".format(cent=cent),
                    "{cent}".format(cent=cent)
                ])
            else:
                cents.append("")
        else:
            cents.append("")
        for template in cent_template:
            for curr in currs:
                for cent in cents:
                    for denom in denoms:
                        expansions.append(
                            template.format(curr=curr, denom=denom, cent=cent))
    else:
        denom = m.group("denom").lower()
        curr = m.group("curr")
        mult = mult_map_curr[m.group("mult")[0]]
        denoms = [denom_map_curr[denom], denom_map_curr[denom] + "s"]
        print(num_expand1(int(curr.split(".")[0])), file=debug)
        print(_expand_spell(curr.split(".")[1])[0], file=debug)

        currs = [
            num_expand1(int(curr.split(".")[0])) + " punt " +
            " ".join(map(lambda x: num_expand1(int(x)),
                         curr.split(".")[-1]))
        ]
        for template in mult_template:
            for curr in currs:
                for denom in denoms:
                    expansions.append(
                        template.format(curr=curr, denom=denom, mult=mult))
    expansions = [e.split() for e in set(expansions)]
    print(expansions, file=debug)
    return expansions
Ejemplo n.º 3
0
def expand_years(m):
    print("expand_years():", m.groups(), file=debug)
    year = m.group("year")
    years = []
    years.append(" ".join(
        [num_expand1(int(year[:2])),
         num_expand1(int(year[2:]))]))
    if year[2:].startswith("0"):
        years.append(" ".join(
            [num_expand1(int(year[:2])), "nul",
             num_expand1(int(year[2:]))]))
    expansions = []
    for year in years:
        expansions.append(year.split())
    return expansions
Ejemplo n.º 4
0
def expand_dates(m):
    print("expand_dates():", m.groups(), file=debug)
    #VALIDATE/INTERPRET MATCH
    try:
        year = int(m.group("year"))
        if year < 100:
            year = 2000 + year
    except IndexError:
        year = None
    try:
        month = expand_month(m.group("month"))
    except KeyError:
        return []
    day = int(m.group("day"))
    if day > 31:
        return []
    #COLLECT ALTERNATIVES
    years = []
    months = []
    days = []
    if year is not None:
        years.extend(num_expand(year))
        years.append(" ".join(
            [num_expand1(int(str(year)[:2])),
             num_expand1(int(str(year)[2:]))]))
        if str(year)[2:].startswith("0"):
            years.append(" ".join([
                num_expand1(int(str(year)[:2])), "nul",
                num_expand1(int(str(year)[2:]))
            ]))
    else:
        years.append("")
    months.append(month)
    days.append(num_expand1(day))
    days.append(dayexpand[day])
    #CONSTRUCT EXPANSIONS
    expansions = []
    for patt in exppatts_dates:
        for year in years:
            for month in months:
                for day in days:
                    expansions.append(
                        patt.format(day=day, month=month, year=year).split())
    return expansions
Ejemplo n.º 5
0
def expand_24hrtimes(hour, minute, afternoon):
    if afternoon and hour < 12:
        hour += 12
    elif not afternoon:
        hour = hour % 12 or 12
    if minute == 0:
        minutes = [""]
    elif minute < 10:
        minutes = ["nul {}".format(num_expand1(minute))]
    else:
        minutes = [num_expand1(minute)]
    hours = [num_expand1(hour)]
    expansions = []
    for patt in exppatts_24:
        for hr in hours:
            for mn in minutes:
                for delim in expdelim24:
                    expansions.append(
                        patt.format(hour=hr, delim=delim, minute=mn))
    return expansions
Ejemplo n.º 6
0
def expand_specialtimes(hour, minute, afternoon):
    expansions = []
    for noon in expand_noon(hour, minute, afternoon):
        if minute == 0:
            expansions.extend([
                "{hour} uur {noon}".format(hour=num_expand1(hour % 12 or 12),
                                           noon=noon),
                "{hour} {noon}".format(hour=num_expand1(hour % 12 or 12),
                                       noon=noon)
            ])
        elif minute == 30:
            expansions.append("half {hour} {noon}".format(hour=num_expand1(
                (hour + 1) % 12 or 12),
                                                          noon=noon))
        elif minute == 15:
            expansions.append("kwart oor {hour} {noon}".format(
                hour=num_expand1(hour % 12 or 12), noon=noon))
        elif minute == 15:
            expansions.append("kwart voor {hour} {noon}".format(
                hour=num_expand1((hour + 1) % 12 or 12), noon=noon))
    return expansions
Ejemplo n.º 7
0
def expand_times(m):
    #DEMITASSE: TODO: Simplify this... reduce repetition
    hour = int(m.group("hour"))
    hour2 = hour + 1
    minute = int(m.group("minute"))
    minute2 = 60 - minute
    try:
        afternoon = "p" in m.group("noon").lower()
    except AttributeError:
        afternoon = hour >= 12
    print("expand_times():", m.groups(), file=debug)
    expansions = []
    if minute == 0:
        expansions.extend(expand_specialtimes(hour, minute, afternoon))
    else:
        #COLLECT "minutes past" expansions
        hours = [hour]
        minutes = [minute]
        noons = expand_noon(
            hour, minute,
            afternoon)  #also returns empty string ("not pronounced")
        for patt in exppatts_mpasth:
            for hr in hours:
                for mn in minutes:
                    for noon in noons:
                        expansions.append(
                            patt.format(hour=num_expand1(hr % 12 or 12),
                                        minute=num_expand1(mn),
                                        noon=noon))
        #COLLECT "minutes to" expansions
        hours = [hour2]
        minutes = [minute2]
        noons = noons
        for patt in exppatts_mtoh:
            for hr in hours:
                for mn in minutes:
                    for noon in noons:
                        expansions.append(
                            patt.format(hour=num_expand1(hr % 12 or 12),
                                        minute=num_expand1(mn),
                                        noon=noon))
        #COLLECT "hour minute"
        hours = [hour]
        minutes = [minute]
        noons = noons
        for patt in exppatts_hm:
            for hr in hours:
                for mn in minutes:
                    for noon in noons:
                        expansions.append(
                            patt.format(hour=num_expand1(hr % 12 or 12),
                                        minute=num_expand1(mn),
                                        noon=noon))
        #COLLECT "special case"
        if minute in [15, 30, 45]:
            expansions.extend(expand_specialtimes(hour, minute, afternoon))
    #COLLECT "24 hour"
    expansions.extend(expand_24hrtimes(hour, minute, afternoon))
    expansions = [e.split() for e in set(expansions)]
    return expansions
Ejemplo n.º 8
0
def expand_floats(m):
    tokentext = m.group("float")
    print("expand_floats():",
          m.groups(),
          tokentext.encode("utf-8"),
          file=debug)
    w = []
    for c in tokentext:
        if c == ".":
            w.append("punt")
        else:
            try:
                w.append(num_expand1(int(c)))
            except ValueError:
                w.append(_expand_spell(c))  #shouldn't happen
    return [w]
Ejemplo n.º 9
0
def num_expand(n):
    return [num_expand1(n), num_expand2(n)]