Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
obc2kg
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
born
obc2kg
Commits
22fe70c4
Commit
22fe70c4
authored
5 years ago
by
opitz
Browse files
Options
Downloads
Patches
Plain Diff
added comments
parent
22ddc850
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/graph_helpers.py
+19
-4
19 additions, 4 deletions
src/graph_helpers.py
with
19 additions
and
4 deletions
src/graph_helpers.py
+
19
−
4
View file @
22fe70c4
...
@@ -23,6 +23,11 @@ def contract_gender(G, gender="male"):
...
@@ -23,6 +23,11 @@ def contract_gender(G, gender="male"):
return
G
,
vns
[
0
][
0
]
return
G
,
vns
[
0
][
0
]
def
simplify_text_description_nodes
(
G
,
node_index_dict
,
mode
=
"
None
"
,
min_freq
=
1
):
def
simplify_text_description_nodes
(
G
,
node_index_dict
,
mode
=
"
None
"
,
min_freq
=
1
):
"""
function takes our graph and simplifies text description nodes
E.g., felouneously stealing, on the 10th Decembre, two silver watches ----> watches
"""
if
mode
==
"
None
"
:
if
mode
==
"
None
"
:
return
G
,
node_index_dict
return
G
,
node_index_dict
#collect all descriptions and their neighbor category
#collect all descriptions and their neighbor category
...
@@ -32,32 +37,42 @@ def simplify_text_description_nodes(G,node_index_dict,mode="None",min_freq=1):
...
@@ -32,32 +37,42 @@ def simplify_text_description_nodes(G,node_index_dict,mode="None",min_freq=1):
trialnodes
=
[
n
for
n
in
G
.
nodes
(
data
=
True
)
if
isinstance
(
n
[
1
][
"
nodeobj
"
],
dh
.
TrialNode
)]
trialnodes
=
[
n
for
n
in
G
.
nodes
(
data
=
True
)
if
isinstance
(
n
[
1
][
"
nodeobj
"
],
dh
.
TrialNode
)]
descr_nodes
=
[]
descr_nodes
=
[]
mask
=
[]
mask
=
[]
# we iterate over all trials
for
i
,
tn
in
enumerate
(
trialnodes
):
for
i
,
tn
in
enumerate
(
trialnodes
):
#get corresponding cat node
#get corresponding cat node
catn
=
[
n
for
n
in
G
.
neighbors
(
tn
[
0
])
if
isinstance
(
G
.
nodes
[
n
][
"
nodeobj
"
],
dh
.
OffenceNode
)][
0
]
catn
=
[
n
for
n
in
G
.
neighbors
(
tn
[
0
])
if
isinstance
(
G
.
nodes
[
n
][
"
nodeobj
"
],
dh
.
OffenceNode
)][
0
]
catn
=
[
catn
,
G
.
nodes
[
catn
]]
catn
=
[
catn
,
G
.
nodes
[
catn
]]
#print(catn)
#catn=[cat]
category
=
catn
[
1
][
"
nodeobj
"
].
d
[
"
category
"
]
category
=
catn
[
1
][
"
nodeobj
"
].
d
[
"
category
"
]
descr_vectors
=
None
descr_vectors
=
None
tid
=
None
tid
=
None
# we iterate over all neighbors of the trial
for
nb
in
G
[
tn
[
0
]]:
for
nb
in
G
[
tn
[
0
]]:
for
edge_id
in
G
[
tn
[
0
]][
nb
]:
for
edge_id
in
G
[
tn
[
0
]][
nb
]:
# and grab nodes which describe a offence
if
G
[
tn
[
0
]][
nb
][
edge_id
][
"
edge_class
"
]
==
"
with-offence-description
"
:
if
G
[
tn
[
0
]][
nb
][
edge_id
][
"
edge_class
"
]
==
"
with-offence-description
"
:
#print(G.nodes[nb])
# we collect the noun chunk vectors
descr_vectors
,
_
=
G
.
nodes
[
nb
][
"
nodeobj
"
].
get_noun_chunk_vectors
()
descr_vectors
,
_
=
G
.
nodes
[
nb
][
"
nodeobj
"
].
get_noun_chunk_vectors
()
descr_nodes
.
append
(
G
.
nodes
[
nb
][
"
nodeobj
"
])
descr_nodes
.
append
(
G
.
nodes
[
nb
][
"
nodeobj
"
])
#descr_nodes[-1].simplify_to_direct_object()
tid
=
(
tn
[
0
],
nb
)
tid
=
(
tn
[
0
],
nb
)
Xid
.
append
(
tid
)
Xid
.
append
(
tid
)
for
dv
in
descr_vectors
:
for
dv
in
descr_vectors
:
#put noun chunk vector into training data
Xvector
.
append
(
dv
)
Xvector
.
append
(
dv
)
#put label into training data
related_cat
.
append
(
category
)
related_cat
.
append
(
category
)
if
mode
==
"
classifier
"
:
if
mode
==
"
classifier
"
:
#fit a classifier to learn a mapping between noun chunks and labels
clf
=
LogisticRegression
()
clf
=
LogisticRegression
()
clf
.
fit
(
Xvector
,
related_cat
)
clf
.
fit
(
Xvector
,
related_cat
)
# now we can remove the textdescription nodes and insert their simplified fporms
for
i
,
idx
in
enumerate
(
Xid
):
for
i
,
idx
in
enumerate
(
Xid
):
if
idx
[
1
]
in
G
:
if
idx
[
1
]
in
G
:
G
.
remove_node
(
idx
[
1
])
G
.
remove_node
(
idx
[
1
])
#node_index_dict.pop(descr_nodes[i])
#node_index_dict.pop(descr_nodes[i])
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment