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)