-
Notifications
You must be signed in to change notification settings - Fork 4
/
mofdict.py
executable file
·223 lines (183 loc) · 7.03 KB
/
mofdict.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import numpy as np
import regex
import json
from itertools import chain
from chemdataextractor.doc import Paragraph, Heading
from doc import Document
from doc.utils import cleanup_text
from mer import get_method_paragraphs
from mof import MOF, replace_mof
from error import MerError, MofError
from utils import find_abbreviation_from_document, remove_hydrogen
class MofDictionary(object):
"""
MOF dictionary
"""
__version__ = '2.1.0'
def __init__(self, mof_list, **kwargs):
"""
Create MofDictionary from mof_list and paper information
:param mof_list: (list) list of class <MOF>
:param kwargs: information of MofDictionary
- raw_elements : (list of class <chemdataextractor.doc.Paragraph> or <chemdataextractor.doc.Header>)
paragraphs of paper
- metadata : (dict) metadata of paper
- abbreviation : (dict) abbreviation of paper
- raw_method_paragraphs : (list of string) method paragraph of paper. If not exists, automatically find
method paragraph from raw_elements.
"""
if mof_list is None:
self.mof_list = []
else:
self.mof_list = mof_list
self.raw_elements = kwargs.get('raw_elements', [])
self.metadata = kwargs.get('metadata', {})
self.abbreviation = kwargs.get('abbreviation', {})
if 'raw_method_paragraphs' in kwargs:
self.raw_method_paragraphs = kwargs.get('raw_method_paragraphs')
else:
try:
self.raw_method_paragraphs = get_method_paragraphs(self.raw_elements)
except MerError:
self.raw_method_paragraphs = []
self._set_doi_to_mofs()
@classmethod
def from_file(cls, filepath, reader=None, fill_condition=True, standard_unit=True, find_abbreviation=False,
convert_precursor=False, character_embedidng=True):
"""
:param filepath: (str, bytes, os.PathLike or pathlib.Path) Path of file
:param reader: (reader.Reader) Reader of file.
:param fill_condition:
:param standard_unit:
:param find_abbreviation: If True, find abbreviation of paper.
"""
doc = Document(filepath, reader)
elements = doc.elements
metadata = doc.metadata
raw_method_paragraphs = get_method_paragraphs(elements)
# Generate MOF list
mof_list = []
for paragraph in raw_method_paragraphs:
try:
mof = MOF.from_paragraph(paragraph, classify_material=False, standard_unit=standard_unit,
database=doc.database, convert_precursor=convert_precursor,
character_embedding=character_embedidng, metadata=metadata)
mof_list.append(mof)
except MofError:
pass
# Processing replace precursors
mof_list = replace_mof(mof_list)
# Classify precursors and extra chemicals
for mof in mof_list:
mof.classify_material()
# MOF must have metal precursors
mof_list = [mof for mof in mof_list if mof.M_precursor]
O_precursor_iter = chain.from_iterable([mof.get_material_list('O_precursor') for mof in mof_list])
# Find abbreviation
if find_abbreviation:
chemical_abbreviation = find_abbreviation_from_document(doc, O_precursor_iter)
for mof in mof_list:
for linker in mof.O_precursor:
name = linker['name']
name = remove_hydrogen(name)
if name in chemical_abbreviation:
linker['full_name'] = chemical_abbreviation[name]
else:
chemical_abbreviation = {}
return MofDictionary(mof_list, raw_elements=elements, metadata=metadata,
raw_method_paragraphs=raw_method_paragraphs, abbreviation=chemical_abbreviation)
@classmethod
def from_dict(cls, file):
if isinstance(file, list):
mof_list = []
for mof_ in file:
mof = MOF.from_dict(mof_)
mof_list.append(mof)
return MofDictionary(mof_list)
else:
raise TypeError(f'file must be list or dict, not {type(file)}')
@classmethod
def from_json(cls, path):
with open(path, 'r', encoding='utf-8') as f:
file = json.load(f)
return MofDictionary.from_dict(file)
@property
def method_paragraphs(self):
return [cleanup_text(text) for text in self.raw_method_paragraphs]
@property
def elements(self):
element_list = []
for raw_element in self.raw_elements:
text = raw_element.text
text = cleanup_text(text)
text = regex.sub(r"\s+", " ", text)
if isinstance(raw_element, Paragraph):
element_list.append(Paragraph(text))
elif isinstance(raw_element, Heading):
element_list.append(Heading(text))
else:
raise TypeError()
return element_list
def __repr__(self):
return f"""Title : {self.title}\nDoi : {self.doi}\nJournal : {self.journal}\nDate : {self.date}"""
def __len__(self):
return len(self.mof_list)
def __iter__(self):
for mof in self.mof_list:
yield mof
def __getitem__(self, item):
return self.mof_list[item]
def __delitem__(self, item):
del self.mof_list[item]
def __contains__(self, item):
return item in self.mof_list
def __bool__(self):
if self.mof_list:
return True
else:
return False
def to_dict(self, extract_all=False):
return [mof.to_dict(extract_all) for mof in self.mof_list]
def _set_doi_to_mofs(self):
if self.doi is not None:
for mof in self.mof_list:
mof.doi = self.doi
def _remove_wrong_mof(self):
for mof in self.mof_list:
if mof.M_precursor and mof.O_precursor:
pass
else:
self.mof_list.remove(mof)
def _fill_condition(self):
before_temp = None
before_time = None
for mof in self.mof_list:
if mof.temperature:
before_temp = mof.temperature
if mof.time:
before_time = mof.time
if not self.temperature:
self.temperature = before_temp
if not self.time:
self.time = before_time
@property
def title(self):
return self.metadata.get('title')
@property
def doi(self):
return self.metadata.get('doi')
@property
def url(self):
if self.doi is None:
return None
else:
return "https://doi.org/" + self.doi
@property
def journal(self):
return self.metadata.get('journal')
@property
def author_list(self):
return self.metadata.get('author_list')
@property
def date(self):
return self.metadata.get('date')