<commit_before>////
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
////
[[centrality]]
Centrality
----------

There are many measures of link:https://en.wikipedia.org/wiki/Centrality[centrality] which are meant to help identify
the most important vertices in a graph. As these measures are common in graph theory, this section attempts to
demonstrate how some of these different indicators can be calculated using Gremlin.

[[degree-centrality]]
Degree Centrality
~~~~~~~~~~~~~~~~~

link:https://en.wikipedia.org/wiki/Centrality#Degree_centrality[Degree centrality] is a measure of the number of
edges associated to each vertex.

[gremlin-groovy,modern]
----
g.V().group().by().by(bothE().count())                <1>
g.V().group().by().by(inE().count())                  <2>
g.V().group().by().by(outE().count())                 <3>
g.V().project("v","degree").by().by(bothE().count())  <4>
g.V().project("v","degree").by().by(bothE().count()). <5>
  order().by(select("degree"), decr).
  limit(4)
----

<1> Calculation of degree centrality which counts all incident edges on each vertex to include those that are both
incoming and outgoing.
<2> Calculation of in-degree centrality which only counts incoming edges to a vertex.
<3> Calculation of out-degree centrality which only counts outgoing edges from a vertex.
<4> The previous examples all produce a single `Map` as their output. While that is a desireable output, producing a
stream of `Map` objects can allow some greater flexibility.
<5> For example, use of a stream enables use of an ordered limit that can be executed in a distributed fashion in
OLAP traversals.<commit_msg>Added a NOTE to the centrality recipe to clarify the usage of by() CTR<commit_after>////
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
////
[[centrality]]
Centrality
----------

There are many measures of link:https://en.wikipedia.org/wiki/Centrality[centrality] which are meant to help identify
the most important vertices in a graph. As these measures are common in graph theory, this section attempts to
demonstrate how some of these different indicators can be calculated using Gremlin.

[[degree-centrality]]
Degree Centrality
~~~~~~~~~~~~~~~~~

link:https://en.wikipedia.org/wiki/Centrality#Degree_centrality[Degree centrality] is a measure of the number of
edges associated to each vertex.

[gremlin-groovy,modern]
----
g.V().group().by().by(bothE().count())                <1>
g.V().group().by().by(inE().count())                  <2>
g.V().group().by().by(outE().count())                 <3>
g.V().project("v","degree").by().by(bothE().count())  <4>
g.V().project("v","degree").by().by(bothE().count()). <5>
  order().by(select("degree"), decr).
  limit(4)
----

<1> Calculation of degree centrality which counts all incident edges on each vertex to include those that are both
incoming and outgoing.
<2> Calculation of in-degree centrality which only counts incoming edges to a vertex.
<3> Calculation of out-degree centrality which only counts outgoing edges from a vertex.
<4> The previous examples all produce a single `Map` as their output. While that is a desireable output, producing a
stream of `Map` objects can allow some greater flexibility.
<5> For example, use of a stream enables use of an ordered limit that can be executed in a distributed fashion in
OLAP traversals.

NOTE: The link:http://tinkerpop.apache.org/docs/x.y.z/reference/#group-step[group] step takes up to two separate
link:http://tinkerpop.apache.org/docs/x.y.z/reference/#by-step[by] modulators. The first `by()` tells `group()`
what the key in the resulting `Map` will be (i.e. the value to group on). In the above examples, the `by()` is empty
and as a result, the grouping will be on the incoming `Vertex` object itself. The second `by()` is the value to be
stored in the `Map` for each key.
{'fraction_non_alphanumeric': 0.11026180011465699, 'fraction_numerical': 0.005732849226065355, 'mean_word_length': 4.1668311944718655, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7642272', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1391, 'n_words': 660}