Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import re
|
3 |
+
|
4 |
+
def parse_references(references):
|
5 |
+
parsed_refs = []
|
6 |
+
prev_author = None
|
7 |
+
for ref in references.split('\n'):
|
8 |
+
# Remove text in italics or quotation marks
|
9 |
+
ref = re.sub(r'<i>.*?</i>|".*?"', '', ref)
|
10 |
+
|
11 |
+
# Check if the reference starts with -- or a dash
|
12 |
+
if ref.strip().startswith('--') or ref.strip().startswith('-'):
|
13 |
+
author = prev_author
|
14 |
+
ref = ref.lstrip('- ') # Remove leading dashes and spaces
|
15 |
+
else:
|
16 |
+
# Find author (first word) and update prev_author
|
17 |
+
author_match = re.search(r'\b\w+', ref)
|
18 |
+
author = author_match.group() if author_match else None
|
19 |
+
prev_author = author
|
20 |
+
|
21 |
+
# Find year (first 4-digit number)
|
22 |
+
year_match = re.search(r'\b\d{4}\b', ref)
|
23 |
+
|
24 |
+
if author and year_match:
|
25 |
+
parsed_refs.append({
|
26 |
+
'author': author,
|
27 |
+
'year': year_match.group(),
|
28 |
+
'full_ref': ref.strip()
|
29 |
+
})
|
30 |
+
return parsed_refs
|
31 |
+
|
32 |
+
def find_citations(text, parsed_refs):
|
33 |
+
citations = {}
|
34 |
+
paragraphs = text.split('\n\n')
|
35 |
+
|
36 |
+
for ref in parsed_refs:
|
37 |
+
author = ref['author']
|
38 |
+
year = ref['year']
|
39 |
+
pattern = rf'{re.escape(author)}.*?{re.escape(year)}'
|
40 |
+
|
41 |
+
citations[ref['full_ref']] = []
|
42 |
+
for paragraph in paragraphs:
|
43 |
+
matches = re.findall(pattern, paragraph, re.IGNORECASE)
|
44 |
+
citations[ref['full_ref']].extend(matches)
|
45 |
+
|
46 |
+
return citations
|
47 |
+
|
48 |
+
def process_text_and_references(text, references):
|
49 |
+
parsed_refs = parse_references(references)
|
50 |
+
citations = find_citations(text, parsed_refs)
|
51 |
+
|
52 |
+
output = []
|
53 |
+
for ref, cites in citations.items():
|
54 |
+
output.append(f"Reference: {ref}")
|
55 |
+
if cites:
|
56 |
+
output.append("In-text citations:")
|
57 |
+
for cite in cites:
|
58 |
+
output.append(f"- {cite}")
|
59 |
+
else:
|
60 |
+
output.append("No in-text citations found.")
|
61 |
+
output.append("") # Add a blank line between references
|
62 |
+
|
63 |
+
return "\n".join(output)
|
64 |
+
|
65 |
+
# Create Gradio interface
|
66 |
+
iface = gr.Interface(
|
67 |
+
fn=process_text_and_references,
|
68 |
+
inputs=[
|
69 |
+
gr.Textbox(label="Text", lines=10),
|
70 |
+
gr.Textbox(label="References", lines=10)
|
71 |
+
],
|
72 |
+
outputs=gr.Textbox(label="References and In-text Citations"),
|
73 |
+
title="In-text Citation Finder",
|
74 |
+
description="Enter your text and references to find in-text citations for each reference."
|
75 |
+
)
|
76 |
+
|
77 |
+
if __name__ == "__main__":
|
78 |
+
iface.launch()
|