Create citation_validator.py
Browse files- citation_validator.py +39 -0
citation_validator.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
def validate_citation(citation_text, style):
|
| 4 |
+
"""Basic validation for citation format"""
|
| 5 |
+
errors = []
|
| 6 |
+
|
| 7 |
+
if not citation_text or len(citation_text.strip()) < 10:
|
| 8 |
+
errors.append("Citation appears too short")
|
| 9 |
+
|
| 10 |
+
if style == "APA7":
|
| 11 |
+
# Check for basic APA7 patterns
|
| 12 |
+
if not re.search(r'\(\d{4}\)', citation_text):
|
| 13 |
+
errors.append("Missing year in parentheses")
|
| 14 |
+
if not citation_text.strip().endswith('.'):
|
| 15 |
+
errors.append("Citation should end with a period")
|
| 16 |
+
|
| 17 |
+
elif style == "Chicago":
|
| 18 |
+
# Check for basic Chicago patterns
|
| 19 |
+
if '"' not in citation_text and '"' not in citation_text:
|
| 20 |
+
errors.append("Chicago style typically includes quotation marks around titles")
|
| 21 |
+
|
| 22 |
+
return errors
|
| 23 |
+
|
| 24 |
+
def format_authors(authors):
|
| 25 |
+
"""Format author list from Crossref data"""
|
| 26 |
+
if not authors:
|
| 27 |
+
return "Unknown Author"
|
| 28 |
+
|
| 29 |
+
formatted_authors = []
|
| 30 |
+
for author in authors[:5]: # Limit to 5 authors
|
| 31 |
+
given = author.get('given', '')
|
| 32 |
+
family = author.get('family', '')
|
| 33 |
+
if family:
|
| 34 |
+
formatted_authors.append(f"{family}, {given}" if given else family)
|
| 35 |
+
|
| 36 |
+
if len(authors) > 5:
|
| 37 |
+
formatted_authors.append("et al.")
|
| 38 |
+
|
| 39 |
+
return "; ".join(formatted_authors)
|