OxbridgeEconomics
commited on
Commit
·
fba27b9
1
Parent(s):
ca144fd
commit
Browse files
utils.py
CHANGED
@@ -114,51 +114,54 @@ def isnot_substring(list_a, string_to_check):
|
|
114 |
return True
|
115 |
|
116 |
def extract_reference(row):
|
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 |
-
if
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
|
|
|
|
|
|
162 |
# update_reference(row)
|
163 |
|
164 |
def translate(text):
|
|
|
114 |
return True
|
115 |
|
116 |
def extract_reference(row):
|
117 |
+
try:
|
118 |
+
pattern = next((elem for elem in patterns if elem['site'] == row['site']), None)
|
119 |
+
extracted_text = extract_from_pdf_by_pattern(row['attachment'],pattern)
|
120 |
+
reference_titles = re.findall(pattern['article_regex'], extracted_text)
|
121 |
+
reference_dates = re.findall(pattern['date_regex'], extracted_text)
|
122 |
+
reference_titles = [s.replace(' ', '') for s in reference_titles]
|
123 |
+
reference_dates = [s.replace(' ', '') for s in reference_dates]
|
124 |
+
print(reference_dates, reference_titles)
|
125 |
+
if 'remove' in pattern:
|
126 |
+
for remove_string in pattern['remove']:
|
127 |
+
reference_titles = [s.replace(remove_string, '') for s in reference_titles]
|
128 |
+
for title, date in zip(reference_titles, reference_dates):
|
129 |
+
print(title, date)
|
130 |
+
try:
|
131 |
+
date = datetime.strptime(date, pattern['date_format'])
|
132 |
+
except:
|
133 |
+
date = datetime(2006, 1, 1)
|
134 |
+
dates = []
|
135 |
+
if 'date_range' in pattern:
|
136 |
+
for i in range(pattern['date_range'] + 1):
|
137 |
+
dates.append((date + timedelta(days=i)).strftime('%Y-%m-%d'))
|
138 |
+
dates.append((date - timedelta(days=i)).strftime('%Y-%m-%d'))
|
139 |
+
dates.append(date.strftime('%Y-%m-%d'))
|
140 |
+
date = date.strftime('%Y-%m-%d')
|
141 |
+
if 'split' in pattern:
|
142 |
+
for split_item in pattern['split']:
|
143 |
+
if 'exceptional_string' in split_item:
|
144 |
+
if split_item['string'] in title and isnot_substring(split_item['exceptional_string'], title):
|
145 |
+
title = re.split(split_item['string'], title)[split_item['index']]
|
146 |
+
else:
|
147 |
+
if split_item['string'] in title:
|
148 |
+
title = title.split(split_item['string'])[split_item['index']]
|
149 |
+
if len(data[(data['titleCN'].str.contains(title)) & (data['site'] == row['site']) & (data['publishdate'].isin(dates))]) == 0:
|
150 |
+
print("------------ = 0 ------------")
|
151 |
+
print(date, repr(title))
|
152 |
+
elif len(data[(data['titleCN'].str.contains(title)) & (data['site'] == row['site']) & (data['publishdate'].isin(dates))]) > 1:
|
153 |
+
print("------------ > 1 ------------")
|
154 |
+
print(date, repr(title))
|
155 |
+
else:
|
156 |
+
print("------------ = 1 ------------")
|
157 |
+
reference_df = data[(data['titleCN'].str.contains(title)) & (data['site'] == row['site']) & (data['publishdate'].isin(dates))]
|
158 |
+
row['referenceID'] = reference_df.iloc[0]['id']
|
159 |
+
row['link'] = reference_df.iloc[0]['link']
|
160 |
+
row['sourceID'] = row['id_x']
|
161 |
+
row['refID'] = uuid.uuid5(uuid.NAMESPACE_OID, str(row['sourceID'])+str(row['referenceID']))
|
162 |
+
print(date, repr(title), row['sourceID'],row['referenceID'])
|
163 |
+
except Exception as error:
|
164 |
+
print(error)
|
165 |
# update_reference(row)
|
166 |
|
167 |
def translate(text):
|