135 lines
4.9 KiB
Plaintext
135 lines
4.9 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 86,
|
|
"source": [
|
|
"file = open(\"testB.neu\")\r\n",
|
|
"dirty_blob = file.read()\r\n",
|
|
"\r\n",
|
|
"# get rid of comments and leading/trailing whitespace\r\n",
|
|
"lines = dirty_blob.split(\"\\n\")\r\n",
|
|
"for i, line in enumerate(lines):\r\n",
|
|
" if line.strip()[:2] == \"//\":\r\n",
|
|
" del lines[i]\r\n",
|
|
"\r\n",
|
|
"for i, line in enumerate(lines):\r\n",
|
|
" lines[i] = line.strip()\r\n",
|
|
"\r\n",
|
|
"tidy_blob = \" \".join(lines)"
|
|
],
|
|
"outputs": [],
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 87,
|
|
"source": [
|
|
"data = []\r\n",
|
|
"\r\n",
|
|
"# get blocks\r\n",
|
|
"for line in lines:\r\n",
|
|
" object = {}\r\n",
|
|
" if \"=\" in line:\r\n",
|
|
" keytype = line.split(\" \")[0].split(\":\")\r\n",
|
|
" key = keytype[0]\r\n",
|
|
" object[\"key\"] = key\r\n",
|
|
" if len(keytype) > 1:\r\n",
|
|
" type = keytype[1]\r\n",
|
|
" object[\"type\"] = type\r\n",
|
|
" data.append(object)"
|
|
],
|
|
"outputs": [],
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 88,
|
|
"source": [
|
|
"words = tidy_blob.split(\" \")"
|
|
],
|
|
"outputs": [],
|
|
"metadata": {}
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 89,
|
|
"source": [
|
|
"def extract_objects(keywords):\r\n",
|
|
" blocks = []\r\n",
|
|
" depth = -1\r\n",
|
|
" i = -1\r\n",
|
|
" last = \"\"\r\n",
|
|
" for word in keywords:\r\n",
|
|
" #print(f\"Word: {word} | Depth: {depth} | i: {i}\")\r\n",
|
|
" if word == \"{\":\r\n",
|
|
" depth += 1\r\n",
|
|
" if depth == 1:\r\n",
|
|
" #print(\"New block of depth 1\")\r\n",
|
|
" blocks.append({\"key\": last, \"object\": []})\r\n",
|
|
" i += 1\r\n",
|
|
" elif word == \"}\":\r\n",
|
|
" depth -= 1\r\n",
|
|
" else:\r\n",
|
|
" last = word\r\n",
|
|
" if depth >= 1:\r\n",
|
|
" blocks[i][\"object\"].append(word)\r\n",
|
|
" return blocks\r\n",
|
|
"\r\n",
|
|
"data = extract_objects(words)\r\n",
|
|
"temp = []\r\n",
|
|
"for blob in data:\r\n",
|
|
" temp.append(extract_objects(blob[\"object\"]))\r\n",
|
|
"\r\n",
|
|
" \r\n",
|
|
"\r\n",
|
|
"temp"
|
|
],
|
|
"outputs": [
|
|
{
|
|
"output_type": "execute_result",
|
|
"data": {
|
|
"text/plain": [
|
|
"[[{'key': 'aa', 'object': ['{', 'aaa']},\n",
|
|
" {'key': 'ab', 'object': ['{', 'aba']},\n",
|
|
" {'key': 'ac', 'object': ['{', 'abb']}],\n",
|
|
" [{'key': 'ba', 'object': ['{']},\n",
|
|
" {'key': 'bb', 'object': ['{']},\n",
|
|
" {'key': 'bc', 'object': ['{']}],\n",
|
|
" [{'key': 'ba', 'object': ['{']},\n",
|
|
" {'key': 'bb', 'object': ['{']},\n",
|
|
" {'key': 'bc', 'object': ['{']}]]"
|
|
]
|
|
},
|
|
"metadata": {},
|
|
"execution_count": 89
|
|
}
|
|
],
|
|
"metadata": {}
|
|
}
|
|
],
|
|
"metadata": {
|
|
"orig_nbformat": 4,
|
|
"language_info": {
|
|
"name": "python",
|
|
"version": "3.7.8",
|
|
"mimetype": "text/x-python",
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"pygments_lexer": "ipython3",
|
|
"nbconvert_exporter": "python",
|
|
"file_extension": ".py"
|
|
},
|
|
"kernelspec": {
|
|
"name": "python3",
|
|
"display_name": "Python 3.7.8 64-bit"
|
|
},
|
|
"interpreter": {
|
|
"hash": "57baa5815c940fdaff4d14510622de9616cae602444507ba5d0b6727c008cbd6"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
} |