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_pubmed.py 6.2 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
  1. ##############################################################################################
  2. ############ Cleaning and Preprocessing the PubMed publications related to COVID-19 ##########
  3. ##############################################################################################
  4. # For collecting the PubMed publications related to COVID-19, we used the "pymed" library.
  5. # It is avaliable on https://pypi.org/project/pymed/.
  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 ProcessPubmed(Preprocess):
  17. # Defining the function "clean_text" for cleaning and preprocessing any text.
  18. @staticmethod
  19. def __clean_text(text):
  20. if text:
  21. return re.sub(r"\\", " ", re.sub(r"\s+", " ", re.sub(r"\-{2,}", "-",
  22. re.sub("[0-9]*\u200b", "", str(text)).replace("\xad", "-")).replace(
  23. "\u2009", " ").replace("\xa0", " ").replace("\n", " ").replace(
  24. "\ufeff", "").replace("\u202f", "").replace("\u2028", " ").replace(
  25. "\u200f", "").replace("\u200e", "").replace("()", "").replace(
  26. "[]", "").replace("\\'", "\'").replace("\uf06b", "").replace(
  27. "\x96", "").replace("\u200c", ""))).strip()
  28. else:
  29. return None
  30. # Cleaning and preprocessing the dataframe.
  31. def _preprocess(self):
  32. # Defining the "None" value for the "NaN" values.
  33. self._dataframe.replace({np.nan: None}, inplace=True)
  34. # Removing unnecessary columns.
  35. columns_drop = ["methods", "conclusions", "results", "copyrights", "xml", "isbn",
  36. "language", "publication_type", "sections", "publisher", "publisher_location"]
  37. self._dataframe.drop(axis=1, columns=columns_drop, inplace=True)
  38. # Getting the PubMed ID for each paper.
  39. self._dataframe.pubmed_id = self._dataframe.pubmed_id.apply(lambda x: x.split()[0].strip())
  40. # Normalizing the doi for each paper.
  41. self._dataframe.loc[self._dataframe.doi.notnull(), "doi"] = self._dataframe.loc[
  42. self._dataframe.doi.notnull(), "doi"].apply(lambda x: x.split()[0].strip())
  43. # Normalizing the features "abstract", "title" and "journal".
  44. self._dataframe.abstract = self._dataframe.abstract.apply(
  45. lambda x: ProcessPubmed.__clean_text(LatexNodes2Text().latex_to_text(
  46. re.sub(r"\s+", " ", re.sub("%", "\\%", x)))) if x and len(x) > 0 else None)
  47. self._dataframe.title = self._dataframe.title.apply(
  48. lambda x: ProcessPubmed.__clean_text(x) if x and len(x) > 0 else None)
  49. self._dataframe.journal = self._dataframe.journal.apply(ProcessPubmed.__clean_text)
  50. # Setting the feature "keywords" as a tuple of keywords and
  51. # normalizing the keywords for each paper.
  52. self._dataframe.keywords.loc[self._dataframe.keywords.notnull()] = [
  53. tuple([ProcessPubmed.__clean_text(keyword) for keyword in eval(keywords)]) \
  54. if eval(keywords) else None
  55. for keywords in self._dataframe.keywords[self._dataframe.keywords.notnull()]]
  56. # Removing the invalid keywords.
  57. self._dataframe.keywords.loc[self._dataframe.keywords.notnull()] = [
  58. tuple([item for item in keywords if item])
  59. for keywords in self._dataframe.keywords[self._dataframe.keywords.notnull()]]
  60. self._dataframe.keywords.loc[self._dataframe.keywords.notnull()] = \
  61. self._dataframe.keywords.loc[self._dataframe.keywords.notnull()].apply(
  62. lambda x: x if len(x) > 0 else None)
  63. # Correcting the feature "authors".
  64. for idx, authors in enumerate(self._dataframe.authors):
  65. if not eval(authors):
  66. self._dataframe.authors[idx] = None
  67. else:
  68. list_authors = []
  69. for author in eval(authors):
  70. auth = {}
  71. if author["firstname"] and author["lastname"]:
  72. auth["name"] = ProcessPubmed.__clean_text(
  73. "{} {}".format(author["firstname"], author["lastname"]))
  74. elif author["firstname"] and not author["lastname"]:
  75. auth["name"] = ProcessPubmed.__clean_text(author["firstname"])
  76. elif not author["firstname"] and author["lastname"]:
  77. auth["name"] = ProcessPubmed.__clean_text(author["lastname"])
  78. else:
  79. auth["name"] = None
  80. auth["id"] = str(hash("{} - {}".format(auth["name"], "PubMed"))) \
  81. if auth["name"] else None
  82. auth["affiliation"] = ProcessPubmed.__clean_text(author["affiliation"]) \
  83. if "affiliation" in author else None
  84. auth["affil_id"] = str(hash("{} - {}".format(auth["affiliation"], "PubMed"))) if auth["affiliation"] else None
  85. auth["country"] = None
  86. if auth["affiliation"] or auth["name"]:
  87. list_authors.append(auth)
  88. self._dataframe.authors[idx] = tuple(list_authors) if len(list_authors) > 0 else None
  89. # Renaming the features "authors", "keywords" and "journal".
  90. self._dataframe.rename(columns={"authors": "author_affil", "keywords": "auth_keywords",
  91. "journal": "vehicle_name"}, inplace=True)
  92. # Removing the duplicated records by features "title" and "doi".
  93. self._dataframe = pd.concat([
  94. self._dataframe[self._dataframe.title.isnull() | self._dataframe.doi.isnull()],
  95. self._dataframe[self._dataframe.title.notnull() & self._dataframe.doi.notnull()].sort_values(
  96. by=["title", "publication_date"]).drop_duplicates(
  97. ["title", "doi"], "last")], ignore_index=True)
Tip!

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

Comments

Loading...