Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
combo
Manage
Activity
Members
Labels
Plan
Issues
20
Issue boards
Milestones
Wiki
Redmine
Code
Merge requests
2
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Syntactic Tools
combo
Merge requests
!36
Release 1.0.4
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Release 1.0.4
candidate_release_1.0.4
into
develop
Overview
0
Commits
28
Pipelines
1
Changes
21
Merged
Mateusz Klimaszewski
requested to merge
candidate_release_1.0.4
into
develop
3 years ago
Overview
0
Commits
28
Pipelines
1
Changes
2
Expand
0
0
Merge request reports
Viewing commit
a3774ab6
Prev
Next
Show latest version
2 files
+
92
−
5
Expand all files
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
a3774ab6
Tree and graph merging algorithm.
· a3774ab6
Mateusz Klimaszewski
authored
4 years ago
combo/utils/graph.py
+
76
−
0
Options
@@ -3,6 +3,82 @@ from typing import List
import
numpy
as
np
_ACL_REL_CL
=
"
acl:relcl
"
def
graph_and_tree_merge
(
tree_arc_scores
,
tree_rel_scores
,
graph_arc_scores
,
graph_rel_scores
,
label2idx
,
idx2label
,
tokens
):
graph_arc_scores
=
np
.
copy
(
graph_arc_scores
)
# Exclude self-loops, in-place operation.
np
.
fill_diagonal
(
graph_arc_scores
,
0
)
# Connection to root will be handled by tree.
graph_arc_scores
[:,
0
]
=
False
# The same with labels.
root_idx
=
label2idx
[
"
root
"
]
graph_rel_scores
[:,
:,
root_idx
]
=
-
float
(
'
inf
'
)
graph_rel_pred
=
graph_rel_scores
.
argmax
(
-
1
)
# Add tree edges to graph
tree_heads
=
[
0
]
+
tree_arc_scores
graph
=
[[]
for
_
in
range
(
len
(
tree_heads
))]
labeled_graph
=
[[]
for
_
in
range
(
len
(
tree_heads
))]
for
d
,
h
in
enumerate
(
tree_heads
):
if
not
d
:
continue
label
=
idx2label
[
tree_rel_scores
[
d
-
1
]]
if
label
!=
_ACL_REL_CL
:
graph
[
h
].
append
(
d
)
labeled_graph
[
h
].
append
((
d
,
label
))
# Debug only
# Extract graph edges
graph_edges
=
np
.
argwhere
(
graph_arc_scores
)
# Add graph edges which aren't creating a cycle
for
(
d
,
h
)
in
graph_edges
:
if
not
d
or
not
h
or
d
in
graph
[
h
]:
continue
try
:
path
=
next
(
_dfs
(
graph
,
d
,
h
))
except
StopIteration
:
# There is not path from d to h
label
=
idx2label
[
graph_rel_pred
[
d
][
h
]]
if
label
!=
_ACL_REL_CL
:
graph
[
h
].
append
(
d
)
labeled_graph
[
h
].
append
((
d
,
label
))
# Add 'acl:relcl' without checking for cycles.
for
d
,
h
in
enumerate
(
tree_heads
):
if
not
d
:
continue
label
=
idx2label
[
tree_rel_scores
[
d
-
1
]]
if
label
==
_ACL_REL_CL
:
graph
[
h
].
append
(
d
)
labeled_graph
[
h
].
append
((
d
,
label
))
assert
len
(
labeled_graph
[
0
])
==
1
d
=
graph
[
0
][
0
]
graph
[
d
].
append
(
0
)
labeled_graph
[
d
].
append
((
0
,
"
root
"
))
parse_graph
=
[[]
for
_
in
range
(
len
(
tree_heads
))]
for
h
in
range
(
len
(
tree_heads
)):
for
d
,
label
in
labeled_graph
[
h
]:
parse_graph
[
d
].
append
((
h
,
label
))
parse_graph
[
d
]
=
sorted
(
parse_graph
[
d
])
for
i
,
g
in
enumerate
(
parse_graph
):
heads
=
[
x
[
0
]
for
x
in
g
]
rels
=
[
x
[
1
]
for
x
in
g
]
deps
=
'
|
'
.
join
(
f
'
{
h
}
:
{
r
}
'
for
h
,
r
in
zip
(
heads
,
rels
))
tokens
[
i
-
1
][
"
deps
"
]
=
deps
return
def
sdp_to_dag_deps
(
arc_scores
,
rel_scores
,
tree_tokens
:
List
,
root_idx
=
0
,
vocab_index
=
None
)
->
None
:
# adding ROOT