-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertGEDCOM.py
141 lines (106 loc) · 3.48 KB
/
convertGEDCOM.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"""
Convert File
"""
import json
from neo4j import GraphDatabase
GEDCOM_FILE = 'Beck Family Tree.ged'
def parseName(nameElementDict)
def readGEDCOMfile(filename):
file = open(GEDCOM_FILE, 'r')
# each objects starts at level 0 and is followed by an @
obj = file.read().split(('\n0 '))
file.close()
return obj
def splitElement(element):
#split an element into Key:Value or
#parent : [sub-elelment]
if len(element.strip())==0:
# do nothing !
return [0,0]
try:
[a, b] = element.replace('\n',' \n').lstrip().split(' ',1)
except ValueError:
return [0,0]
else:
return [a,b]
def insertObject(keyStr, value, objDict):
if keyStr in objDict.keys():
#is it already a list
if type(objDict[keyStr]) is list:
objDict[keyStr].append(value)
else:
#make it a list
newList = []
# get existing key/value
newList.append(objDict.pop(keyStr))
# add the new value to the list
newList.append(value)
objDict.update({keyStr:newList})
else:
newList = []
newList.append(value)
objDict.update({keyStr:newList})
return objDict
def parseName(name):
# split name: "Roland /Beck/"
# into:
# fullname: Roland Beck
# firstname: Roland
# lastname: Beck
firstname = ''
lastname = ''
fullname = name
if name.find('/') > 0:
[firstname, lastname] = name.split('/',1)
lastname = lastname.rstrip('/')
fullname = firstname + ' ' + lastname
return [firstname, lastname, fullname]
def parseObject(obj, level=0):
objDict = {}
objArray = obj.split('\n'+str(level+1))
if level == 0:
idLine = objArray.pop(0)
[id, elementType] = splitElement(idLine)
objDict = insertObject(elementType.strip(), id.strip(), objDict)
#objDict.update({type.strip():id.strip()})
#objDict.update({'ElementType':type})
for element in objArray:
if element.count('\n')==0:
# simple element
[keyString, valueString] = splitElement(element)
if keyString != 0:
objDict.update({keyString.strip() : valueString})
else:
# element has sub-elements
# check if first line has TAG + VALUE
# **************** HERE ***************
[parent, children] = splitElement(element)
subElem = parseObject(children, level+1)
if parent != 0:
objDict = insertObject(parent.strip(), subElem, objDict)
#objDict.update({parent:subElem})
return objDict
# --
OBJECTS = readGEDCOMfile(GEDCOM_FILE)
tree = []
for i in range(1,len(OBJECTS)):
tree.append(parseObject(OBJECTS[i], 0))
#print(json.dumps(tree))
for elem in tree:
if 'INDI' in elem.keys():
[firstname, lastname, fullname] = parseName(elem["NAME"])
print([firstname, lastname, fullname])
# DATABASE STUFF
def addIndividualToGraph(tx, individual):
# individual is a dict that needs at a mininmum
# - ancestryID
# - sex
# - name
result = tx.run("match (a:Male {firstname: $name}) return a.firstname as firstname, ID(a) as id",
name=individual)
return result
#driver = GraphDatabase.driver("bolt://192.168.2.130:7687")
#with driver.session() as session:
# result = session.read_transaction(addIndividualToGraph, 'Roland')
# for record in result:
# print(record )