-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_scrapper.py
191 lines (163 loc) · 7.12 KB
/
url_scrapper.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import argparse
import logging
import multiprocessing
import re
from ast import literal_eval
from pathlib import Path
import pandas as pd
from text_cleaner import TextCleaner
from utils import parallelize_dataframe, setup_log
_logger = logging.getLogger(__name__)
def _retrieve_urls(text: str) -> str:
regex = '\\b(ht|f)tp[s]?://[a-zA-Z0-9:/?_.&=#\-\~\˜]+[.,\\d\)]?'
urls = []
for match in re.finditer(regex, text):
start, end = match.span()
url = text[start:end].strip()
if url.endswith('.') or url.endswith(',') or url.endswith(')'):
url = url[:-1].strip()
url = re.sub('\˜', '~', url)
_logger.debug(f'Found {url}')
urls.append(url)
urls = list(set(urls))
sorted_urls: list[str] = []
for url in reversed(urls):
if 'github' in url or 'gitlab' in url:
sorted_urls.insert(0, url)
else:
sorted_urls.append(url)
return ' '.join(sorted_urls)
def _clean_and_get_urls_for_paper(paper: pd.Series, stop_when='') -> None:
text_cleaner = TextCleaner(debug=True)
title = paper["title"]
text = literal_eval(paper["paper"])
# stop = len(stop_when) > 0
_logger.debug(
f'\nText from PDF:\n###########################\n{text}\n###########################')
text = text_cleaner.remove_from_word_to_end(
text, 'references')
text = text_cleaner.remove_from_word_to_end(
text, 'acknowledgment')
text = text_cleaner.remove_from_word_to_end(
text, 'acknowledgement')
text = text_cleaner.remove_before_title(text, title)
# text = text_cleaner.remove_between_title_abstract(text, title)
text = text_cleaner.replace_symbol_by_letters(text)
text = text_cleaner.remove_cid(text)
text = text_cleaner.remove_equations(text)
text = text_cleaner.remove_numbers_only_lines(text)
text = text_cleaner.remove_tabular_data(text)
# run these 3 again to remove consecutive lines
text = text_cleaner.remove_equations(text)
text = text_cleaner.remove_numbers_only_lines(text)
text = text_cleaner.remove_tabular_data(text)
text = ' '.join(text.split())
text = text_cleaner.aglutinate_urls(text)
text = _retrieve_urls(text)
urls = '\n'.join(text.split())
_logger.info(f'\n###########################\n\nTitle: \n{title}')
_logger.info(
f'\n###########################\n\nURLs:\n{urls}')
def _clean_and_get_urls(df: pd.DataFrame) -> pd.DataFrame:
text_cleaner = TextCleaner()
# drop papers that are not usable
total_papers = len(df)
min_title_len = 4
df_not_nan = df[df['title'].notna() & df['paper'].notna()]
new_df = df_not_nan[df_not_nan['title'].map(len) > min_title_len]
new_total_papers = len(new_df)
if total_papers - new_total_papers > 0:
_logger.debug(f'Dropped {total_papers - new_total_papers} papers')
df = new_df
df.loc[:, 'paper'] = df['paper'].apply(literal_eval)
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.remove_from_word_to_end, from_word='references')
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.remove_from_word_to_end, from_word='acknowledgment')
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.remove_from_word_to_end, from_word='acknowledgement')
df.loc[:, 'paper'] = df.apply(
_remove_before_title, axis=1, text_cleaner=text_cleaner)
# df.loc[:, 'paper'] = df.apply(
# _remove_between_title_abstract, axis=1, text_cleaner=text_cleaner)
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.replace_symbol_by_letters)
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.remove_cid)
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.remove_equations)
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.remove_numbers_only_lines)
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.remove_tabular_data)
# run these 3 again to remove consecutive lines
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.remove_equations)
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.remove_numbers_only_lines)
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.remove_tabular_data)
df.loc[:, 'paper'] = df['paper'].str.split().str.join(' ')
df.loc[:, 'paper'] = df['paper'].apply(
text_cleaner.aglutinate_urls)
df.loc[:, 'paper'] = df['paper'].apply(_retrieve_urls)
return df
def _remove_before_title(row: pd.Series, text_cleaner: TextCleaner) -> str:
return text_cleaner.remove_before_title(row['paper'], row['title'])
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Extract URLs from papers.")
parser.add_argument('-f', '--file', type=str, default='',
help='file name with paper info')
parser.add_argument('-t', '--title', type=str, default='',
help='title of paper to debug')
parser.add_argument('-i', '--index', type=int, default=-1,
help='index of paper to debug')
parser.add_argument('-l', '--log_level', type=str, default='warning',
choices=('debug', 'info', 'warning',
'error', 'critical', 'print'),
help='log level to debug')
parser.add_argument('-s', '--separator', type=str, default='<#sep#>',
help='csv separator')
args = parser.parse_args()
log_dir = Path('logs/').expanduser()
log_dir.mkdir(exist_ok=True)
setup_log(args.log_level, log_dir / 'url_scrapper.log')
if len(args.separator) == 1:
df = pd.read_csv(args.file, sep=args.separator,
dtype=str, keep_default_na=False)
else:
df = pd.read_csv(args.file, sep=args.separator,
dtype=str, engine='python', keep_default_na=False)
if args.index != -1:
paper = df.iloc[args.index]
specific_paper = True
elif len(args.title) > 0:
file_to_find = args.title.lower()
found_papers = df.loc[df['title'].str.lower().str.find(
file_to_find) >= 0]
if len(found_papers) == 0:
_logger.error(
f"Couldn't find any paper with '{args.title}' in title")
exit(0)
elif len(found_papers) > 1:
_logger.info(
f'Found {len(found_papers)} papers with "{args.title}" in title. Using the first one')
paper = found_papers.iloc[0]
specific_paper = True
else:
specific_paper = False
if specific_paper:
_clean_and_get_urls_for_paper(paper)
else:
n_subprocesses = multiprocessing.cpu_count()//2
if len(df) < n_subprocesses * 3:
df = _clean_and_get_urls(df)
else:
df = parallelize_dataframe(
df, _clean_and_get_urls, n_subprocesses)
df = df.rename(columns={'paper': 'urls'})
new_file_name = Path(args.file).name
splitted_file_name = new_file_name.split('.')
new_file_name = '.'.join(splitted_file_name[:-1]) + '_urls.' + splitted_file_name[-1]
df.to_csv(Path(args.file).parent / new_file_name, sep='|', index=False)