forked from elishowk/cablegate_semnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datamodel.py
91 lines (83 loc) · 2.74 KB
/
datamodel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
# Copyright (C) 2010 elishowk@nonutc.fr
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from hashlib import sha256
import logging
logging.basicConfig(level=logging.DEBUG, format="%(levelname)-8s %(message)s")
def getNodeLabel(tokens):
"""
method forming clean unicode labels from token list
"""
label = " ".join(tokens)
if not isinstance(label, unicode):
return unicode(label , "utf_8", errors='ignore')
else:
return label
def updateNodeEdges(updateedges, toupdate):
"""
increments an object's edges with the candidate object's edges
"""
for targettype in updateedges.iterkeys():
for targetid, weight in updateedges[targettype].iteritems():
toupdate.addEdge( targettype, targetid, weight )
return toupdate
def getNodeId(tokens):
"""
Common method constructing an ID str for all GraphNode objects
"""
convert = getNodeLabel(tokens).lower()
return sha256( convert.encode( 'ascii', 'replace' ) ).hexdigest()
def addUniqueEdge( node, type, key, value ):
"""
low level method writing ONLY ONCE a weighted edge
"""
if 'edges' not in node:
node['edges']={}
if type not in node['edges']:
node['edges'][type]={}
if key in node['edges'][type]:
return node
else:
node['edges'][type][key] = value
return node
def overwriteEdge( node, type, key, value ):
"""
low level method overwriting a weighted edge
"""
if 'edges' not in node:
node['edges']={}
if type not in node['edges']:
node['edges'][type]={}
node['edges'][type][key] = value
return node
def addEdge( node, type, key, value ):
"""
low level method incrementing a weighted edge
"""
if 'edges' not in node:
node['edges']={}
if type not in node['edges']:
node['edges'][type]={}
if key in node['edges'][type]:
node['edges'][type][key] += value
else:
node['edges'][type][key] = value
return node
def initEdges(node):
if 'edges' not in node:
node['edges']={}
node["edges"]['NGram'] = {}
node["edges"]['Document'] = {}
return node