-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocess.py
More file actions
145 lines (115 loc) · 6.1 KB
/
Copy pathpreprocess.py
File metadata and controls
145 lines (115 loc) · 6.1 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import json
import urllib.request
from typing import List
from collections import defaultdict
from bs4 import BeautifulSoup
import numpy as np
import torch
from transformers import (
AutoConfig,
AutoTokenizer,
AutoModel,
)
from sentence_transformers import SentenceTransformer
from tqdm import tqdm
CS_CATEGORIES = {'cs.CG', 'cs.GR', 'cs.DB', 'cs.FL', 'cs.CC', 'cs.SC', 'cs.MM', 'cs.PF',
'cs.SE', 'cs.MS', 'cs.IT', 'cs.MA', 'cs.OH', 'cs.NA', 'cs.PL', 'cs.HC',
'cs.RO', 'cs.DS', 'cs.CL', 'cs.AR', 'cs.ET', 'cs.NE', 'cs.CV', 'cs.CY',
'cs.CE', 'cs.NI', 'cs.DM', 'cs.OS', 'cs.IR', 'cs.DC', 'cs.SD', 'cs.CR',
'cs.LG', 'cs.GT', 'cs.LO', 'cs.SI', 'cs.DL', 'cs.GL', 'cs.SY', 'cs.AI'}
def load_and_cache_examples(data_path):
cached_examples_file = os.path.join("data", "cached_examples.pt")
abstracts_file = os.path.join("data", "abstracts.pt")
if os.path.exists(cached_examples_file):
print("Loading examples")
examples = torch.load(cached_examples_file)
abstracts = torch.load(abstracts_file)
else:
print("Creating examples")
max_seq_len = 512
config = AutoConfig.from_pretrained('albert-base-v2', cache_dir='cache/')
tokenizer = AutoTokenizer.from_pretrained('albert-base-v2', cache_dir='cache/', use_fast=True)
model = AutoModel.from_pretrained('albert-base-v2', cache_dir='cache/', config=config)
sbert_model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
sbert_model.max_seq_length = max_seq_len
examples, abstracts = [], []
with open(data_path, "r", encoding="utf-8") as f:
for line in tqdm(f, desc="Reading data"):
json_obj = json.loads(line)
categories = json_obj['categories'].split()
cs_categories = []
for c in categories:
if c[:2] == 'cs': # only grab CS papers
cs_categories.append(c)
if len(cs_categories) > 0:
# Preprocess abstract text
abstract = json_obj['abstract'].replace('\n', ' ').strip()
# TODO: MORE PREPROCESSING: lowercase? remove punctuation? remove stop words?
# SBERT Embedding
sbert_embedding = sbert_model.encode(abstract)
# BERT Embedding
tokens = tokenizer.tokenize(abstract)[:max_seq_len-2]
tokens = ['[CLS]'] + tokens + ['[SEP]']
input_ids = tokenizer.convert_tokens_to_ids(tokens)
input_ids = torch.tensor(input_ids).unsqueeze(0)
outputs = model(input_ids=input_ids)
albert_embedding = outputs.pooler_output.detach().cpu().numpy()
examples.append({
"id": json_obj['id'],
"authors": json_obj['authors'],
"title": json_obj['title'],
"date": json_obj['update_date'],
"categories": cs_categories,
"albert_embedding": albert_embedding,
"sbert_embedding": sbert_embedding,
})
abstracts.append(abstract)
torch.save(examples, cached_examples_file)
torch.save(abstracts, abstracts_file)
return examples, abstracts
def categorize_embeddings(examples, abstracts):
sbert_embeddings_dict = defaultdict(list)
albert_embeddings_dict = defaultdict(list)
metadata_dict = defaultdict(list)
abstracts_dict = defaultdict(list)
for example, abstract in tqdm(zip(examples, abstracts), desc="Categorizing examples"):
for category in example['categories']:
sbert_embeddings_dict[category].append(torch.tensor(example['sbert_embedding'])) # sbert_embedding of shape (D,)
albert_embeddings_dict[category].append(torch.tensor(example['albert_embedding'])) # albert_embedding of shape (1, D,)
# [DEPRECATED: TOO SLOW] web scrape citation info
# url = f"http://export.arxiv.org/api/query?id_list={example['id']}"
# url = f"https://arxiv2bibtex.org/?q={example['id']}&format=bibtex"
# with urllib.request.urlopen(url) as response:
# html = response.read()
# soup = BeautifulSoup(html, 'html.parser')
# textarea = soup.find_all('textarea')
# bibtex = textarea[0].string.strip()
metadata_dict[category].append({
"id": example['id'],
"authors": example['authors'],
"title": example['title'],
"date": example['date'],
"categories": example['categories'],
})
abstracts_dict[category].append(abstract)
sbert_stacked_embeddings_dict = {}
albert_stacked_embeddings_dict = {}
for category in CS_CATEGORIES:
sbert_stacked_embeddings_dict[category] = torch.stack(sbert_embeddings_dict[category]) # stack == concat along a new dim
albert_stacked_embeddings_dict[category] = torch.cat(albert_embeddings_dict[category]) # cat == concat along a given dim
print(category)
print(len(sbert_embeddings_dict[category]))
print(len(albert_embeddings_dict[category]))
print(len(metadata_dict[category]))
print(len(abstracts_dict[category]))
print()
torch.save(sbert_stacked_embeddings_dict[category], os.path.join("data", f"sbert_embeddings_{category[3:]}.pt"))
torch.save(albert_stacked_embeddings_dict[category], os.path.join("data", f"albert_embeddings_{category[3:]}.pt"))
torch.save(metadata_dict[category], os.path.join("data", f"metadata_{category[3:]}.pt"))
torch.save(abstracts_dict[category], os.path.join("data", f"abstracts_{category[3:]}.pt"))
if __name__ == "__main__":
data_path = '../arxiv-metadata-oai-snapshot.json'
examples, abstracts = load_and_cache_examples(data_path)
print(len(abstracts))
categorize_embeddings(examples, abstracts)