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 4.4 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
  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, 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"\s+", " ", re.sub("[0-9]*\u200b", "", str(text)).replace(
  22. "\u2009", " ").replace("\xa0", " ").replace("\n", " ").replace(
  23. "\ufeff", "").replace("\u202f", "").replace("\u2028", " ").replace(
  24. "\u200f", "")).strip()
  25. else:
  26. return None
  27. def _preprocess(self):
  28. # Defining the "None" value for the "NaN" values.
  29. self._dataframe.replace({np.nan: None}, inplace=True)
  30. # Removing unnecessary columns.
  31. columns_drop = ["methods", "conclusions", "results", "copyrights", "xml", "isbn",
  32. "language", "publication_type", "sections", "publisher", "publisher_location"]
  33. self._dataframe.drop(axis=1, columns=columns_drop, inplace=True)
  34. # Getting the PubMed ID for each paper.
  35. self._dataframe.pubmed_id = self._dataframe.pubmed_id.apply(lambda x: x.split()[0].strip())
  36. # Normalizing the features "abstract" and "title".
  37. self._dataframe.abstract = self._dataframe.abstract.apply(
  38. lambda x: LatexNodes2Text().latex_to_text(
  39. re.sub(r"\s+", " ", re.sub("%", "\\%", x))) if x and len(x) > 0 else None)
  40. self._dataframe.title = self._dataframe.title.apply(
  41. lambda x: x.replace("\n", " ") if x and len(x) > 0 else None)
  42. # Setting the feature "keywords" as a tuple of keywords and
  43. # normalizing the keywords for each paper.
  44. self._dataframe.keywords.loc[self._dataframe.keywords.notnull()] = [
  45. tuple([ProcessPubmed.__clean_text(keyword) for keyword in eval(keywords)]) \
  46. if eval(keywords) else None
  47. for keywords in self._dataframe.keywords[self._dataframe.keywords.notnull()]]
  48. # Correcting the feature "authors".
  49. for idx, authors in enumerate(self._dataframe.authors):
  50. if not eval(authors):
  51. self._dataframe.authors[idx] = None
  52. else:
  53. list_authors = []
  54. for author in eval(authors):
  55. auth = {}
  56. if author["firstname"] and author["lastname"]:
  57. auth["name"] = ProcessPubmed.__clean_text(
  58. "{} {}".format(author["firstname"], author["lastname"]))
  59. elif author["firstname"] and not author["lastname"]:
  60. auth["name"] = ProcessPubmed.__clean_text(author["firstname"])
  61. elif not author["firstname"] and author["lastname"]:
  62. auth["name"] = ProcessPubmed.__clean_text(author["lastname"])
  63. if "affiliation" in author:
  64. auth["affiliation"] = ProcessPubmed.__clean_text(author["affiliation"])
  65. else:
  66. auth["affiliation"] = None
  67. if "name" in auth:
  68. list_authors.append(auth)
  69. if list_authors:
  70. self._dataframe.authors[idx] = tuple(list_authors)
  71. else:
  72. self._dataframe.authors[idx] = None
  73. # Renaming the features "authors", "keywords" and "journal".
  74. self._dataframe.rename(columns={"authors": "author_affil", "keywords": "auth_keywords",
  75. "journal": "vehicle_name"}, inplace=True)
Tip!

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

Comments

Loading...