File size: 1,401 Bytes
9002555
 
 
0743bb0
9002555
 
0743bb0
 
 
9002555
 
0743bb0
 
 
9002555
0743bb0
9002555
 
 
 
 
0743bb0
 
 
 
 
 
9002555
d57efd6
9002555
 
 
 
 
 
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
# Get reference
class Metadata:
    def __init__(self, reference):
        self.reference = reference

    def add_metadata(self, documents, metadata):
        """Add metadata to each document and include page number."""
        for page_number, document in enumerate(documents, start=1):
            # Ensure the document has a metadata attribute
            if not hasattr(document, "metadata") or document.metadata is None:
                document.metadata = {}
            
            # Update metadata with page number
            document.metadata["page"] = page_number
            document.metadata.update(metadata)
        
        return documents

    def _generate_metadata(self):
        """Generate metadata and return it."""
        metadata = {
            "title": self.reference["title"],
            "author": self.reference["author"],
            "category": self.reference["category"],
            "year": self.reference["year"],
            "publisher": self.reference["publisher"],
            "reference": f"{self.reference['author']}. ({self.reference['year']}). *{self.reference['title']}*. {self.reference['publisher']}."  # APA style reference
        }

        return metadata

    def apply_metadata(self, documents):
        """Apply generated metadata to documents."""
        metadata = self._generate_metadata()
        return self.add_metadata(documents, metadata)