-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.php
177 lines (149 loc) · 6.75 KB
/
parser.php
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
$fileName = "E:\ICD-10 WHO\icdClaML2015ens.zip\icdClaML2015ens.xml";
if (file_exists($fileName)) {
$xml = simplexml_load_file($fileName);
} else {
exit('Failed to open ' .$fileName);
}
foreach ($xml as $item) {
if ($item->getName() === 'Class') {
$level = null;
$attributes = $item->attributes();
if (isset($attributes["kind"])) {
if ($attributes["kind"] == "chapter") {
$level = 1;
} elseif ($attributes["kind"] == "block") {
$level = 2;
} elseif ($attributes["kind"] == "category") {
if (isset($attributes["code"]) and preg_match("/^[A-Z]\\d{2}$/", $attributes["code"])) {
$level = 3;
} elseif (isset($attributes["code"]) and preg_match("/^[A-Z]\\d{2}\\.\\d$/", $attributes["code"])) {
$level = 4;
}
}
}
if (isset($attributes["code"])) {
$code = $attributes["code"];
}
$title = null;
$description = null;
$note = null;
$parentCode = null;
$mortBCode = null;
$mortL4Code = null;
$mortL3Code = null;
$mortL2Code = null;
$mortL1Code = null;
$clusions = ["inclusions" => [], "exclusions" => []];
foreach ($item->children() as $child) {
$name = $child->getName();
$attrLev2 = $child->attributes();
if ($name == "Meta" and $attrLev2["name"] == "MortBCode" and $attrLev2["value"] != "UNDEF") {
$mortBCode = $attrLev2["value"];
} elseif ($name == "Meta" and $attrLev2["name"] == "MortL4Code" and $attrLev2["value"] != "UNDEF") {
$mortL4Code = $attrLev2["value"];
} elseif ($name == "Meta" and $attrLev2["name"] == "MortL3Code" and $attrLev2["value"] != "UNDEF") {
$mortL3Code = $attrLev2["value"];
} elseif ($name == "Meta" and $attrLev2["name"] == "MortL2Code" and $attrLev2["value"] != "UNDEF") {
$mortL2Code = $attrLev2["value"];
} elseif ($name == "Meta" and $attrLev2["name"] == "MortL1Code" and $attrLev2["value"] != "UNDEF") {
$mortL1Code = $attrLev2["value"];
} elseif ($name == "Rubric" and $attrLev2["kind"] == "preferred") {
$title = $child->Label[0];
} elseif ($name == "Rubric" and $attrLev2["kind"] == "preferredLong") {
$description = $child->Label[0];
} elseif (
$name == "Rubric" and
($attrLev2["kind"] == "text" or $attrLev2["kind"] == "note" or $attrLev2["kind"] == "footnote" or
$attrLev2["kind"] == "introduction" or $attrLev2["kind"] == "definition" or $attrLev2["kind"] == "modifierlink")
) {
$labelChildren = $child->Label[0]->children();
$labelChildrenCount = count($labelChildren);
if ($labelChildrenCount == 1 and trim($child->Label[0]) == "") {
$note .= "<div>".$labelChildren[0]."</div>\n";
} elseif ($labelChildrenCount > 0) {
$rawNote = $child->Label[0]->asXML();
$rawNote = preg_replace(
["|^<Label[^>]*>|", "|</Label>\s*$|", "|<Para\b|", "|</Para>|", "|<List\b|", "|</List>|", "|<ListItem\b|", "|</ListItem>|"],
["<div>", "</div>", "<p", "</p>", "<ul", "</ul>", "<li", "</li>"],
$rawNote
);
$note .= $rawNote."\n";
} else {
$note .= "<div>".$child->Label[0]."</div>\n";
}
} elseif ($name == "Rubric" and ($attrLev2["kind"] == "inclusion" or $attrLev2["kind"] == "exclusion")) {
$type = $attrLev2["kind"];
$label = $child->Label[0];
if (isset($label->Fragment[0])) {
if (isset($label->Fragment[1]->Reference[0])) {
$bracket = " [".$label->Fragment[1]->Reference[0]."]";
} else {
$bracket = "";
}
$clusions[$type."s"][] = $label->Fragment[0]." ".$label->Fragment[1].$bracket;
} else {
if (isset($label->Reference[0])) {
$bracket = " [".$label->Reference[0]."]";
} else {
$bracket = "";
}
$clusions[$type."s"][] = $label.$bracket;
}
} elseif ($name == "SuperClass") {
$parentCode = $attrLev2["code"];
}
}
if ($level == 1) {
$icd = new Icd10;
$icd->level = $level;
$icd->code = $code."";
$icd->name = $title."";
$icd->description = $description;
$icd->note = $note;
if (!$icd->save()) {
Yii::$app->response->statusCode = 400;
Yii::$app->response->statusText = "dataNotValid";
return $icd->errors;
}
} else {
$parent = Icd10::find()->where(["code" => $parentCode])->one();
if ($parent == null) {
continue;
} else {
$icd = new Icd10;
$icd->level = $level;
$icd->parent_id = $parent->id;
$icd->code = $code."";
$icd->name = $title."";
$icd->description = $description;
$icd->note = $note;
$icd->mortb_code = $mortBCode;
$icd->mortl4_code = $mortL4Code;
$icd->mortl3_code = $mortL3Code;
$icd->mortl2_code = $mortL2Code;
$icd->mortl1_code = $mortL1Code;
if (!$icd->save()) {
Yii::$app->response->statusCode = 400;
Yii::$app->response->statusText = "dataNotValid";
return $icd->errors;
} else {
foreach ($clusions["inclusions"] as $inclusion) {
$inc = new Icd10Clusion;
$inc->icd10_id = $icd->id;
$inc->is_inclusion = true;
$inc->name = $inclusion;
$inc->save();
}
foreach ($clusions["exclusions"] as $exclusion) {
$inc = new Icd10Clusion;
$inc->icd10_id = $icd->id;
$inc->is_inclusion = false;
$inc->name = $exclusion;
$inc->save();
}
}
}
}
}
}