Пример #1
0
def get_facts_by_title(title):
    query = '''
        MATCH (fact:Fact)
        WHERE fact.title = {title}
        RETURN fact
        '''
    return graph.run(query, title=title)
Пример #2
0
def get_questions_by_tag(tag):
    query = '''
        MATCH (question:Question)<-[:TAGGED]-(t:Tag)
        WHERE t.name = {tag}
        RETURN question
        '''
    return graph.run(query, tag=tag)
Пример #3
0
def get_questions_by_title(title):
    query = '''
        MATCH (question:Question)
        WHERE question.title = {title}
        RETURN question
        '''
    return graph.run(query, title=title)
Пример #4
0
def get_facts_by_text(text):
    query = '''
        MATCH (fact:Fact)
        WHERE fact.text CONTAINS {text}
        RETURN fact
        '''
    return graph.run(query, text=text)
Пример #5
0
def get_questions_by_text(text):
    query = '''
        MATCH (question:Question)
        WHERE question.text CONTAINS {text}
        RETURN question
        '''
    return graph.run(query, text=text)
Пример #6
0
def get_fact_from_id(fact_id):
    query = '''
    MATCH (fact:Fact)
    WHERE fact.id = {fact_id}
    RETURN fact
    '''
    return graph.run(query, fact_id=fact_id)
Пример #7
0
 def delete_fact(self, fact_id):
     query = '''
             MATCH (user:User)-[:PUBLISHED]->(fact:Fact)
             WHERE user.username = {username} AND fact.id = {fact_id}
             DETACH DELETE (fact)
             '''
     return graph.run(query, username=self.username, fact_id=fact_id)
Пример #8
0
 def delete_question(self, question_id):
     query = '''
             MATCH (user:User)-[:PUBLISHED]->(question:Question)
             WHERE user.username = {username} AND question.id = {question_id}
             DETACH DELETE (question)
             '''
     return graph.run(query, username=self.username, question_id=question_id)
Пример #9
0
def get_node_from_id(primary_label, node_id):
    query = f'''
        MATCH (node:{primary_label})
        WHERE node.id = '{node_id}'
        RETURN node
        '''
    return graph.run(query)  #, primary_label=primary_label, node_id=node_id)
Пример #10
0
def get_question_tags_from_id(question_id):
    query = '''
            MATCH (question:Question)<-[:TAGGED]-(tag:Tag)
            WHERE question.id = {question_id}
            RETURN COLLECT(tag.name) AS tags
            '''
    return graph.run(query, question_id=question_id)
Пример #11
0
def get_fact_tags_from_id(fact_id):
    query = '''
            MATCH (fact:Fact)<-[:TAGGED]-(tag:Tag)
            WHERE fact.id = {fact_id}
            RETURN COLLECT(tag.name) AS tags
            '''
    return graph.run(query, fact_id=fact_id)
Пример #12
0
def get_question_from_id(question_id):
    query = '''
        MATCH (question:Question)
        WHERE question.id = {question_id}
        RETURN question
        '''
    return graph.run(query, question_id=question_id)
Пример #13
0
def get_facts_by_tag(tag):
    query = '''
        MATCH (fact:Fact)<-[:TAGGED]-(t:Tag)
        WHERE t.name = {tag}
        RETURN fact
        '''
    return graph.run(query, tag=tag)
Пример #14
0
 def get_recent_questions(self):
     query = '''
     MATCH (user:User)-[:PUBLISHED]->(question:Question)<-[:TAGGED]-(tag:Tag)
     WHERE user.username = {username}
     RETURN question, COLLECT(tag.name) AS tags
     ORDER BY question.timestamp DESC LIMIT 5
     '''
     return graph.run(query, username=self.username)
Пример #15
0
 def get_recent_facts(self):
     query = '''
     MATCH (user:User)-[:PUBLISHED]->(fact:Fact)<-[:TAGGED]-(tag:Tag)
     WHERE user.username = {username}
     RETURN fact, COLLECT(tag.name) AS tags
     ORDER BY fact.timestamp DESC LIMIT 5
     '''
     return graph.run(query, username=self.username)
Пример #16
0
def get_todays_recent_facts():
    query = '''
        MATCH (user:User)-[:PUBLISHED]->(fact:Fact)<-[:TAGGED]-(tag:Tag)
        WHERE fact.date = {today}
        RETURN user.username AS username, fact, COLLECT(tag.name) AS tags
        ORDER BY fact.timestamp DESC LIMIT 5
        '''
    return graph.run(query, today=date())
Пример #17
0
def get_todays_recent_questions():
    query = '''
        MATCH (user:User)-[:PUBLISHED]->(question:Question)<-[:TAGGED]-(tag:Tag)
        WHERE question.date = {today}
        RETURN user.username AS username, question, COLLECT(tag.name) AS tags
        ORDER BY question.timestamp DESC LIMIT 5
        '''
    return graph.run(query, today=date())
Пример #18
0
def get_related_questions(primary_label, node_id):
    query = f'''
        MATCH (node:{primary_label})-[:RELATED_TO]-(question:Question)
        WHERE node.id = '{node_id}'
        RETURN question
        ORDER BY question.timestamp DESC LIMIT 5
        '''
    return graph.run(query)
Пример #19
0
def get_related_facts(primary_label, node_id):
    query = f'''
        MATCH (node:{primary_label})-[:RELATED_TO]-(fact:Fact)
        WHERE node.id = '{node_id}'
        RETURN fact
        ORDER BY fact.timestamp DESC LIMIT 5
        '''
    return graph.run(query)
Пример #20
0
def naive_rule(username):
    """
    Given a username, automatically select a random fact or question that has not been viewed yet.
    Returns the node containing the fact or question.
    """

    query = '''
        MATCH (post)
        WHERE post:Fact OR post:Question
        MATCH (user:User)
        WHERE NOT (user)-[:VIEWED]->(post)
        AND user.username = {username}
        RETURN post
    '''

    results = graph.run(query, username=username)
    results = [x for x in results]

    try:
        return random.choice(results)
    except IndexError:
        pass