Spaces:
Sleeping
Sleeping
samyak152002
commited on
Update annotations.py
Browse files- annotations.py +20 -3
annotations.py
CHANGED
@@ -8,12 +8,15 @@ import io
|
|
8 |
def extract_pdf_text(file) -> str:
|
9 |
"""Extracts full text from a PDF file using PyMuPDF."""
|
10 |
try:
|
|
|
11 |
doc = fitz.open(stream=file.read(), filetype="pdf") if not isinstance(file, str) else fitz.open(file)
|
12 |
full_text = ""
|
13 |
-
for page in doc:
|
14 |
text = page.get_text("text")
|
15 |
full_text += text + "\n"
|
|
|
16 |
doc.close()
|
|
|
17 |
return full_text
|
18 |
except Exception as e:
|
19 |
print(f"Error extracting text from PDF: {e}")
|
@@ -28,13 +31,14 @@ def check_language_issues(full_text: str) -> Dict[str, Any]:
|
|
28 |
for match in matches:
|
29 |
issues.append({
|
30 |
"message": match.message,
|
31 |
-
"context": match.context,
|
32 |
"suggestions": match.replacements[:3] if match.replacements else [],
|
33 |
"category": match.category,
|
34 |
"rule_id": match.ruleId,
|
35 |
"offset": match.offset,
|
36 |
"length": match.errorLength
|
37 |
})
|
|
|
38 |
return {
|
39 |
"total_issues": len(issues),
|
40 |
"issues": issues
|
@@ -52,6 +56,7 @@ def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> byt
|
|
52 |
try:
|
53 |
# Open the PDF
|
54 |
doc = fitz.open(stream=file.read(), filetype="pdf") if not isinstance(file, str) else fitz.open(file)
|
|
|
55 |
|
56 |
# Extract words with positions from each page
|
57 |
word_list = [] # List of tuples: (page_number, word, x0, y0, x1, y1)
|
@@ -61,15 +66,18 @@ def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> byt
|
|
61 |
for w in words:
|
62 |
word_text = w[4]
|
63 |
word_list.append((page_number, word_text, w[0], w[1], w[2], w[3]))
|
|
|
64 |
|
65 |
# Concatenate all words to form the full text
|
66 |
concatenated_text = " ".join([w[1] for w in word_list])
|
|
|
67 |
|
68 |
# Iterate over each language issue
|
69 |
-
for issue in language_matches:
|
70 |
offset = issue["offset"]
|
71 |
length = issue["length"]
|
72 |
error_text = concatenated_text[offset:offset+length]
|
|
|
73 |
|
74 |
# Find the words that fall within the error span
|
75 |
current_pos = 0
|
@@ -82,6 +90,10 @@ def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> byt
|
|
82 |
target_words.append(word)
|
83 |
current_pos += word_length
|
84 |
|
|
|
|
|
|
|
|
|
85 |
# Add highlight annotations to the target words
|
86 |
for target in target_words:
|
87 |
page_num, word_text, x0, y0, x1, y1 = target
|
@@ -92,6 +104,7 @@ def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> byt
|
|
92 |
highlight = page.add_highlight_annot(rect)
|
93 |
highlight.set_colors(stroke=(1, 1, 0)) # Yellow color
|
94 |
highlight.update()
|
|
|
95 |
|
96 |
# Save annotated PDF to bytes
|
97 |
byte_stream = io.BytesIO()
|
@@ -112,6 +125,8 @@ def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> byt
|
|
112 |
def analyze_pdf(file) -> Tuple[Dict[str, Any], bytes]:
|
113 |
"""Analyzes the PDF for language issues and returns results and annotated PDF."""
|
114 |
try:
|
|
|
|
|
115 |
full_text = extract_pdf_text(file)
|
116 |
if not full_text:
|
117 |
return {"error": "Failed to extract text from PDF."}, None
|
@@ -121,6 +136,8 @@ def analyze_pdf(file) -> Tuple[Dict[str, Any], bytes]:
|
|
121 |
return language_issues, None
|
122 |
|
123 |
issues = language_issues.get("issues", [])
|
|
|
|
|
124 |
annotated_pdf = highlight_issues_in_pdf(file, issues) if issues else None
|
125 |
return language_issues, annotated_pdf
|
126 |
except Exception as e:
|
|
|
8 |
def extract_pdf_text(file) -> str:
|
9 |
"""Extracts full text from a PDF file using PyMuPDF."""
|
10 |
try:
|
11 |
+
# Open the PDF file
|
12 |
doc = fitz.open(stream=file.read(), filetype="pdf") if not isinstance(file, str) else fitz.open(file)
|
13 |
full_text = ""
|
14 |
+
for page_num, page in enumerate(doc, start=1):
|
15 |
text = page.get_text("text")
|
16 |
full_text += text + "\n"
|
17 |
+
print(f"Extracted text from page {page_num}: {len(text)} characters.")
|
18 |
doc.close()
|
19 |
+
print(f"Total extracted text length: {len(full_text)} characters.")
|
20 |
return full_text
|
21 |
except Exception as e:
|
22 |
print(f"Error extracting text from PDF: {e}")
|
|
|
31 |
for match in matches:
|
32 |
issues.append({
|
33 |
"message": match.message,
|
34 |
+
"context": match.context.strip(),
|
35 |
"suggestions": match.replacements[:3] if match.replacements else [],
|
36 |
"category": match.category,
|
37 |
"rule_id": match.ruleId,
|
38 |
"offset": match.offset,
|
39 |
"length": match.errorLength
|
40 |
})
|
41 |
+
print(f"Total language issues found: {len(issues)}")
|
42 |
return {
|
43 |
"total_issues": len(issues),
|
44 |
"issues": issues
|
|
|
56 |
try:
|
57 |
# Open the PDF
|
58 |
doc = fitz.open(stream=file.read(), filetype="pdf") if not isinstance(file, str) else fitz.open(file)
|
59 |
+
print(f"Opened PDF with {len(doc)} pages.")
|
60 |
|
61 |
# Extract words with positions from each page
|
62 |
word_list = [] # List of tuples: (page_number, word, x0, y0, x1, y1)
|
|
|
66 |
for w in words:
|
67 |
word_text = w[4]
|
68 |
word_list.append((page_number, word_text, w[0], w[1], w[2], w[3]))
|
69 |
+
print(f"Total words extracted: {len(word_list)}")
|
70 |
|
71 |
# Concatenate all words to form the full text
|
72 |
concatenated_text = " ".join([w[1] for w in word_list])
|
73 |
+
print(f"Concatenated text length: {len(concatenated_text)} characters.")
|
74 |
|
75 |
# Iterate over each language issue
|
76 |
+
for idx, issue in enumerate(language_matches, start=1):
|
77 |
offset = issue["offset"]
|
78 |
length = issue["length"]
|
79 |
error_text = concatenated_text[offset:offset+length]
|
80 |
+
print(f"\nIssue {idx}: '{error_text}' at offset {offset} with length {length}")
|
81 |
|
82 |
# Find the words that fall within the error span
|
83 |
current_pos = 0
|
|
|
90 |
target_words.append(word)
|
91 |
current_pos += word_length
|
92 |
|
93 |
+
if not target_words:
|
94 |
+
print("No matching words found for this issue.")
|
95 |
+
continue
|
96 |
+
|
97 |
# Add highlight annotations to the target words
|
98 |
for target in target_words:
|
99 |
page_num, word_text, x0, y0, x1, y1 = target
|
|
|
104 |
highlight = page.add_highlight_annot(rect)
|
105 |
highlight.set_colors(stroke=(1, 1, 0)) # Yellow color
|
106 |
highlight.update()
|
107 |
+
print(f"Highlighted '{word_text}' on page {page_num + 1} at position ({x0}, {y0}, {x1}, {y1})")
|
108 |
|
109 |
# Save annotated PDF to bytes
|
110 |
byte_stream = io.BytesIO()
|
|
|
125 |
def analyze_pdf(file) -> Tuple[Dict[str, Any], bytes]:
|
126 |
"""Analyzes the PDF for language issues and returns results and annotated PDF."""
|
127 |
try:
|
128 |
+
# Reset file pointer before reading
|
129 |
+
file.seek(0)
|
130 |
full_text = extract_pdf_text(file)
|
131 |
if not full_text:
|
132 |
return {"error": "Failed to extract text from PDF."}, None
|
|
|
136 |
return language_issues, None
|
137 |
|
138 |
issues = language_issues.get("issues", [])
|
139 |
+
# Reset file pointer before highlighting
|
140 |
+
file.seek(0)
|
141 |
annotated_pdf = highlight_issues_in_pdf(file, issues) if issues else None
|
142 |
return language_issues, annotated_pdf
|
143 |
except Exception as e:
|