Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

preprocess_scopus.py 13 KB

You have to be logged in to leave a comment. Sign In
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
  1. ##############################################################################################
  2. ############ Cleaning and Preprocessing the Scopus publications related to COVID-19 ##########
  3. ##############################################################################################
  4. # For collecting the Scopus publications related to COVID-19, we used the "pybliometrics"
  5. # library. It is avaliable on https://pypi.org/project/pybliometrics/.
  6. ########################################################################
  7. # Uncomment to install the library.
  8. # %pip install pylatexenc
  9. ########################################################################
  10. ########################################################################
  11. # Importing the required libraries.
  12. import re, pandas as pd, numpy as np
  13. from pylatexenc.latex2text import LatexNodes2Text
  14. from preprocess import Preprocess
  15. ########################################################################
  16. class ProcessScopus(Preprocess):
  17. # Defining the function "clean_text" to clean and preprocess any text.
  18. @staticmethod
  19. def __clean_text(text, has_latex=False):
  20. if text:
  21. text = re.sub(r"\u2fff(s|\s)", r"'\1", re.sub(r"\s+", " ", re.sub(r"\ufeff\.?", "",
  22. re.sub(r"\\\\(\’\s)?", "", str(text))))).replace("\u200b", "").replace(
  23. "\ue001", "").replace("\ue061", "").replace("\u202f", "").replace(
  24. "\u2060", "").replace("\u200f", "").replace("\u200e", "").replace(
  25. "\u202c", "").replace("–", "-").replace("&quot", "\"\"").replace(
  26. "\u200c", "").replace("\\u0019", "").replace("\\s", "s").replace(
  27. "\u202a", "").replace("\u202d", "-").replace("\u0383", "-").replace(
  28. "\u20f3", "ó").replace("\u20fa", "ú").replace("\u2fff", "-").strip()
  29. text = text.replace("TNF-alpha induced", "TNF-α induced").replace(
  30. "TNF-Alpha induced", "TNF-α induced").replace(
  31. "TNF- ␣ induced", "TNF-α induced").replace("TNF-αinduced", "TNF-α induced").replace(
  32. "via NF- \u242c B pathway", "via NF-κB pathway").replace(
  33. "via NF-kappaB pathway", "via NF-κB pathway").strip()
  34. if has_latex:
  35. text = LatexNodes2Text().latex_to_text(re.sub("\\?%", "@PER@CENT@", text)).replace(
  36. "@PER@CENT@", "%")
  37. text = re.sub(r"\s+", " ", re.sub(r"\-{2,}", "-",
  38. re.sub(r"\s?\xad(\s|\-)?", "-", text))).replace("\\", "").replace(
  39. "\\%", "%").replace("()", "").replace("[]", "").strip()
  40. return text
  41. else:
  42. return None
  43. # Cleaning and preprocessing the dataframe.
  44. def _preprocess(self):
  45. # Removing the invalid articles.
  46. self._dataframe = self._dataframe.loc[
  47. self._dataframe.id.notnull() & self._dataframe.eid.notnull()]
  48. # Defining the "None" value for the "NaN" values.
  49. self._dataframe.replace({np.nan: None}, inplace=True)
  50. # Defining the "zero" value for the articles without numbers of citation and references.
  51. self._dataframe.citation_num.loc[self._dataframe.citation_num.isnull()] = 0
  52. self._dataframe.ref_count.loc[self._dataframe.ref_count.isnull()] = 0
  53. # Normalizing the feature "abstract".
  54. self._dataframe.abstract.loc[
  55. self._dataframe.abstract.isnull() & self._dataframe.description.notnull()
  56. ] = self._dataframe.description.loc[
  57. self._dataframe.abstract.isnull() & self._dataframe.description.notnull()]
  58. self._dataframe.abstract.loc[self._dataframe.abstract.notnull()] = \
  59. self._dataframe.abstract.loc[self._dataframe.abstract.notnull()].apply(
  60. lambda x: ProcessScopus.__clean_text(x, True))
  61. # Normalizing the feature "vehicle_name".
  62. self._dataframe.vehicle_name.loc[
  63. self._dataframe.conference_name.notnull() & self._dataframe.vehicle_name.notnull()
  64. ] = self._dataframe.conference_name.loc[
  65. self._dataframe.conference_name.notnull() & self._dataframe.vehicle_name.notnull()]
  66. self._dataframe.vehicle_name.loc[self._dataframe.vehicle_name.notnull()] = \
  67. self._dataframe.vehicle_name.loc[self._dataframe.vehicle_name.notnull()].apply(
  68. ProcessScopus.__clean_text)
  69. # Normalizing the feature "title".
  70. self._dataframe.title.loc[self._dataframe.title.notnull()] = self._dataframe.title.loc[
  71. self._dataframe.title.notnull()].apply(ProcessScopus.__clean_text)
  72. # Removing unnecessary columns.
  73. columns_drop = ["eid", "pii", "description", "isbn", "conf_location", "conference_name",
  74. "vehicle_address", "title_edition"]
  75. self._dataframe.drop(axis=1, columns=columns_drop, inplace=True)
  76. # Changing the type of some features.
  77. self._dataframe.loc[:, ["citation_num", "ref_count"]] = self._dataframe.loc[:,
  78. ["citation_num", "ref_count"]].astype(np.float32)
  79. self._dataframe.auth_keywords.loc[self._dataframe.auth_keywords.notnull()] = \
  80. self._dataframe.auth_keywords.loc[self._dataframe.auth_keywords.notnull()].apply(eval)
  81. self._dataframe.index_terms.loc[self._dataframe.index_terms.notnull()] = \
  82. self._dataframe.index_terms.loc[self._dataframe.index_terms.notnull()].apply(eval)
  83. self._dataframe.affiliations.loc[self._dataframe.affiliations.notnull()] = \
  84. self._dataframe.affiliations.loc[self._dataframe.affiliations.notnull()].apply(eval)
  85. self._dataframe.subject_areas.loc[self._dataframe.subject_areas.notnull()] = \
  86. self._dataframe.subject_areas.loc[self._dataframe.subject_areas.notnull()].apply(eval)
  87. self._dataframe.authors.loc[self._dataframe.authors.notnull()] = \
  88. self._dataframe.authors.loc[self._dataframe.authors.notnull()].apply(eval)
  89. self._dataframe.author_affil.loc[self._dataframe.author_affil.notnull()] = \
  90. self._dataframe.author_affil.loc[self._dataframe.author_affil.notnull()].apply(eval)
  91. self._dataframe.references.loc[self._dataframe.references.notnull()] = \
  92. self._dataframe.references.loc[self._dataframe.references.notnull()].apply(eval)
  93. self._dataframe.publication_date = pd.to_datetime(self._dataframe.publication_date)
  94. # Creating the feature "period" from the feature "publication_date".
  95. if "period" not in self._dataframe:
  96. self._dataframe["period"] = self._dataframe.publication_date.apply(
  97. lambda x: "{}-{}".format(x.year, x.month))
  98. # Normalizing the itens contained in the features "auth_keywords" and "index_terms".
  99. self._dataframe.auth_keywords.loc[self._dataframe.auth_keywords.notnull()] = \
  100. self._dataframe.auth_keywords.loc[self._dataframe.auth_keywords.notnull()].apply(
  101. lambda x: tuple([ProcessScopus.__clean_text(item) for item in x]))
  102. self._dataframe.index_terms.loc[self._dataframe.index_terms.notnull()] = \
  103. self._dataframe.index_terms.loc[self._dataframe.index_terms.notnull()].apply(
  104. lambda x: tuple([ProcessScopus.__clean_text(item) for item in x]))
  105. # Checking there are invalid values in the features "auth_keywords", "index_terms" and "subject_areas".
  106. for column in ["auth_keywords", "index_terms", "subject_areas"]:
  107. count = self._dataframe.loc[self._dataframe[column].notnull(), column][
  108. [np.any([item == None or item.lower() == "none" for item in items])
  109. for items in self._dataframe.loc[self._dataframe[column].notnull(), column]]].size
  110. print("{}: {}".format(column, count))
  111. # Removing the invalid values in the features "auth_keywords", "index_terms" and "subject_areas".
  112. for column in ["auth_keywords", "index_terms", "subject_areas"]:
  113. self._dataframe.loc[self._dataframe[column].notnull(), column] = [
  114. tuple([item for item in items if item])
  115. for items in self._dataframe.loc[self._dataframe[column].notnull(), column]]
  116. self._dataframe.loc[self._dataframe[column].notnull(), column] = self._dataframe.loc[
  117. self._dataframe[column].notnull(), column].apply(lambda x: x if len(x) > 0 else None)
  118. # Normalizing the content contained in the features "authors", "affiliations" and "author_affil".
  119. self._dataframe.affiliations.loc[self._dataframe.affiliations.notnull()] = \
  120. self._dataframe.affiliations.loc[self._dataframe.affiliations.notnull()].apply(
  121. lambda x: tuple([{"id": item["id"],
  122. "affiliation": ProcessScopus.__clean_text(item["affiliation"]),
  123. "country": item["country"]} for item in x if item["id"]]))
  124. self._dataframe.author_affil.loc[self._dataframe.author_affil.notnull()] = \
  125. self._dataframe.author_affil.loc[self._dataframe.author_affil.notnull()].apply(
  126. lambda x: tuple([{"id": item["id"],
  127. "name": ProcessScopus.__clean_text(item["name"]), "affil_id": item["affil_id"],
  128. "affiliation": ProcessScopus.__clean_text(item["affiliation"]),
  129. "country": item["country"]} for item in x if item["id"] or item["name"] or \
  130. item["affil_id"] or item["affiliation"] or item["country"]]))
  131. self._dataframe.authors.loc[self._dataframe.authors.notnull()] = \
  132. self._dataframe.authors.loc[self._dataframe.authors.notnull()].apply(
  133. lambda x: tuple([{"id": item["id"],
  134. "name": ProcessScopus.__clean_text(item["name"])} for item in x if item["id"]]))
  135. # Removing the invalid values in the features "authors", "affiliations" and "author_affil".
  136. for column in ["authors", "affiliations", "author_affil"]:
  137. self._dataframe.loc[self._dataframe[column].notnull(), column] = self._dataframe.loc[
  138. self._dataframe[column].notnull(), column].apply(lambda x: x if len(x) > 0 else None)
  139. # Creating the affiliations' and authors' IDs for those that have not a ID.
  140. self._dataframe.author_affil.loc[self._dataframe.author_affil.notnull()] = \
  141. self._dataframe.author_affil.loc[self._dataframe.author_affil.notnull()].apply(
  142. lambda x: tuple([{"id": item["id"] if item["id"] and item["name"] else \
  143. str(hash("{} - {}".format(item["name"], "Scopus"))) if item["name"] else None,
  144. "name": item["name"],
  145. "affil_id": item["affil_id"] if item["affil_id"] and item["affiliation"] else \
  146. str(hash("{} - {}".format(item["affiliation"], "Scopus"))) \
  147. if item["affiliation"] else None,
  148. "affiliation": item["affiliation"], "country": item["country"]}
  149. for item in x]))
  150. # Removing duplicates within the list of affiliations and authors.
  151. self._dataframe.author_affil.loc[self._dataframe.author_affil.notnull()] = [
  152. set([(au["id"], au["name"], au["affil_id"],
  153. au["affiliation"], au["country"]) for au in row])
  154. for row in self._dataframe.author_affil[self._dataframe.author_affil.notnull()]]
  155. self._dataframe.author_affil.loc[self._dataframe.author_affil.notnull()] = [tuple([dict(zip(
  156. ["id", "name", "affil_id", "affiliation", "country"], au)) for au in row])
  157. for row in self._dataframe.author_affil[self._dataframe.author_affil.notnull()]]
  158. # Removing the duplicated records by feature "id".
  159. self._dataframe = self._dataframe.sort_values(by=["id", "period"]).drop_duplicates(
  160. "id", keep="first")
  161. # Removing the duplicated records by features "title" and "doi".
  162. self._dataframe = pd.concat([
  163. self._dataframe[self._dataframe.title.isnull() | self._dataframe.doi.isnull()],
  164. self._dataframe[self._dataframe.title.notnull() & self._dataframe.doi.notnull()].sort_values(
  165. by=["title", "citation_num", "publication_date"]).drop_duplicates(
  166. ["title", "doi"], "last")], ignore_index=True)
  167. # Normalizing the feature "references".
  168. self._dataframe.references.loc[self._dataframe.references.notnull()] = \
  169. self._dataframe.references.loc[self._dataframe.references.notnull()].apply(
  170. lambda x: tuple([
  171. {"id": ref["id"], "title": ProcessScopus.__clean_text(ref["title"], True),
  172. "doi": ProcessScopus.__clean_text(ref["doi"]),
  173. "authors": ProcessScopus.__clean_text(ref["authors"], True)} for ref in x]))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...