How to group "match" results - groovy

My question is "how to group the match results" of select-ed items ?
Here is the structure and data I have in my graph :
Structure :
5 kind of vertices : user / experience / school / company / tag
5 kind of edges : studied / worked /school / company / speaks
Data :
user --[worked]--> experience --[company]--> company
user --[worked]--> experience --[company]--> company
user --[stidued]--> experience --[school]--> school
user --[stidued]--> experience --[school]--> school
user --[speaks (level: x)]--> language
user --[speaks (level: x)]--> language
I already wrote something close to what I need with match, dedup and unfold
g.V(16520).match(
__.as('user'),
__.as('user').out('worked').as('workExperiences'),
__.as('user').out('studied').as('schoolExperiences'),
__.as('workExperiences').out('company').as('company'),
__.as('schoolExperiences').out('school').as('school'),
__.as('user').outE('speaks').as('a').values('level').as('level').select('a').inV().values('name').as('language').select('level', 'language').as('languages')
).select('user', 'schoolExperiences', 'school', 'workExperiences', 'company', 'languages')
.unfold().dedup()
Here is what this query gives me :
==>user=v[16520]
==>schoolExperiences=v[4184]
==>school=v[4232]
==>workExperiences=v[12496]
==>company=v[8320]
==>languages={level=6, language=DEU}
==>languages={level=3, language=FRA}
==>schoolExperiences=v[16424]
==>school=v[4136]
==>workExperiences=v[16512]
==>company=v[4176]
I need to find a query that return this kind of result :
==>user=[v[16520]]
==>languages=[{level=6, language=DEU},{level=3, language=FRA}]
==>workExperiences=[v[12496], v[16512]]
==>schoolExperiences=[v[4184], v[16424]]
==>company=[v[4176], v[8320]]
==>school=[v[4136], v[4232]]
I can't manage to find a solution.
Any advice would be appriciated :)
Thanks for reading
F.
PS : I'm running a v3.0.1-incubating Tinkerpop version

Thanks to #DanielKuppitz here is the solution I needed :
g.V(16520).as('user').match(
__.as('user').out('worked').as('experience').out('company').as('companyId').select('experience', 'companyId').fold().as('tmpWorkExperiences'),
__.as('user').out('studied').as('experience').out('school').as('schoolId').select('experience', 'schoolId').fold().as('tmpSchoolExperiences'),
__.as('user').outE('speaks').as('level').inV().as('language').select('level', 'language').by('level').by('name').fold().as('languages')
).select('tmpWorkExperiences').map(unfold().select('experience', 'companyId').by().by(id).fold()).as('workExperiences').
select('tmpWorkExperiences').map(unfold().select('companyId').fold()).as('company').
select('tmpSchoolExperiences').map(unfold().select('experience', 'schoolId').by().by(id).fold()).as('schoolExperiences').
select('tmpSchoolExperiences').map(unfold().select('schoolId').fold()).as('school').
select('workExperiences', 'schoolExperiences', 'company', 'school', 'languages').unfold()
And here is the result :
==>workExperiences=[{experience=v[12496], companyId=8320}, {experience=v[16512], companyId=4176}]
==>schoolExperiences=[{experience=v[4184], schoolId=4232}, {experience=v[16424], schoolId=4136}]
==>company=[v[8320], v[4176]]
==>school=[v[4232], v[4136]]
==>languages=[{level=6, language=DEU}, {level=3, language=FRA}]

Related

Odoo select all records where user is follower

Can somebody give me example (or hint) how to select all records where user is follower?
I cant find any information about handling followers. What the condition should be?
find_record = self.env['my_class'].search([( **user is floower** )])
Followers are related to partners, you can select records where current user related partner is in followers:
self.env['sale.order'].search([('message_follower_ids.partner_id', '=', self.env.user.partner_id.id)])
try the following
first element of the tuple : the partner id related to the user
2nd element of the tuple : the logical operator 'in'
3rd element of the tuple : the id of partners following the record
self.env['helpdesk.ticket'].search([(self.env.user.partner_id.id, 'in', 'message_follower_ids.partner_id')])

ArangoDb AQL Graph queries traversal example

