Ejemplo n.º 1
0
def approximate(word, amount=1, plural={}):
    """ Returns an approximation of the number of given objects.
        Two objects are described as being "a pair",
        smaller than eight is "several",
        smaller than twenty is "a number of",
        smaller than two hundred are "dozens",
        anything bigger is described as being tens or hundreds of thousands or millions.
        For example: approximate("chicken", 100) => "dozens of chickens".
    """
    try: p = pluralize(word, custom=plural)
    except:
        raise TypeError, "can't pluralize %s, only str and unicode" % word.__class__.__name__
    # Anything up to 200.
    if amount == 0: 
        return "%s %s" % (NONE, p)
    if amount == 1: 
        return referenced(word) # "a" chicken, "an" elephant
    if amount == 2: 
        return "%s %s" % (PAIR, p)
    if 3 <= amount < 8: 
        return "%s %s" % (SEVERAL, p)
    if 8 <= amount < 18: 
        return "%s %s" % (NUMBER, p)
    if 18 <= amount < 23: 
        return "%s %s" % (SCORE, p)
    if 23 <= amount < 200: 
        return "%s %s" % (DOZENS, p)
    if amount > 10000000:
        return "%s %s" % (COUNTLESS, p)
    # Hundreds and thousands.
    thousands = int(log(amount, 10) / 3)
    hundreds  = ceil(log(amount, 10) % 3) - 1
    h = hundreds==2 and "hundreds of " or (hundreds==1 and "tens of " or "")
    t = thousands>0 and pluralize(ORDER[thousands])+" of " or ""
    return "%s%s%s" % (h, t, p)
Ejemplo n.º 2
0
def approximate(word, amount=1, plural={}):
    """ Returns an approximation of the number of given objects.
        Two objects are described as being "a pair",
        smaller than eight is "several",
        smaller than twenty is "a number of",
        smaller than two hundred are "dozens",
        anything bigger is described as being tens or hundreds of thousands or millions.
        For example: approximate("chicken", 100) => "dozens of chickens".
    """
    try: p = pluralize(word, custom=plural)
    except:
        raise TypeError("can't pluralize %s (not a string)" % word.__class__.__name__)
    # Anything up to 200.
    if amount == 0: 
        return "%s %s" % (NONE, p)
    if amount == 1: 
        return referenced(word) # "a" chicken, "an" elephant
    if amount == 2: 
        return "%s %s" % (PAIR, p)
    if 3 <= amount < 8: 
        return "%s %s" % (SEVERAL, p)
    if 8 <= amount < 18: 
        return "%s %s" % (NUMBER, p)
    if 18 <= amount < 23: 
        return "%s %s" % (SCORE, p)
    if 23 <= amount < 200: 
        return "%s %s" % (DOZENS, p)
    if amount > 10000000:
        return "%s %s" % (COUNTLESS, p)
    # Hundreds and thousands.
    thousands = int(log(amount, 10) / 3)
    hundreds  = ceil(log(amount, 10) % 3) - 1
    h = hundreds==2 and "hundreds of " or (hundreds==1 and "tens of " or "")
    t = thousands>0 and pluralize(ORDER[thousands])+" of " or ""
    return "%s%s%s" % (h, t, p)
Ejemplo n.º 3
0
    def inl(self, question=False):
        # add apostrophe, if the select entries contain column names with multiple words with spaces
        columns = [
            "'" + x + "'" if (" " in x and x[0] != "'" and x[-1] != "'") else x
            for x in self.selcols
        ]

        #if question or not
        count_start = "how many " if question else "count the "
        count_end = " are there" if question else ""

        #case for counting:
        if self.agg == "COUNT":
            if len(columns) == 1 and (self.entity != ""
                                      and columns[0] == self.entity):
                if self.entity[
                        -1] == "s" or self.VERBOSE:  #it must be already plural?
                    return count_start + self.entity + count_end
                else:
                    return count_start + inflect.pluralize(
                        self.entity) + count_end
            elif len(columns) == 1:
                if columns[0][
                        -1] == "s" or self.VERBOSE:  #it must be already plural?
                    return count_start + str(columns[0]) + count_end
                else:
                    return count_start + inflect.pluralize(
                        columns[0]) + count_end

        showstart = "what is the " if question else "show the "
        output = "what is the " if question else "show the "

        #aggregation case.
        if self.agg != "" and len(self.selcols) == 1:
            colname = columns[0]
            if self.agg in self.aggwords_inl.keys():
                aggregation = self.aggwords_inl[self.agg]
                #special case for SUM: I have to use plural: example: "sum of all prices" instead of "sum of all price"
                if self.agg == "SUM":
                    plurayesno, singular = isplural(colname)
                    if not plurayesno and not self.VERBOSE:
                        colname = inflect.pluralize(colname)

                output += aggregation + " " + colname
                return output

        if len(columns) == 1 and (self.entity != ""
                                  and columns[0] == self.entity):
            return showstart + self.entity
        else:
            if len(columns) == 1:
                output += columns[0]
            elif len(columns) == 0:
                return ""
            else:
                for col in columns[:-1]:
                    output += col + ", "
                output = output[:-2]
                output += " and " + columns[-1]
            return output
Ejemplo n.º 4
0
        if len(columns) == 1 and (self.entity != ""
                                  and columns[0] == self.entity):
            return showstart + self.entity
        else:
            if len(columns) == 1:
                output += columns[0]
            elif len(columns) == 0:
                return ""
            else:
                for col in columns[:-1]:
                    output += col + ", "
                output = output[:-2]
                output += " and " + columns[-1]
            return output


if __name__ == "__main__":
    print("starte")
    a = SelectState()
    a.selcols = ["time", "arrival", "destination"]
    print(a)
    print()
    b = SelectState()
    b.entity = "flight"
    print(b)
    print("###")
    print(a.inl())
    print(b.inl())
    print("Plural of: apple")
    print(inflect.pluralize("apple"))
Ejemplo n.º 5
0
def fixNoun(elem, spanishNoun, tag):
    if tag in [u'NNS', u'NNPS']:
        return (pluralize(elem[0]), elem[1])
    else:
        return elem