ppxscal commited on
Commit
017e382
·
1 Parent(s): 8aa8cb0

Upload graphing.py

Browse files

script I used to embed abstracts

Files changed (1) hide show
  1. graphing.py +45 -0
graphing.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from sentence_transformers import SentenceTransformer
3
+ from tqdm import tqdm
4
+
5
+ model = SentenceTransformer('thenlper/gte-large')
6
+
7
+ embed = lambda text: model.encode(text).tolist()
8
+
9
+ def parse_data(file_path):
10
+ papers = []
11
+ with open(file_path, 'r') as file:
12
+ paper = {}
13
+ for line in tqdm(file, desc="Parsing data"):
14
+ if line.startswith('#*'):
15
+ paper['title'] = line[2:].strip()
16
+ elif line.startswith('#@'):
17
+ paper['authors'] = line[2:].strip().split(',')
18
+ elif line.startswith('#t'):
19
+ paper['year'] = int(line[2:].strip())
20
+ elif line.startswith('#c'):
21
+ paper['venue'] = line[2:].strip()
22
+ elif line.startswith('#index'):
23
+ paper['index'] = int(line[6:].strip())
24
+ elif line.startswith('#%'):
25
+ if 'references' not in paper:
26
+ paper['references'] = []
27
+ paper['references'].append(int(line[2:].strip()))
28
+ elif line.startswith('#!'):
29
+ paper['abstract'] = line[2:].strip()
30
+ paper['embedding'] = embed(line[2:].strip())
31
+ if 'references' not in paper: paper['references'] = []
32
+ papers.append(paper)
33
+ paper = {}
34
+ elif line.strip() == '':
35
+ continue
36
+ return papers
37
+
38
+ file_path = '/home/ppxscal/Projects/kellis/neuralmaps/papers.txt'
39
+ papers = parse_data(file_path)
40
+
41
+ with open('papers.json', 'w') as json_file:
42
+ json.dump(papers, json_file)
43
+
44
+ with open('papers_formatted.json', 'w') as json_file:
45
+ json.dump(papers, json_file, indent=1)