sia_tp_sample / Academic-Hammer__SciTSR.jsonl
shahp7575's picture
commit files to HF hub
3a7f06a
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/table.py","language":"python","identifier":"Box.__init__","parameters":"(self, pos)","argument_list":"","return_statement":"","docstring":"pos: (x1, x2, y1, y2)","docstring_summary":"pos: (x1, x2, y1, y2)","docstring_tokens":["pos",":","(","x1","x2","y1","y2",")"],"function":"def __init__(self, pos):\n \"\"\"pos: (x1, x2, y1, y2)\"\"\"\n self.set_pos(pos)","function_tokens":["def","__init__","(","self",",","pos",")",":","self",".","set_pos","(","pos",")"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/table.py#L30-L32"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/eval.py","language":"python","identifier":"eval_relations","parameters":"(gt:List[List], res:List[List], cmp_blank=True)","argument_list":"","return_statement":"return precision, recall","docstring":"Evaluate results\n\n Args:\n gt: a list of list of Relation\n res: a list of list of Relation","docstring_summary":"Evaluate results","docstring_tokens":["Evaluate","results"],"function":"def eval_relations(gt:List[List], res:List[List], cmp_blank=True):\n \"\"\"Evaluate results\n\n Args:\n gt: a list of list of Relation\n res: a list of list of Relation\n \"\"\"\n\n #TODO to know how to calculate the total recall and prec\n\n assert len(gt) == len(res)\n tot_prec = 0\n tot_recall = 0\n total = 0\n # print(\"evaluating result...\")\n\n # for _gt, _res in tqdm(zip(gt, res)):\n # for _gt, _res in tqdm(zip(gt, res), total=len(gt), desc='eval'):\n idx, t = 0, len(gt) \n for _gt, _res in zip(gt, res):\n idx += 1\n print('Eval %d\/%d (%d%%)' % (idx, t, idx \/ t * 100), ' ' * 45, end='\\r')\n corr = compare_rel(_gt, _res, cmp_blank)\n precision = corr \/ len(_res) if len(_res) != 0 else 0\n recall = corr \/ len(_gt) if len(_gt) != 0 else 0\n tot_prec += precision\n tot_recall += recall\n total += 1\n # print()\n \n precision = tot_prec \/ total\n recall = tot_recall \/ total\n # print(\"Test on %d instances. Precision: %.2f, Recall: %.2f\" % (\n # total, precision, recall))\n return precision, recall","function_tokens":["def","eval_relations","(","gt",":","List","[","List","]",",","res",":","List","[","List","]",",","cmp_blank","=","True",")",":","#TODO to know how to calculate the total recall and prec","assert","len","(","gt",")","==","len","(","res",")","tot_prec","=","0","tot_recall","=","0","total","=","0","# print(\"evaluating result...\")","# for _gt, _res in tqdm(zip(gt, res)):","# for _gt, _res in tqdm(zip(gt, res), total=len(gt), desc='eval'):","idx",",","t","=","0",",","len","(","gt",")","for","_gt",",","_res","in","zip","(","gt",",","res",")",":","idx","+=","1","print","(","'Eval %d\/%d (%d%%)'","%","(","idx",",","t",",","idx","\/","t","*","100",")",",","' '","*","45",",","end","=","'\\r'",")","corr","=","compare_rel","(","_gt",",","_res",",","cmp_blank",")","precision","=","corr","\/","len","(","_res",")","if","len","(","_res",")","!=","0","else","0","recall","=","corr","\/","len","(","_gt",")","if","len","(","_gt",")","!=","0","else","0","tot_prec","+=","precision","tot_recall","+=","recall","total","+=","1","# print()","precision","=","tot_prec","\/","total","recall","=","tot_recall","\/","total","# print(\"Test on %d instances. Precision: %.2f, Recall: %.2f\" % (","# total, precision, recall))","return","precision",",","recall"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/eval.py#L30-L64"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/eval.py","language":"python","identifier":"Table2Relations","parameters":"(t:Table)","argument_list":"","return_statement":"return ret","docstring":"Convert a Table object to a List of Relation.","docstring_summary":"Convert a Table object to a List of Relation.","docstring_tokens":["Convert","a","Table","object","to","a","List","of","Relation","."],"function":"def Table2Relations(t:Table):\n \"\"\"Convert a Table object to a List of Relation.\n \"\"\"\n ret = []\n cl = t.coo2cell_id\n # remove duplicates with pair set\n used = set()\n\n # look right\n for r in range(t.row_n):\n for cFrom in range(t.col_n - 1):\n cTo = cFrom + 1\n loop = True\n while loop and cTo < t.col_n:\n fid, tid = cl[r][cFrom], cl[r][cTo]\n if fid != -1 and tid != -1 and fid != tid:\n if (fid, tid) not in used:\n ret.append(Relation(\n from_text=t.cells[fid].text,\n to_text=t.cells[tid].text,\n direction=DIR_HORIZ,\n from_id=fid,\n to_id=tid,\n no_blanks=cTo - cFrom - 1\n ))\n used.add((fid, tid))\n loop = False\n else:\n if fid != -1 and tid != -1 and fid == tid:\n cFrom = cTo\n cTo += 1\n \n # look down\n for c in range(t.col_n):\n for rFrom in range(t.row_n - 1):\n rTo = rFrom + 1\n loop = True\n while loop and rTo < t.row_n:\n fid, tid = cl[rFrom][c], cl[rTo][c]\n if fid != -1 and tid != -1 and fid != tid:\n if (fid, tid) not in used: \n ret.append(Relation(\n from_text=t.cells[fid].text,\n to_text=t.cells[tid].text,\n direction=DIR_VERT,\n from_id=fid,\n to_id=tid,\n no_blanks=rTo - rFrom - 1\n ))\n used.add((fid, tid))\n loop = False\n else:\n if fid != -1 and tid != -1 and fid == tid:\n rFrom = rTo\n rTo += 1\n\n return ret","function_tokens":["def","Table2Relations","(","t",":","Table",")",":","ret","=","[","]","cl","=","t",".","coo2cell_id","# remove duplicates with pair set","used","=","set","(",")","# look right","for","r","in","range","(","t",".","row_n",")",":","for","cFrom","in","range","(","t",".","col_n","-","1",")",":","cTo","=","cFrom","+","1","loop","=","True","while","loop","and","cTo","<","t",".","col_n",":","fid",",","tid","=","cl","[","r","]","[","cFrom","]",",","cl","[","r","]","[","cTo","]","if","fid","!=","-","1","and","tid","!=","-","1","and","fid","!=","tid",":","if","(","fid",",","tid",")","not","in","used",":","ret",".","append","(","Relation","(","from_text","=","t",".","cells","[","fid","]",".","text",",","to_text","=","t",".","cells","[","tid","]",".","text",",","direction","=","DIR_HORIZ",",","from_id","=","fid",",","to_id","=","tid",",","no_blanks","=","cTo","-","cFrom","-","1",")",")","used",".","add","(","(","fid",",","tid",")",")","loop","=","False","else",":","if","fid","!=","-","1","and","tid","!=","-","1","and","fid","==","tid",":","cFrom","=","cTo","cTo","+=","1","# look down","for","c","in","range","(","t",".","col_n",")",":","for","rFrom","in","range","(","t",".","row_n","-","1",")",":","rTo","=","rFrom","+","1","loop","=","True","while","loop","and","rTo","<","t",".","row_n",":","fid",",","tid","=","cl","[","rFrom","]","[","c","]",",","cl","[","rTo","]","[","c","]","if","fid","!=","-","1","and","tid","!=","-","1","and","fid","!=","tid",":","if","(","fid",",","tid",")","not","in","used",":","ret",".","append","(","Relation","(","from_text","=","t",".","cells","[","fid","]",".","text",",","to_text","=","t",".","cells","[","tid","]",".","text",",","direction","=","DIR_VERT",",","from_id","=","fid",",","to_id","=","tid",",","no_blanks","=","rTo","-","rFrom","-","1",")",")","used",".","add","(","(","fid",",","tid",")",")","loop","=","False","else",":","if","fid","!=","-","1","and","tid","!=","-","1","and","fid","==","tid",":","rFrom","=","rTo","rTo","+=","1","return","ret"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/eval.py#L89-L145"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/eval.py","language":"python","identifier":"json2Table","parameters":"(json_obj, tid=\"\", splitted_content=False)","argument_list":"","return_statement":"return Table(row_n + 1, col_n + 1, cells, tid)","docstring":"Construct a Table object from json object\n\n Args:\n json_obj: a json object\n Returns:\n a Table object","docstring_summary":"Construct a Table object from json object","docstring_tokens":["Construct","a","Table","object","from","json","object"],"function":"def json2Table(json_obj, tid=\"\", splitted_content=False):\n \"\"\"Construct a Table object from json object\n\n Args:\n json_obj: a json object\n Returns:\n a Table object\n \"\"\"\n jo = json_obj[\"cells\"]\n row_n, col_n = 0, 0\n cells = []\n for co in jo:\n content = co[\"content\"]\n if content is None: continue\n if splitted_content:\n content = \" \".join(content)\n else:\n content = content.strip()\n if content == \"\": continue\n start_row = co[\"start_row\"]\n end_row = co[\"end_row\"]\n start_col = co[\"start_col\"]\n end_col = co[\"end_col\"]\n row_n = max(row_n, end_row)\n col_n = max(col_n, end_col)\n cell = Chunk(content, (start_row, end_row, start_col, end_col))\n cells.append(cell)\n return Table(row_n + 1, col_n + 1, cells, tid)","function_tokens":["def","json2Table","(","json_obj",",","tid","=","\"\"",",","splitted_content","=","False",")",":","jo","=","json_obj","[","\"cells\"","]","row_n",",","col_n","=","0",",","0","cells","=","[","]","for","co","in","jo",":","content","=","co","[","\"content\"","]","if","content","is","None",":","continue","if","splitted_content",":","content","=","\" \"",".","join","(","content",")","else",":","content","=","content",".","strip","(",")","if","content","==","\"\"",":","continue","start_row","=","co","[","\"start_row\"","]","end_row","=","co","[","\"end_row\"","]","start_col","=","co","[","\"start_col\"","]","end_col","=","co","[","\"end_col\"","]","row_n","=","max","(","row_n",",","end_row",")","col_n","=","max","(","col_n",",","end_col",")","cell","=","Chunk","(","content",",","(","start_row",",","end_row",",","start_col",",","end_col",")",")","cells",".","append","(","cell",")","return","Table","(","row_n","+","1",",","col_n","+","1",",","cells",",","tid",")"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/eval.py#L147-L174"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/model.py","language":"python","identifier":"Attention.forward","parameters":"(self, x, y, mask)","argument_list":"","return_statement":"return x","docstring":"Shapes:\n mask: [nodes\/edges, edges\/nodes]\n q: [nodes\/edges, h]\n k: [edges\/nodes, h]\n v: [edges\/nodes, h]\n score: [nodes\/edges, edges\/nodes]\n x_atten: [nodes\/edges, h]","docstring_summary":"Shapes:\n mask: [nodes\/edges, edges\/nodes]\n q: [nodes\/edges, h]\n k: [edges\/nodes, h]\n v: [edges\/nodes, h]\n score: [nodes\/edges, edges\/nodes]\n x_atten: [nodes\/edges, h]","docstring_tokens":["Shapes",":","mask",":","[","nodes","\/","edges","edges","\/","nodes","]","q",":","[","nodes","\/","edges","h","]","k",":","[","edges","\/","nodes","h","]","v",":","[","edges","\/","nodes","h","]","score",":","[","nodes","\/","edges","edges","\/","nodes","]","x_atten",":","[","nodes","\/","edges","h","]"],"function":"def forward(self, x, y, mask):\n \"\"\"\n Shapes:\n mask: [nodes\/edges, edges\/nodes]\n q: [nodes\/edges, h]\n k: [edges\/nodes, h]\n v: [edges\/nodes, h]\n score: [nodes\/edges, edges\/nodes]\n x_atten: [nodes\/edges, h]\n \"\"\"\n q = self.linear_q(x)\n k = self.linear_k(y)\n v = self.linear_v(y)\n score = torch.mm(q, k.t()) \/ math.sqrt(self.size)\n score = self.masked_softmax(score, mask, dim=1)\n x_atten = torch.mm(score, v)\n # dropout\n x_atten = self.dropout(x_atten)\n x = self.layer_norm_1(x + x_atten)\n x_linear = self.feed_forward(x)\n # dropout\n x_linear = self.dropout(x_linear)\n x = self.layer_norm_2(x + x_linear)\n return x","function_tokens":["def","forward","(","self",",","x",",","y",",","mask",")",":","q","=","self",".","linear_q","(","x",")","k","=","self",".","linear_k","(","y",")","v","=","self",".","linear_v","(","y",")","score","=","torch",".","mm","(","q",",","k",".","t","(",")",")","\/","math",".","sqrt","(","self",".","size",")","score","=","self",".","masked_softmax","(","score",",","mask",",","dim","=","1",")","x_atten","=","torch",".","mm","(","score",",","v",")","# dropout","x_atten","=","self",".","dropout","(","x_atten",")","x","=","self",".","layer_norm_1","(","x","+","x_atten",")","x_linear","=","self",".","feed_forward","(","x",")","# dropout","x_linear","=","self",".","dropout","(","x_linear",")","x","=","self",".","layer_norm_2","(","x","+","x_linear",")","return","x"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/model.py#L38-L61"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/graph.py","language":"python","identifier":"Vertex.__init__","parameters":"(self, vid: int, chunk: Chunk, tab_h, tab_w)","argument_list":"","return_statement":"","docstring":"Args:\n vid: Vertex id\n chunk: the chunk to extract features\n tab_h: height of the table (y-axis)\n tab_w: width of the table (x-axis)","docstring_summary":"Args:\n vid: Vertex id\n chunk: the chunk to extract features\n tab_h: height of the table (y-axis)\n tab_w: width of the table (x-axis)","docstring_tokens":["Args",":","vid",":","Vertex","id","chunk",":","the","chunk","to","extract","features","tab_h",":","height","of","the","table","(","y","-","axis",")","tab_w",":","width","of","the","table","(","x","-","axis",")"],"function":"def __init__(self, vid: int, chunk: Chunk, tab_h, tab_w):\n \"\"\"\n Args:\n vid: Vertex id\n chunk: the chunk to extract features\n tab_h: height of the table (y-axis)\n tab_w: width of the table (x-axis)\n \"\"\"\n self.vid = vid\n self.tab_h = tab_h\n self.tab_w = tab_w\n self.chunk = chunk\n self.features = self.get_features()","function_tokens":["def","__init__","(","self",",","vid",":","int",",","chunk",":","Chunk",",","tab_h",",","tab_w",")",":","self",".","vid","=","vid","self",".","tab_h","=","tab_h","self",".","tab_w","=","tab_w","self",".","chunk","=","chunk","self",".","features","=","self",".","get_features","(",")"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/graph.py#L15-L27"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/train.py","language":"python","identifier":"patch_chunks","parameters":"(dataset_folder)","argument_list":"","return_statement":"return 1","docstring":"To patch the all chunk files of the train & test dataset that have the problem of duplicate last character\n\tof the last cell in all chunk files\n\t:param dataset_folder: train dataset path\n\t:return: 1","docstring_summary":"To patch the all chunk files of the train & test dataset that have the problem of duplicate last character\n\tof the last cell in all chunk files\n\t:param dataset_folder: train dataset path\n\t:return: 1","docstring_tokens":["To","patch","the","all","chunk","files","of","the","train","&","test","dataset","that","have","the","problem","of","duplicate","last","character","of","the","last","cell","in","all","chunk","files",":","param","dataset_folder",":","train","dataset","path",":","return",":","1"],"function":"def patch_chunks(dataset_folder):\n\t\"\"\"\n\tTo patch the all chunk files of the train & test dataset that have the problem of duplicate last character\n\tof the last cell in all chunk files\n\t:param dataset_folder: train dataset path\n\t:return: 1\n\t\"\"\"\n\timport os\n\timport shutil\n\tfrom pathlib import Path\n\n\tshutil.move(os.path.join(dataset_folder, \"chunk\"), os.path.join(dataset_folder, \"chunk-old\"))\n\tdir_ = Path(os.path.join(dataset_folder, \"chunk-old\"))\n\tos.makedirs(os.path.join(dataset_folder, \"chunk\"), exist_ok=True)\n\n\tfor chunk_path in dir_.iterdir():\n\t\t# print(chunk_path)\n\t\twith open(str(chunk_path), encoding=\"utf-8\") as f:\n\t\t\tchunks = json.load(f)['chunks']\n\t\tchunks[-1]['text'] = chunks[-1]['text'][:-1]\n\n\t\twith open(str(chunk_path).replace(\"chunk-old\", \"chunk\"), \"w\", encoding=\"utf-8\") as ofile:\n\t\t\tjson.dump({\"chunks\": chunks}, ofile)\n\tprint(\"Input files patched, ready for the use\")\n\treturn 1","function_tokens":["def","patch_chunks","(","dataset_folder",")",":","import","os","import","shutil","from","pathlib","import","Path","shutil",".","move","(","os",".","path",".","join","(","dataset_folder",",","\"chunk\"",")",",","os",".","path",".","join","(","dataset_folder",",","\"chunk-old\"",")",")","dir_","=","Path","(","os",".","path",".","join","(","dataset_folder",",","\"chunk-old\"",")",")","os",".","makedirs","(","os",".","path",".","join","(","dataset_folder",",","\"chunk\"",")",",","exist_ok","=","True",")","for","chunk_path","in","dir_",".","iterdir","(",")",":","# print(chunk_path)","with","open","(","str","(","chunk_path",")",",","encoding","=","\"utf-8\"",")","as","f",":","chunks","=","json",".","load","(","f",")","[","'chunks'","]","chunks","[","-","1","]","[","'text'","]","=","chunks","[","-","1","]","[","'text'","]","[",":","-","1","]","with","open","(","str","(","chunk_path",")",".","replace","(","\"chunk-old\"",",","\"chunk\"",")",",","\"w\"",",","encoding","=","\"utf-8\"",")","as","ofile",":","json",".","dump","(","{","\"chunks\"",":","chunks","}",",","ofile",")","print","(","\"Input files patched, ready for the use\"",")","return","1"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/train.py#L146-L170"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/train.py","language":"python","identifier":"Trainer.test_epoch","parameters":"(self, epoch, dataset, should_print=False, use_mask=True)","argument_list":"","return_statement":"return acc","docstring":"use_mask: mask the 0 label","docstring_summary":"use_mask: mask the 0 label","docstring_tokens":["use_mask",":","mask","the","0","label"],"function":"def test_epoch(self, epoch, dataset, should_print=False, use_mask=True):\n \"\"\"\n use_mask: mask the 0 label\n \"\"\"\n self.model.eval()\n acc_list = []\n for index, data in tqdm(enumerate(dataset)):\n self._to_device(data)\n percent = index \/ len(dataset) * 100\n if should_print:\n print('[Epoch %d] Test | Data %d (%d%%): acc: | path: %s' % \\\n (epoch, index, percent, data.path), ' ' * 30, end='\\r')\n outputs = self.model(data.nodes, data.edges, data.adj, data.incidence)\n _lab_len = len(data.labels)\n if use_mask:\n for i in data.labels: \n if i == 0: _lab_len -= 1\n _labels = torch.LongTensor(\n [(-1 if i == 0 else i) for i in data.labels]).to(self.device)\n else: _labels = data.labels\n acc = (outputs.max(dim=1)[1] == _labels).float().sum().item() \/ _lab_len\n acc_list.append(acc)\n # if index % 10 == 0:\n if should_print:\n print('[Epoch %d] Test | Data %d (%d%%): acc: %.3f | path: %s' % \\\n (epoch, index, percent, acc, data.path), ' ' * 30, end='\\n')\n acc = sum(acc_list) \/ len(acc_list)\n return acc","function_tokens":["def","test_epoch","(","self",",","epoch",",","dataset",",","should_print","=","False",",","use_mask","=","True",")",":","self",".","model",".","eval","(",")","acc_list","=","[","]","for","index",",","data","in","tqdm","(","enumerate","(","dataset",")",")",":","self",".","_to_device","(","data",")","percent","=","index","\/","len","(","dataset",")","*","100","if","should_print",":","print","(","'[Epoch %d] Test | Data %d (%d%%): acc: | path: %s'","%","(","epoch",",","index",",","percent",",","data",".","path",")",",","' '","*","30",",","end","=","'\\r'",")","outputs","=","self",".","model","(","data",".","nodes",",","data",".","edges",",","data",".","adj",",","data",".","incidence",")","_lab_len","=","len","(","data",".","labels",")","if","use_mask",":","for","i","in","data",".","labels",":","if","i","==","0",":","_lab_len","-=","1","_labels","=","torch",".","LongTensor","(","[","(","-","1","if","i","==","0","else","i",")","for","i","in","data",".","labels","]",")",".","to","(","self",".","device",")","else",":","_labels","=","data",".","labels","acc","=","(","outputs",".","max","(","dim","=","1",")","[","1","]","==","_labels",")",".","float","(",")",".","sum","(",")",".","item","(",")","\/","_lab_len","acc_list",".","append","(","acc",")","# if index % 10 == 0:","if","should_print",":","print","(","'[Epoch %d] Test | Data %d (%d%%): acc: %.3f | path: %s'","%","(","epoch",",","index",",","percent",",","acc",",","data",".","path",")",",","' '","*","30",",","end","=","'\\n'",")","acc","=","sum","(","acc_list",")","\/","len","(","acc_list",")","return","acc"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/train.py#L108-L135"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/data\/utils.py","language":"python","identifier":"json2Table","parameters":"(json_obj, tid=\"\", splitted_content=False)","argument_list":"","return_statement":"return Table(row_n + 1, col_n + 1, cells, tid)","docstring":"Construct a Table object from json object\n Args:\n json_obj: a json object\n Returns:\n a Table object","docstring_summary":"Construct a Table object from json object\n Args:\n json_obj: a json object\n Returns:\n a Table object","docstring_tokens":["Construct","a","Table","object","from","json","object","Args",":","json_obj",":","a","json","object","Returns",":","a","Table","object"],"function":"def json2Table(json_obj, tid=\"\", splitted_content=False):\n \"\"\"Construct a Table object from json object\n Args:\n json_obj: a json object\n Returns:\n a Table object\n \"\"\"\n jo = json_obj[\"cells\"]\n row_n, col_n = 0, 0\n cells = []\n for co in jo:\n content = co[\"content\"]\n if content is None: continue\n if splitted_content:\n content = \" \".join(content)\n else:\n content = content.strip()\n if content == \"\": continue\n start_row = co[\"start_row\"]\n end_row = co[\"end_row\"]\n start_col = co[\"start_col\"]\n end_col = co[\"end_col\"]\n row_n = max(row_n, end_row)\n col_n = max(col_n, end_col)\n cell = Chunk(content, (start_row, end_row, start_col, end_col))\n cells.append(cell)\n return Table(row_n + 1, col_n + 1, cells, tid)","function_tokens":["def","json2Table","(","json_obj",",","tid","=","\"\"",",","splitted_content","=","False",")",":","jo","=","json_obj","[","\"cells\"","]","row_n",",","col_n","=","0",",","0","cells","=","[","]","for","co","in","jo",":","content","=","co","[","\"content\"","]","if","content","is","None",":","continue","if","splitted_content",":","content","=","\" \"",".","join","(","content",")","else",":","content","=","content",".","strip","(",")","if","content","==","\"\"",":","continue","start_row","=","co","[","\"start_row\"","]","end_row","=","co","[","\"end_row\"","]","start_col","=","co","[","\"start_col\"","]","end_col","=","co","[","\"end_col\"","]","row_n","=","max","(","row_n",",","end_row",")","col_n","=","max","(","col_n",",","end_col",")","cell","=","Chunk","(","content",",","(","start_row",",","end_row",",","start_col",",","end_col",")",")","cells",".","append","(","cell",")","return","Table","(","row_n","+","1",",","col_n","+","1",",","cells",",","tid",")"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/data\/utils.py#L38-L64"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/data\/utils.py","language":"python","identifier":"add_knn_edges","parameters":"(chunks, relations, k=20, debug=False)","argument_list":"","return_statement":"return relations, recall","docstring":"Add edges according to knn of vertexes.","docstring_summary":"Add edges according to knn of vertexes.","docstring_tokens":["Add","edges","according","to","knn","of","vertexes","."],"function":"def add_knn_edges(chunks, relations, k=20, debug=False):\n \"\"\"Add edges according to knn of vertexes.\n \"\"\"\n edges = set()\n rel_recall = {}\n for i, j, _ in relations:\n edges.add((i, j) if i < j else (j, i))\n rel_recall[(i, j) if i < j else (j, i)] = False\n for i in range(len(chunks)):\n _dis_ij = []\n for j in range(len(chunks)):\n if j == i: continue\n _dis_ij.append((_eul_dis(chunks, i, j), j))\n sorted_dis_ij = sorted(_dis_ij)\n for _, j in sorted_dis_ij[:k]:\n _i, _j = (i, j) if i < j else (j, i)\n if (_i, _j) in rel_recall: rel_recall[(_i, _j)] = True\n if (_i, _j) not in edges:\n edges.add((_i, _j))\n relations.append((_i, _j, 0))\n cnt = 0\n for _, val in rel_recall.items():\n if val: cnt += 1\n recall = 0 if len(rel_recall) == 0 else cnt \/ len(rel_recall)\n if debug:\n print(\"add knn edge. recall:%.3f\" % recall)\n return relations, recall","function_tokens":["def","add_knn_edges","(","chunks",",","relations",",","k","=","20",",","debug","=","False",")",":","edges","=","set","(",")","rel_recall","=","{","}","for","i",",","j",",","_","in","relations",":","edges",".","add","(","(","i",",","j",")","if","i","<","j","else","(","j",",","i",")",")","rel_recall","[","(","i",",","j",")","if","i","<","j","else","(","j",",","i",")","]","=","False","for","i","in","range","(","len","(","chunks",")",")",":","_dis_ij","=","[","]","for","j","in","range","(","len","(","chunks",")",")",":","if","j","==","i",":","continue","_dis_ij",".","append","(","(","_eul_dis","(","chunks",",","i",",","j",")",",","j",")",")","sorted_dis_ij","=","sorted","(","_dis_ij",")","for","_",",","j","in","sorted_dis_ij","[",":","k","]",":","_i",",","_j","=","(","i",",","j",")","if","i","<","j","else","(","j",",","i",")","if","(","_i",",","_j",")","in","rel_recall",":","rel_recall","[","(","_i",",","_j",")","]","=","True","if","(","_i",",","_j",")","not","in","edges",":","edges",".","add","(","(","_i",",","_j",")",")","relations",".","append","(","(","_i",",","_j",",","0",")",")","cnt","=","0","for","_",",","val","in","rel_recall",".","items","(",")",":","if","val",":","cnt","+=","1","recall","=","0","if","len","(","rel_recall",")","==","0","else","cnt","\/","len","(","rel_recall",")","if","debug",":","print","(","\"add knn edge. recall:%.3f\"","%","recall",")","return","relations",",","recall"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/data\/utils.py#L120-L146"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/data\/loader.py","language":"python","identifier":"TableDataset.clean_chunk_rel","parameters":"(self, chunks, relations)","argument_list":"","return_statement":"return new_chunks, new_rels","docstring":"Remove null chunks","docstring_summary":"Remove null chunks","docstring_tokens":["Remove","null","chunks"],"function":"def clean_chunk_rel(self, chunks, relations):\n \"\"\"Remove null chunks\"\"\"\n new_chunks = []\n oldid2newid = [-1 for i in range(len(chunks))]\n for i, c in enumerate(chunks):\n if c.x2 == c.x1 or c.y2 == c.y1 or c.text == \"\":\n continue\n oldid2newid[i] = len(new_chunks)\n new_chunks.append(c)\n new_rels = []\n for i, j, t in relations:\n ni = oldid2newid[i]\n nj = oldid2newid[j]\n if ni != -1 and nj != -1: new_rels.append((ni, nj, t))\n return new_chunks, new_rels","function_tokens":["def","clean_chunk_rel","(","self",",","chunks",",","relations",")",":","new_chunks","=","[","]","oldid2newid","=","[","-","1","for","i","in","range","(","len","(","chunks",")",")","]","for","i",",","c","in","enumerate","(","chunks",")",":","if","c",".","x2","==","c",".","x1","or","c",".","y2","==","c",".","y1","or","c",".","text","==","\"\"",":","continue","oldid2newid","[","i","]","=","len","(","new_chunks",")","new_chunks",".","append","(","c",")","new_rels","=","[","]","for","i",",","j",",","t","in","relations",":","ni","=","oldid2newid","[","i","]","nj","=","oldid2newid","[","j","]","if","ni","!=","-","1","and","nj","!=","-","1",":","new_rels",".","append","(","(","ni",",","nj",",","t",")",")","return","new_chunks",",","new_rels"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/data\/loader.py#L140-L154"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/data\/rel_gen.py","language":"python","identifier":"dump_iters_as_tsv","parameters":"(filename, iterables, spliter=\"\\t\")","argument_list":"","return_statement":"","docstring":"Dump iters as tsv.\n item1\\titem2\\t... (from iterable1)\n item1\\titem2\\t... (from iterable2)","docstring_summary":"Dump iters as tsv.\n item1\\titem2\\t... (from iterable1)\n item1\\titem2\\t... (from iterable2)","docstring_tokens":["Dump","iters","as","tsv",".","item1","\\","titem2","\\","t","...","(","from","iterable1",")","item1","\\","titem2","\\","t","...","(","from","iterable2",")"],"function":"def dump_iters_as_tsv(filename, iterables, spliter=\"\\t\"):\n \"\"\"\n Dump iters as tsv.\n item1\\titem2\\t... (from iterable1)\n item1\\titem2\\t... (from iterable2)\n \"\"\"\n with open(filename, \"w\") as f:\n for iterable in iterables:\n iterable = [str(i) for i in iterable]\n f.write(spliter.join(iterable) + \"\\n\")","function_tokens":["def","dump_iters_as_tsv","(","filename",",","iterables",",","spliter","=","\"\\t\"",")",":","with","open","(","filename",",","\"w\"",")","as","f",":","for","iterable","in","iterables",":","iterable","=","[","str","(","i",")","for","i","in","iterable","]","f",".","write","(","spliter",".","join","(","iterable",")","+","\"\\n\"",")"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/data\/rel_gen.py#L18-L27"}
{"nwo":"Academic-Hammer\/SciTSR","sha":"79954b5143295162ceaf7e9d9af918a29fe12f55","path":"scitsr\/data\/rel_gen.py","language":"python","identifier":"match","parameters":"(src:dict, trg:dict, src_chunks, trg_chunks, fid)","argument_list":"","return_statement":"return sid2tid","docstring":"Match chunks to latex cells w.r.t. the contents.","docstring_summary":"Match chunks to latex cells w.r.t. the contents.","docstring_tokens":["Match","chunks","to","latex","cells","w",".","r",".","t",".","the","contents","."],"function":"def match(src:dict, trg:dict, src_chunks, trg_chunks, fid):\n \"\"\"Match chunks to latex cells w.r.t. the contents.\"\"\"\n sid2tid = {}\n print(\"--------%s---------------------------\" % fid)\n for stxt, sids in src.items():\n if stxt in trg:\n tids = trg[stxt]\n if len(sids) == 1 and len(tids) == 1: sid2tid[sids[0]] = tids[0]\n elif len(sids) == len(tids):\n schunks = [(sid, src_chunks[sid]) for sid in sids]\n tchunks = [(tid, trg_chunks[tid]) for tid in tids]\n sorted_sc = sorted(schunks, key=lambda x: (-x[1].y1, x[1].x1))\n sorted_tc = sorted(tchunks, key=lambda x: (x[1].x1, x[1].y1))\n for (sid, _), (tid, _) in zip(sorted_sc, sorted_tc):\n sid2tid[sid] = tid\n else: \n print(\"[W] length of sids and tids doesn't match\")\n else: \n print(\"[W] no match for text %s\" % stxt)\n print(\"-----------------------------------------------------------\")\n return sid2tid","function_tokens":["def","match","(","src",":","dict",",","trg",":","dict",",","src_chunks",",","trg_chunks",",","fid",")",":","sid2tid","=","{","}","print","(","\"--------%s---------------------------\"","%","fid",")","for","stxt",",","sids","in","src",".","items","(",")",":","if","stxt","in","trg",":","tids","=","trg","[","stxt","]","if","len","(","sids",")","==","1","and","len","(","tids",")","==","1",":","sid2tid","[","sids","[","0","]","]","=","tids","[","0","]","elif","len","(","sids",")","==","len","(","tids",")",":","schunks","=","[","(","sid",",","src_chunks","[","sid","]",")","for","sid","in","sids","]","tchunks","=","[","(","tid",",","trg_chunks","[","tid","]",")","for","tid","in","tids","]","sorted_sc","=","sorted","(","schunks",",","key","=","lambda","x",":","(","-","x","[","1","]",".","y1",",","x","[","1","]",".","x1",")",")","sorted_tc","=","sorted","(","tchunks",",","key","=","lambda","x",":","(","x","[","1","]",".","x1",",","x","[","1","]",".","y1",")",")","for","(","sid",",","_",")",",","(","tid",",","_",")","in","zip","(","sorted_sc",",","sorted_tc",")",":","sid2tid","[","sid","]","=","tid","else",":","print","(","\"[W] length of sids and tids doesn't match\"",")","else",":","print","(","\"[W] no match for text %s\"","%","stxt",")","print","(","\"-----------------------------------------------------------\"",")","return","sid2tid"],"url":"https:\/\/github.com\/Academic-Hammer\/SciTSR\/blob\/79954b5143295162ceaf7e9d9af918a29fe12f55\/scitsr\/data\/rel_gen.py#L30-L50"}