def test_P(self): # verify that the order of operations is respected assert "and(eq(a),lt(b))" == str(P.eq("a").and_(P.lt("b"))) assert "and(or(lt(b),gt(c)),neq(d))" == str( P.lt("b").or_(P.gt("c")).and_(P.neq("d"))) assert "and(or(lt(b),gt(c)),or(neq(d),gte(e)))" == str( P.lt("b").or_(P.gt("c")).and_(P.neq("d").or_(P.gte("e"))))
def test_P(self): result = {'@type': 'g:P', '@value': { 'predicate': 'and', 'value': [{ '@type': 'g:P', '@value': { 'predicate': 'or', 'value': [{ '@type': 'g:P', '@value': {'predicate': 'lt', 'value': 'b'} }, {'@type': 'g:P', '@value': {'predicate': 'gt', 'value': 'c'}} ] } }, {'@type': 'g:P', '@value': {'predicate': 'neq', 'value': 'd'}}]}} assert result == json.loads( self.graphson_writer.writeObject(P.lt("b").or_(P.gt("c")).and_(P.neq("d")))) result = {'@type': 'g:P', '@value': {'predicate':'within','value': [{"@type": "g:Int32", "@value": 1},{"@type": "g:Int32", "@value": 2}]}} assert result == json.loads(self.graphson_writer.writeObject(P.within([1, 2]))) assert result == json.loads(self.graphson_writer.writeObject(P.within(1, 2))) result = {'@type': 'g:P', '@value': {'predicate':'within','value': [{"@type": "g:Int32", "@value": 1}]}} assert result == json.loads(self.graphson_writer.writeObject(P.within([1]))) assert result == json.loads(self.graphson_writer.writeObject(P.within(1)))
def test_P(self): result = {'@type': 'g:P', '@value': { 'predicate': 'and', 'value': [{ '@type': 'g:P', '@value': { 'predicate': 'or', 'value': [{ '@type': 'g:P', '@value': {'predicate': 'lt', 'value': 'b'} }, {'@type': 'g:P', '@value': {'predicate': 'gt', 'value': 'c'}} ] } }, {'@type': 'g:P', '@value': {'predicate': 'neq', 'value': 'd'}}]}} assert result == json.loads( self.graphson_writer.writeObject(P.lt("b").or_(P.gt("c")).and_(P.neq("d")))) result = {'@type': 'g:P', '@value': {'predicate':'within','value': [{"@type": "g:Int32", "@value": 1},{"@type": "g:Int32", "@value": 2}]}} assert result == json.loads(self.graphson_writer.writeObject(P.within([1, 2]))) assert result == json.loads(self.graphson_writer.writeObject(P.within(1, 2))) result = {'@type': 'g:P', '@value': {'predicate':'within','value': [{"@type": "g:Int32", "@value": 1}]}} assert result == json.loads(self.graphson_writer.writeObject(P.within([1]))) assert result == json.loads(self.graphson_writer.writeObject(P.within(1)))
def recommend_similar_audience(g, website_url, categories_limit=3, search_time_limit_in_seconds=15): """Given website url, categories_limit, categories_coin recommend similar audience in n most popular categories. Similar audience - audience of users that at least once visited subpage of domain that contains IAB-category codes that are most popular across users of given website """ average_guy = (g.V(website_url).in_("visited").in_( "has_identity").dedup().hasLabel("persistentId").group().by().by( out("has_identity").out("visited").in_("links_to").groupCount().by( "categoryCode")).select( Column.values).unfold().unfold().group().by( Column.keys).by(select( Column.values).mean()).unfold().order().by( Column.values, Order.desc).limit(categories_limit)) most_popular_categories = dict( chain(*category.items()) for category in average_guy.toList()) guy_stats_subquery = (out("has_identity").out("visited").in_( "links_to").groupCount().by("categoryCode").project( *most_popular_categories.keys())) conditions_subqueries = [] for i in most_popular_categories: guy_stats_subquery = guy_stats_subquery.by( choose(select(i), select(i), constant(0))) conditions_subqueries.append( select(Column.values).unfold().select(i).is_( P.gt(int(most_popular_categories[i])))) return (g.V().hasLabel("websiteGroup").has( "categoryCode", P.within(list( most_popular_categories.keys()))).out("links_to").in_("visited"). dedup().in_("has_identity").dedup().hasLabel("persistentId").where( out("has_identity").out("visited").has( "url", P.neq(website_url))).timeLimit( search_time_limit_in_seconds * 1000).local( group().by().by(guy_stats_subquery).where( or_(*conditions_subqueries))).select( Column.keys).unfold().out( "has_identity").values("uid"))
def people_you_may_know(g, user_name, limit=10): from gremlin_python.process.traversal import Scope, Column, Order recommendations = (g.V().hasLabel('person').has('_name', user_name.lower()).as_('person'). both('knows').aggregate('friends'). both('knows'). where(P.neq('person')).where(P.without('friends')). groupCount().by('id'). order(Scope.local).by(Column.values, Order.decr). next()) vertex_scores = [(key, score) for key, score in recommendations.items()][:limit] res = [] for key, score in vertex_scores: value = {k: v for k, v in g.V(key).valueMap().next().items() if not (k == 'id' or k.startswith('_'))} value['score'] = float(score) res.append(value) return res
def _query_users_activities_stats(g, website_url, most_popular_categories, search_time_limit_in_seconds=30): return (g.V().hasLabel("websiteGroup").has( "categoryCode", P.within(list( most_popular_categories.keys()))).out("links_to").in_("visited"). dedup().in_("has_identity").dedup().hasLabel("persistentId").where( out("has_identity").out("visited").has( "url", P.neq(website_url))).timeLimit( search_time_limit_in_seconds * 1000). local(group().by().by( out("has_identity").out("visited").in_( "links_to").groupCount().by("categoryCode")).project( "pid", "iabs", "tids").by(select(Column.keys).unfold()).by( select(Column.values).unfold()).by( select(Column.keys).unfold().out( "has_identity").values("uid").fold())))
def test_P(self): result = {'@type': 'g:P', '@value': { 'predicate': 'and', 'value': [{ '@type': 'g:P', '@value': { 'predicate': 'or', 'value': [{ '@type': 'g:P', '@value': {'predicate': 'lt', 'value': 'b'} }, {'@type': 'g:P', '@value': {'predicate': 'gt', 'value': 'c'}} ] } }, {'@type': 'g:P', '@value': {'predicate': 'neq', 'value': 'd'}}]}} assert result == json.loads( self.graphson_writer.writeObject(P.lt("b").or_(P.gt("c")).and_(P.neq("d"))))
def handler(event, context): graph = Graph() g = graph.traversal().withRemote(DriverRemoteConnection(myNeptuneEndpoint,"g")) toReturn = g.V(event["pathParameters"]["bookId"]).project("friendsPurchased","purchased") \ .by(in_("purchased").dedup().where(id().is_(P.neq(event["pathParameters"]["bookId"]))).id().fold()).by(in_("purchased").count()).toList() response = { "statusCode": 200, "headers": { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Credentials": True }, "body": json.dumps(toReturn) } print(response) return response
def test_P(self): assert """{"@type":"g:P","@value":{"predicate":"and","value":[{"@type":"g:P","@value":{"predicate":"or","value":[{"@type":"g:P","@value":{"predicate":"lt","value":"b"}},{"@type":"g:P","@value":{"predicate":"gt","value":"c"}}]}},{"@type":"g:P","@value":{"predicate":"neq","value":"d"}}]}}""" == self.graphson_writer.writeObject( P.lt("b").or_(P.gt("c")).and_(P.neq("d")))
def test_P(self): # verify that the order of operations is respected assert "and(eq(a),lt(b))" == str(P.eq("a").and_(P.lt("b"))) assert "and(or(lt(b),gt(c)),neq(d))" == str(P.lt("b").or_(P.gt("c")).and_(P.neq("d"))) assert "and(or(lt(b),gt(c)),or(neq(d),gte(e)))" == str( P.lt("b").or_(P.gt("c")).and_(P.neq("d").or_(P.gte("e"))))
def test_P(self): assert """{"@type":"g:P","@value":{"predicate":"and","value":[{"@type":"g:P","@value":{"predicate":"or","value":[{"@type":"g:P","@value":{"predicate":"lt","value":"b"}},{"@type":"g:P","@value":{"predicate":"gt","value":"c"}}]}},{"@type":"g:P","@value":{"predicate":"neq","value":"d"}}]}}""" == self.graphson_writer.writeObject( P.lt("b").or_(P.gt("c")).and_(P.neq("d")))