-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatters.py
218 lines (184 loc) · 8.42 KB
/
formatters.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
def paper_format_apa(reference):
try:
authors = reference.get('author', [])
if authors:
author_str = ', '.join([f"{author['family']}, {author['given'][0]}." for author in authors])
else:
author_str = "Sin autor"
issued = reference.get('issued', {})
year = issued.get('date-parts', [[None]])[0][0]
title = reference.get('title', ["Sin título"])[0]
container_title = reference.get('container-title', [""])[0]
volume = reference.get('volume', "")
issue = reference.get('issue', "")
pages = reference.get('page', "")
doi = reference.get('DOI', "")
apa_citation = f"{author_str} ({year}). {title}. *{container_title}*, {volume}({issue}), {pages}. https://doi.org/{doi}"
return apa_citation
except Exception as e:
return f"Error al formatear en APA: {str(e)}"
def paper_format_vancouver(reference):
try:
authors = reference.get('author', [])
if authors:
author_str = ', '.join([f"{author['family']} {author['given'][0]}." for author in authors])
else:
author_str = "Sin autor"
title = reference.get('title', ["Sin título"])[0]
container_title = reference.get('container-title', [""])[0]
year = reference.get('issued', {}).get('date-parts', [[None]])[0][0]
volume = reference.get('volume', "")
issue = reference.get('issue', "")
pages = reference.get('page', "")
doi = reference.get('DOI', "")
vancouver_citation = f"{author_str}. {title}. {container_title}. {year};{volume}({issue}):{pages}. doi:{doi}"
return vancouver_citation
except Exception as e:
return f"Error al formatear en Vancouver: {str(e)}"
def paper_format_chicago(reference):
try:
authors = reference.get('author', [])
if authors:
author_str = ' and '.join([f"{author['given']} {author['family']}" for author in authors])
else:
author_str = "Sin autor"
title = reference.get('title', ["Sin título"])[0]
container_title = reference.get('container-title', [""])[0]
year = reference.get('issued', {}).get('date-parts', [[None]])[0][0]
volume = reference.get('volume', "")
issue = reference.get('issue', "")
pages = reference.get('page', "")
doi = reference.get('DOI', "")
chicago_citation = f"{author_str}. {year}. \"{title}.\" *{container_title}* {volume}, no. {issue} ({pages}). https://doi.org/{doi}."
return chicago_citation
except Exception as e:
return f"Error al formatear en Chicago: {str(e)}"
def paper_format_mla(reference):
try:
authors = reference.get('author', [])
if authors:
author_str = ', '.join([f"{author['family']}, {author['given']}" for author in authors])
else:
author_str = "Sin autor"
title = reference.get('title', ["Sin título"])[0]
container_title = reference.get('container-title', [""])[0]
volume = reference.get('volume', "")
issue = reference.get('issue', "")
year = reference.get('issued', {}).get('date-parts', [[None]])[0][0]
pages = reference.get('page', "")
doi = reference.get('DOI', "")
mla_citation = f"{author_str}. \"{title}.\" *{container_title}*, vol. {volume}, no. {issue}, {year}, pp. {pages}. doi:{doi}"
return mla_citation
except Exception as e:
return f"Error al formatear en MLA: {str(e)}"
def paper_format_harvard(reference):
try:
authors = reference.get('author', [])
if authors:
author_str = ', '.join([f"{author['family']}, {author['given'][0]}" for author in authors])
else:
author_str = "Sin autor"
title = reference.get('title', ["Sin título"])[0]
container_title = reference.get('container-title', [""])[0]
volume = reference.get('volume', "")
issue = reference.get('issue', "")
year = reference.get('issued', {}).get('date-parts', [[None]])[0][0]
pages = reference.get('page', "")
doi = reference.get('DOI', "")
harvard_citation = f"{author_str}. ({year}) '{title}', {container_title}, {volume}({issue}), {pages}. doi:{doi}"
return harvard_citation
except Exception as e:
return f"Error al formatear en Harvard: {str(e)}"
def paper_format_ieee(reference):
try:
authors = reference.get('author', [])
if authors:
author_str = ', '.join([f"{author['given']} {author['family']}" for author in authors])
else:
author_str = "Sin autor"
title = reference.get('title', ["Sin título"])[0]
container_title = reference.get('container-title', [""])[0]
volume = reference.get('volume', "")
issue = reference.get('issue', "")
year = reference.get('issued', {}).get('date-parts', [[None]])[0][0]
pages = reference.get('page', "")
doi = reference.get('DOI', "")
ieee_citation = f"{author_str}, \"{title},\" {container_title}, vol. {volume}, no. {issue}, pp. {pages}, {year}. doi:{doi}"
return ieee_citation
except Exception as e:
return f"Error al formatear en IEEE: {str(e)}"
def paper_format_iso690(reference):
try:
authors = reference.get('author', [])
if authors:
author_str = '; '.join([f"{author['family']}, {author['given']}" for author in authors])
else:
author_str = "Sin autor"
title = reference.get('title', ["Sin título"])[0]
container_title = reference.get('container-title', [""])[0]
year = reference.get('issued', {}).get('date-parts', [[None]])[0][0]
volume = reference.get('volume', "")
issue = reference.get('issue', "")
pages = reference.get('page', "")
doi = reference.get('DOI', "")
iso690_citation = f"{author_str} ({year}). {title}. {container_title}, {volume}({issue}), {pages}. doi:{doi}"
return iso690_citation
except Exception as e:
return f"Error al formatear en UNE-ISO 690:2024: {str(e)}"
def format_reference(style, reference):
if style == 'APA':
return paper_format_apa(reference)
elif style == 'Vancouver':
return paper_format_vancouver(reference)
elif style == 'Chicago':
return paper_format_chicago(reference)
elif style == 'MLA':
return paper_format_mla(reference)
elif style == 'Harvard':
return paper_format_harvard(reference)
elif style == 'IEEE':
return paper_format_ieee(reference)
elif style == 'UNE-ISO 690:2024':
return paper_format_iso690(reference)
else:
return f"Formato {style} aún no soportado."
def format_book(book_data, style):
try:
title = book_data.get("title", "Sin título")
authors = book_data.get("authors", [])
if authors:
author_str = ', '.join([author['name'] for author in authors])
else:
author_str = "Sin autor"
publishers = book_data.get("publishers", [])
if publishers:
publisher_str = ', '.join([publisher['name'] for publisher in publishers])
else:
publisher_str = "Sin editorial"
year = book_data.get("publish_date", "Sin año")
if style == "APA":
citation = f"{author_str} ({year}). {title}. {publisher_str}."
elif style == "MLA":
citation = f"{author_str}. {title}. {publisher_str}, {year}."
# Agregar otros estilos según sea necesario
else:
citation = f"Formato {style} aún no soportado para libros."
return citation
except Exception as e:
return f"Error al formatear el libro: {str(e)}"
def format_webpage(metadata, style):
try:
title = metadata.get("title", "Sin título")
author = metadata.get("author", "Desconocido")
publication_date = metadata.get("publication_date", "Fecha desconocida")
url = metadata.get("url", "")
if style == "APA":
citation = f"{author}. ({publication_date}). {title}. Recuperado de {url}"
elif style == "MLA":
citation = f"{author}. \"{title}.\" {publication_date}, {url}."
# Agregar otros estilos según sea necesario
else:
citation = f"Formato {style} aún no soportado para páginas web."
return citation
except Exception as e:
return f"Error al formatear la página web: {str(e)}"