Ejemplo n.º 1
0
def articles_with_multiple_types(*types, **kwargs):
    """
    Given one or many lowercase types (spaces allowed, string), return
    articles containing those types.

    Specify op='and' for an AND query that returns articles with ALL types (default)
    Specify op='or' for an OR query that returns articles with AT LEAST one of types

    If an invalid op is passed, raises a ValueError.

    Specify limit=n kwarg to limit return values to n rows.
    """

    # In Python 2, we can't specify *args and named kwargs
    # we must use *args and **kwargs
    op = kwargs.pop('op', 'and')
    limit = kwargs.pop('limit', sys.maxint)

    if op == 'and':
        result = (Article.select().join(ArticleType).join(Type).where(
            Type.type << types).group_by(Article).having(
                fn.COUNT(Article.id) == len(types)).limit(limit))
    elif op == 'or':
        result = (Article.select().join(ArticleType).join(Type).where(
            Type.type << types).group_by(Article).limit(limit))
    else:
        raise ValueError(
            "Illegal op: {}. Valid options are 'and', 'or'.".format(op))

    return [x.title for x in result]
Ejemplo n.º 2
0
def articles_of_class(w_class, limit=sys.maxint, random=False):
    """
    Given a wikipedia class, return all articles of that type.
    Set random=True to get random articles from the database (slower)
    """
    w_class = to_wikipedia_class(w_class)
    if random:
        result = (Article.select().join(ArticleClass).join(WikiClass).where(
            WikiClass.class_name == w_class).order_by(
                fn.Random()).limit(limit))
    else:
        result = (Article.select().join(ArticleClass).join(WikiClass).where(
            WikiClass.class_name == w_class).limit(limit))
    return [x.title for x in result]
Ejemplo n.º 3
0
def articles_of_hypernym_from_db(hypernym, limit=sys.maxint):
    """
    Given a hypernym from DBpedia (string), return a list of
    articles of that hypernym. Note: use 'thing' instead of
    'owl:Thing'.
    """
    hypernym = to_dbpedia_class(hypernym)
    result = (Article.select().join(ArticleClass).join(
        Hypernym,
        on=ArticleClass.c_id).join(DbpediaClass, on=Hypernym.d_id).where(
            DbpediaClass.dbpedia_class == hypernym).limit(limit))
    return [x.title for x in result]