Spaces:
Sleeping
Sleeping
samyak152002
commited on
Update annotations.py
Browse files- annotations.py +46 -25
annotations.py
CHANGED
@@ -11,8 +11,11 @@ def extract_pdf_text(file) -> str:
|
|
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 |
-
|
|
|
|
|
15 |
doc.close()
|
|
|
16 |
return full_text
|
17 |
except Exception as e:
|
18 |
print(f"Error extracting text from PDF: {e}")
|
@@ -20,23 +23,28 @@ def extract_pdf_text(file) -> str:
|
|
20 |
|
21 |
def check_language_issues(full_text: str) -> Dict[str, Any]:
|
22 |
"""Check for language issues using LanguageTool."""
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
"
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> bytes:
|
42 |
"""
|
@@ -47,26 +55,29 @@ def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> byt
|
|
47 |
try:
|
48 |
# Open the PDF
|
49 |
doc = fitz.open(stream=file.read(), filetype="pdf") if not isinstance(file, str) else fitz.open(file)
|
|
|
50 |
|
51 |
# Extract words with positions from each page
|
52 |
word_list = [] # List of tuples: (page_number, word, x0, y0, x1, y1)
|
53 |
-
page_text = ""
|
54 |
for page_number in range(len(doc)):
|
55 |
page = doc[page_number]
|
56 |
words = page.get_text("words") # List of tuples: (x0, y0, x1, y1, "word", block_no, line_no, word_no)
|
57 |
for w in words:
|
58 |
word_text = w[4]
|
59 |
word_list.append((page_number, word_text, w[0], w[1], w[2], w[3]))
|
60 |
-
|
|
|
61 |
# Concatenate all words to form the full text
|
62 |
concatenated_text = " ".join([w[1] for w in word_list])
|
63 |
-
|
|
|
64 |
# Iterate over each language issue
|
65 |
-
for issue in language_matches:
|
66 |
offset = issue["offset"]
|
67 |
length = issue["length"]
|
68 |
error_text = concatenated_text[offset:offset+length]
|
69 |
-
|
|
|
70 |
# Find the words that fall within the error span
|
71 |
current_pos = 0
|
72 |
target_words = []
|
@@ -78,22 +89,28 @@ def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> byt
|
|
78 |
target_words.append(word)
|
79 |
current_pos += word_length
|
80 |
|
|
|
|
|
|
|
|
|
81 |
# Add highlight annotations to the target words
|
82 |
for target in target_words:
|
83 |
page_num, word_text, x0, y0, x1, y1 = target
|
84 |
page = doc[page_num]
|
85 |
-
# Define a rectangle around the word
|
86 |
-
rect = fitz.Rect(x0, y0, x1, y1)
|
87 |
# Add a highlight annotation
|
88 |
highlight = page.add_highlight_annot(rect)
|
89 |
highlight.set_colors(stroke=(1, 1, 0)) # Yellow color
|
90 |
highlight.update()
|
|
|
91 |
|
92 |
# Save annotated PDF to bytes
|
93 |
byte_stream = io.BytesIO()
|
94 |
doc.save(byte_stream)
|
95 |
annotated_pdf_bytes = byte_stream.getvalue()
|
96 |
doc.close()
|
|
|
97 |
return annotated_pdf_bytes
|
98 |
except Exception as e:
|
99 |
print(f"Error in highlighting PDF: {e}")
|
@@ -107,8 +124,12 @@ def analyze_pdf(file) -> Tuple[Dict[str, Any], bytes]:
|
|
107 |
return {"error": "Failed to extract text from PDF."}, None
|
108 |
|
109 |
language_issues = check_language_issues(full_text)
|
|
|
|
|
|
|
110 |
issues = language_issues.get("issues", [])
|
111 |
annotated_pdf = highlight_issues_in_pdf(file, issues) if issues else None
|
112 |
return language_issues, annotated_pdf
|
113 |
except Exception as e:
|
|
|
114 |
return {"error": str(e)}, None
|
|
|
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 |
+
print(f"Extracted text from page {page.number + 1}: {len(text)} characters")
|
17 |
doc.close()
|
18 |
+
print(f"Total extracted text length: {len(full_text)} characters")
|
19 |
return full_text
|
20 |
except Exception as e:
|
21 |
print(f"Error extracting text from PDF: {e}")
|
|
|
23 |
|
24 |
def check_language_issues(full_text: str) -> Dict[str, Any]:
|
25 |
"""Check for language issues using LanguageTool."""
|
26 |
+
try:
|
27 |
+
language_tool = language_tool_python.LanguageTool('en-US')
|
28 |
+
matches = language_tool.check(full_text)
|
29 |
+
issues = []
|
30 |
+
for match in matches:
|
31 |
+
issues.append({
|
32 |
+
"message": match.message,
|
33 |
+
"context": match.context,
|
34 |
+
"suggestions": match.replacements[:3] if match.replacements else [],
|
35 |
+
"category": match.category,
|
36 |
+
"rule_id": match.ruleId,
|
37 |
+
"offset": match.offset,
|
38 |
+
"length": match.errorLength
|
39 |
+
})
|
40 |
+
print(f"Total language issues found: {len(issues)}")
|
41 |
+
return {
|
42 |
+
"total_issues": len(issues),
|
43 |
+
"issues": issues
|
44 |
+
}
|
45 |
+
except Exception as e:
|
46 |
+
print(f"Error checking language issues: {e}")
|
47 |
+
return {"error": str(e)}
|
48 |
|
49 |
def highlight_issues_in_pdf(file, language_matches: List[Dict[str, Any]]) -> bytes:
|
50 |
"""
|
|
|
55 |
try:
|
56 |
# Open the PDF
|
57 |
doc = fitz.open(stream=file.read(), filetype="pdf") if not isinstance(file, str) else fitz.open(file)
|
58 |
+
print(f"Opened PDF with {len(doc)} pages.")
|
59 |
|
60 |
# Extract words with positions from each page
|
61 |
word_list = [] # List of tuples: (page_number, word, x0, y0, x1, y1)
|
|
|
62 |
for page_number in range(len(doc)):
|
63 |
page = doc[page_number]
|
64 |
words = page.get_text("words") # List of tuples: (x0, y0, x1, y1, "word", block_no, line_no, word_no)
|
65 |
for w in words:
|
66 |
word_text = w[4]
|
67 |
word_list.append((page_number, word_text, w[0], w[1], w[2], w[3]))
|
68 |
+
print(f"Page {page_number + 1}: Extracted {len(words)} words.")
|
69 |
+
|
70 |
# Concatenate all words to form the full text
|
71 |
concatenated_text = " ".join([w[1] for w in word_list])
|
72 |
+
print(f"Concatenated text length: {len(concatenated_text)} characters.")
|
73 |
+
|
74 |
# Iterate over each language issue
|
75 |
+
for idx, issue in enumerate(language_matches, 1):
|
76 |
offset = issue["offset"]
|
77 |
length = issue["length"]
|
78 |
error_text = concatenated_text[offset:offset+length]
|
79 |
+
print(f"\nIssue {idx}: '{error_text}' at offset {offset} with length {length}")
|
80 |
+
|
81 |
# Find the words that fall within the error span
|
82 |
current_pos = 0
|
83 |
target_words = []
|
|
|
89 |
target_words.append(word)
|
90 |
current_pos += word_length
|
91 |
|
92 |
+
if not target_words:
|
93 |
+
print("No matching words found for this issue.")
|
94 |
+
continue
|
95 |
+
|
96 |
# Add highlight annotations to the target words
|
97 |
for target in target_words:
|
98 |
page_num, word_text, x0, y0, x1, y1 = target
|
99 |
page = doc[page_num]
|
100 |
+
# Define a rectangle around the word with some padding
|
101 |
+
rect = fitz.Rect(x0 - 1, y0 - 1, x1 + 1, y1 + 1)
|
102 |
# Add a highlight annotation
|
103 |
highlight = page.add_highlight_annot(rect)
|
104 |
highlight.set_colors(stroke=(1, 1, 0)) # Yellow color
|
105 |
highlight.update()
|
106 |
+
print(f"Highlighted '{word_text}' on page {page_num + 1} at position ({x0}, {y0}, {x1}, {y1})")
|
107 |
|
108 |
# Save annotated PDF to bytes
|
109 |
byte_stream = io.BytesIO()
|
110 |
doc.save(byte_stream)
|
111 |
annotated_pdf_bytes = byte_stream.getvalue()
|
112 |
doc.close()
|
113 |
+
print("Annotated PDF successfully created.")
|
114 |
return annotated_pdf_bytes
|
115 |
except Exception as e:
|
116 |
print(f"Error in highlighting PDF: {e}")
|
|
|
124 |
return {"error": "Failed to extract text from PDF."}, None
|
125 |
|
126 |
language_issues = check_language_issues(full_text)
|
127 |
+
if "error" in language_issues:
|
128 |
+
return language_issues, None
|
129 |
+
|
130 |
issues = language_issues.get("issues", [])
|
131 |
annotated_pdf = highlight_issues_in_pdf(file, issues) if issues else None
|
132 |
return language_issues, annotated_pdf
|
133 |
except Exception as e:
|
134 |
+
print(f"Error in analyze_pdf: {e}")
|
135 |
return {"error": str(e)}, None
|