I am having some trouble wrapping my head around how to traverse a certain graph to extract some data.
Given a collection of "users" and a collection of "places".
And a "likes" edge collection to denote that a user likes a certain place. The "likes" edge collection also has a "review" property to store a user's review about the place.
And a "follows" edge collection to denote that a user follows another user.
How can I traverse the graph to fetch all the places that I like with my review of the place and the reviews of the users I follow that also like the same place.
for example, in the above graph. I am user 6327 and I reviewed both places(7968 and 16213)
I also follow user 6344 which also happens to have reviewed the place 7968.
How can I get all the places that I like and the reviews of the people that I follow who also reviewed the same place that I like.
an expected output would be something like the following:
[
{
name:"my name",
place: "place 1",
id: 1
review,"my review about place 1"
},
{
name:"my name",
place: "place 2",
id: 2
review,"my review about place 2"
},
{
name:"name of the user I follow",
place: "place 2",
id: 2
review,"review about place 2 from the user I follow"
}
]
There are a number of ways to do this query, and it also depends on where you want to add parameters, but for the sake of simplicity I've built this quite verbose query below to help you understand one way of approaching the problem.
One way is to determine the _id of your user record, then find all the _id's of the friends you follow, and then to work out all related reviews in one query.
I take a different approach below, and that is to:
Determine the reviews you have written
Determine who you follow
Determine the reviews the people you follow have written
Merge together your reviews with those of the people you follow
It is possible to merge these queries together more optimally, but I thought it worth breaking them out like this (and showing the output of each stage as well as the final answer) to help you see what data is available.
A key thing to understand about AQL graph queries is how you have access to vertices, edges, and paths when you perform a query.
A path is an object in it's own right and it's worth investigating the contents of that object to better understand how to exploit it for path information.
This query assumes:
users document collection contains users
places document collection contains places
follows edge collection tracks users following other users
reviews edge collection tracks reviews people wrote
Note: When providing an id on each record I used the id of the review, because if you know that id you can fetch the edge document and get the id of both the user and the place as well as read all the data about the review.
LET my_reviews = (
FOR vertices, edges, paths IN 1..1 OUTBOUND "users/6327" reviews
RETURN {
name: FIRST(paths.vertices).name,
review_id: FIRST(paths.edges)._id,
review: FIRST(paths.edges).review,
place: LAST(paths.vertices).place
}
)
LET who_i_follow = (
FOR v IN 1..1 OUTBOUND "users/6327" follows
RETURN v
)
LET reviews_of_who_i_follow = (
FOR users IN who_i_follow
FOR vertices, edges, paths in 1..1 OUTBOUND users._id reviews
RETURN {
name: FIRST(paths.vertices).name,
review_id: FIRST(paths.edges)._id,
review: FIRST(paths.edges).review,
place: LAST(paths.vertices).place
}
)
RETURN {
my_reviews: my_reviews,
who_i_follow: who_i_follow,
reviews_of_who_i_follow: reviews_of_who_i_follow,
merged_reviews: UNION(my_reviews, reviews_of_who_i_follow)
}
The first vertex in paths.vertices is the starting vertex (users/6327)
The last vertex in paths.vertices is the end of the path, e.g. who you follow
The first edge in paths.edges is the review that the user made of the place
Here is another more compact version of the query that takes a param, the _id of the user that is 'you'.
LET target_users = APPEND(TO_ARRAY(#user), (
FOR v IN 1..1 OUTBOUND #user follows RETURN v._id
))
LET selected_reviews = (
FOR u IN target_users
FOR vertices, edges, paths in 1..1 OUTBOUND u reviews
LET user = FIRST(paths.vertices)
LET place = LAST(paths.vertices)
LET review = FIRST(paths.edges)
RETURN {
name: user.name,
review_id: review._id,
review: review.review,
place: place.place
}
)
RETURN selected_reviews

Find all references to a supplied noun in StanfordNLP

