File size: 1,657 Bytes
017e382
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from sentence_transformers import SentenceTransformer
from tqdm import tqdm

model = SentenceTransformer('thenlper/gte-large')

embed = lambda text: model.encode(text).tolist()

def parse_data(file_path):
    papers = []
    with open(file_path, 'r') as file:
        paper = {}
        for line in tqdm(file, desc="Parsing data"):
            if line.startswith('#*'):
                paper['title'] = line[2:].strip()
            elif line.startswith('#@'):
                paper['authors'] = line[2:].strip().split(',')
            elif line.startswith('#t'):
                paper['year'] = int(line[2:].strip())
            elif line.startswith('#c'):
                paper['venue'] = line[2:].strip()
            elif line.startswith('#index'):
                paper['index'] = int(line[6:].strip())
            elif line.startswith('#%'):
                if 'references' not in paper:
                    paper['references'] = []
                paper['references'].append(int(line[2:].strip()))
            elif line.startswith('#!'):
                paper['abstract'] = line[2:].strip()
                paper['embedding'] = embed(line[2:].strip())
                if 'references' not in paper: paper['references'] = []
                papers.append(paper)
                paper = {}
            elif line.strip() == '':
                continue
    return papers

file_path = '/home/ppxscal/Projects/kellis/neuralmaps/papers.txt'
papers = parse_data(file_path)

with open('papers.json', 'w') as json_file:
    json.dump(papers, json_file)

with open('papers_formatted.json', 'w') as json_file:
    json.dump(papers, json_file, indent=1)