File size: 1,917 Bytes
434cf9f
 
 
99cad96
434cf9f
99cad96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
434cf9f
99cad96
 
 
434cf9f
 
 
 
 
99cad96
 
434cf9f
99cad96
434cf9f
99cad96
434cf9f
99cad96
 
434cf9f
99cad96
434cf9f
99cad96
434cf9f
 
99cad96
 
434cf9f
99cad96
 
 
 
 
 
 
 
434cf9f
99cad96
434cf9f
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
import os
import fitz  # PyMuPDF

def check_files_and_contents(directory, expected_titles, strings_to_check):
    results = []
    
    # Check if the directory exists first to avoid crashing
    if not os.path.exists(directory):
        return [-1] * len(expected_titles)
        
    available_files = os.listdir(directory)
    
    for expected_title, string_to_check in zip(expected_titles, strings_to_check):
        matching_file = None
        
        # Substring match: check if the core title is inside the generated filename
        for file in available_files:
            if expected_title in file and file.endswith(".pdf"):
                matching_file = file
                break
        
        # If no file contains the expected title, fail this check
        if not matching_file:
            results.append(-1)
            continue
            
        file_path = os.path.join(directory, matching_file)
        
        try:
            doc = fitz.open(file_path)
            text = ""
            for page in doc:
                text += page.get_text()
            
            # Check if the PDF content matches expectations
            if string_to_check in text:
                results.append(1)
            else:
                results.append(0)
        except Exception as e:
            print(f"Error reading {matching_file}: {e}")
            results.append(0)
        finally:
            if 'doc' in locals() and doc:
                doc.close()
                
    return results

# Updated target directory for tasks 002 and 005
directory = "/home/user/Documents/Blog1"

expected_titles = [
    "LLM Powered Autonomous Agents", 
    "Thinking about High-Quality Human Data"
]
strings_to_check = [
    "LLM Powered Autonomous Agents", 
    "Thinking about High-Quality Human Data"
]

results = check_files_and_contents(directory, expected_titles, strings_to_check)
print(results)