I'm trying to parse some text to find all references to a particular item. So, for example, if my item was The Bridge on the River Kwai and I passed it this text, I'd like it to find all the instances I've put in bold.
The Bridge on the River Kwai is a 1957 British-American epic war film
directed by David Lean and starring William Holden, Jack Hawkins, Alec
Guinness, and Sessue Hayakawa. The film is a work of fiction, but
borrows the construction of the Burma Railway in 1942–1943 for its
historical setting. The movie was filmed in Ceylon (now Sri Lanka).
The bridge in the film was near Kitulgala.
So far my attempt has been to go through all the mentions attached to each CorefChain and loop through those hunting for my target string. If I find the target string, I add the whole CorefChain, as I think this means the other items in that CorefChain also refer to the same thing.
List<CorefChain> gotRefs = new ArrayList<CorefChain>();
String pQuery = "The Bridge on the River Kwai";
for (CorefChain cc : document.get(CorefCoreAnnotations.CorefChainAnnotation.class).values()) {
List<CorefChain.CorefMention> corefMentions = cc.getMentionsInTextualOrder();
boolean addedChain = false;
for (CorefChain.CorefMention cm : corefMentions) {
if ((!addedChain) &&
(pQuery.equals(cm.mentionSpan))) {
gotRefs.add(cc);
addedChain = true;
}
}
}
I then loop through this second list of CorefChains, re-retrieve the mentions for each chain and step through them. In that loop I show which sentences have a likely mention of my item in a sentence.
for (CorefChain gr : gotRefs) {
List<CorefChain.CorefMention> corefMentionsUsing = gr.getMentionsInTextualOrder();
for (CorefChain.CorefMention cm : corefMentionsUsing) {
//System.out.println("Got reference to " + cm.mentionSpan + " in sentence #" + cm.sentNum);
}
}
It finds some of my references, but not that many, and it produces a lot of false positives. As might be entirely apparently from reading this, I don't really know the first thing about NLP - am I going about this entirely the wrong way? Is there a StanfordNLP parser that will already do some of what I'm after? Should I be training a model in some way?
I think a problem with your example is that you are looking for references to a movie title, and there isn't support in Stanford CoreNLP for recognizing movie titles, book titles, etc...
If you look at this example:
"Joe bought a laptop. He is happy with it."
You will notice that it connects:
"Joe" -> "He"
and
"a laptop" -> "it"
Coreference is an active research area and even the best system can only really be expected to produce an F1 of around 60.0 on general text, meaning it will often make errors.

making link between a person name and pronoun in GATE

Is it possible to make a link between a person name and its PRP? in GATE E.g i have document "Maria Sharapova is a tennis player from russia. She participates in international tennis tournament. She is known for winning Wimbledon, US Open and Australian Open titles as for her looks, decibel-breaking grunts and commercial savvy - all of which made her the world's highest-paid female athlete." i want to annotate the "she" as "Maria Sharapova". I have written the following JAPE rule which identifies a pattern having a PRP after a person name
Phase: Simple
Input: Lookup Token Split
Options: control = appelt
Rule:joblookup
(
({Lookup.majorType == person_first}|
{Lookup.majorType == person_full})
({Token.kind==word})+
{Split.kind==internal}
{Token.category==PRP}
):sameathlete
-->
:sameathlete.sameAthlete1 = {kind="athlete", rule="same-athlete"}
How can i make the annotation that from She means we are talking about the same person whose name is mentioned 1 or 2 sentence before??
Please help
Have you tried Co-reference PR for gate?

neo4j nodejs strength or properties's relationship modified : how to do it?

I meet a problem with count request of neo4j with nodejs.
Here my problem :
When I insert a data, it will present like this:
start a = node(0)
create unique a-[:HAS_ID]->(b{id:'xx'})
create unique b-[:HAS_INFO]->(c{info:'xx'})
return a,b,c;
because it's unique node, so that it will not insert a new node if there are a same node exist. But, I wanna count how many request to call this query.
Ex :
request: -domain/id01/info
--return a node[0], b node[1] and c node[2]
add another data:
request: -domain/id02/info
-- return : a node[0], b node[3], c node[4]
call it again :
request: -domain/id01/info
--return a node[0], b node[1] and c node[2] //but here is any attribute or properties count to 2.
I've read any solution about strength. It told me create an properties of relationship as example :
[:HAS_INFO{strength:num}]
and let it increase but I still don't get it.
Anyone please give me solution and tell me how to do it.
Thank you.
more info : Representing (and incrementing) relationship strength in Neo4j
You can use the CASE statement, see http://gist.neo4j.org/?6052414 for an example. Feel free to for the underlying gist and improve it!
MATCH path=(a)-[rel:HAS_INFO]->(b)
WHERE a.name?='A' AND b.name?='Info'
SET rel.weight = (
CASE
WHEN not(has(rel.weight))
THEN 0
ELSE rel.weight + 1
END)
RETURN path, rel.weight;

Resources