File size: 6,625 Bytes
09801ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Graph builder module
import networkx as nx
import re
import networkx as nx
import re
from config.settings import Settings
from graph.schema import detect_schema
from utils.paths import STORAGE_BASE


def detect_currency_from_value(value: str) -> str:
    """Detect currency from a value string like '$100' or '₹1000'."""
    if not isinstance(value, str):
        return 'USD'
    
    value = value.strip()
    
    # Check multi-char symbols first
    if value.startswith('A$') or 'AUD' in value.upper():
        return 'AUD'
    if value.startswith('C$') or 'CAD' in value.upper():
        return 'CAD'
    if value.startswith('S$') or 'SGD' in value.upper():
        return 'SGD'
    if value.startswith('CHF') or 'CHF' in value.upper():
        return 'CHF'
    
    # Single char symbols
    if '€' in value:
        return 'EUR'
    if '£' in value:
        return 'GBP'
    if '₹' in value:
        return 'INR'
    if '$' in value and not any(x in value for x in ['A$', 'C$', 'S$']):
        return 'USD'
    if 'Â¥' in value:
        return 'JPY' if '.' not in value else 'CNY'
    
    return 'USD'


class GraphBuilder:
    @staticmethod
    def build(company_id: str, tables: list, default_currency: str = 'USD', source_files: list = None):
        """
        Build knowledge graph from tabular data with multi-currency and source tracking.
        
        Args:
            company_id: Company identifier
            tables: List of pandas DataFrames
            default_currency: Default currency if none detected
            source_files: List of source file names (one per table)
        """
        G = nx.Graph()
        currencies_detected = {}  # Track currency counts
        sources_tracked = {}  # Track records per source file

        for table_idx, df in enumerate(tables):
            # Get source file name for this table
            source_file = source_files[table_idx] if source_files and table_idx < len(source_files) else f"file_{table_idx}"
            sources_tracked[source_file] = sources_tracked.get(source_file, 0)
            
            schema = detect_schema(df.columns)
            print(f"📊 Detected schema for {source_file}: {schema}")
            print(f"📊 DataFrame columns: {list(df.columns)}")
            print(f"📊 DataFrame shape: {df.shape}")

            for idx, row in df.iterrows():
                # Create invoice node with source file tracking
                inv_id = f"invoice:{row[schema['invoice']]}_{source_file}" if "invoice" in schema else f"invoice:row_{idx}_{source_file}"
                
                # Get amount and detect currency from the value
                amount_raw = row.get(schema.get("amount"), 0.0)
                currency = default_currency
                
                try:
                    if isinstance(amount_raw, str):
                        # Detect currency from the string value
                        currency = detect_currency_from_value(amount_raw)
                        # Strip currency symbols and commas
                        amount = re.sub(r'[₹$€£¥,\s]', '', amount_raw)
                        amount = float(amount)
                    else:
                        amount = float(amount_raw)
                except:
                    amount = 0.0
                
                # Track currency frequency
                currencies_detected[currency] = currencies_detected.get(currency, 0) + 1
                sources_tracked[source_file] = sources_tracked.get(source_file, 0) + 1
                
                # Store amount, currency AND source_file in the invoice node
                G.add_node(inv_id, type="invoice", label=inv_id, amount=amount, currency=currency, source_file=source_file)

                # Connect to customer (Fallback if missing)
                if "customer" in schema:
                    val = str(row[schema["customer"]])
                    node_id = f"customer:{val}"
                    if not G.has_node(node_id):
                        G.add_node(node_id, type="customer", label=val)
                    G.add_edge(inv_id, node_id, relation="has_customer")
                else:
                    # Fallback for missing customer
                    node_id = "customer:Unknown"
                    if not G.has_node(node_id):
                        G.add_node(node_id, type="customer", label="Unknown Customer")
                    G.add_edge(inv_id, node_id, relation="has_customer")

                # Connect to product (Fallback if missing)
                if "product" in schema:
                    val = str(row[schema["product"]])
                    node_id = f"product:{val}"
                    if not G.has_node(node_id):
                        G.add_node(node_id, type="product", label=val)
                    G.add_edge(inv_id, node_id, relation="has_product")
                else:
                    # Fallback for missing product
                    node_id = "product:General"
                    if not G.has_node(node_id):
                        G.add_node(node_id, type="product", label="General Item")
                    G.add_edge(inv_id, node_id, relation="has_product")

                # Connect to date (Optional)
                if "date" in schema:
                    val = str(row[schema["date"]])
                    node_id = f"date:{val}"
                    if not G.has_node(node_id):
                        G.add_node(node_id, type="date", label=val)
                    G.add_edge(inv_id, node_id, relation="has_date")
                    
        print(f"📊 Built graph with {G.number_of_nodes()} nodes and {G.number_of_edges()} edges")
        print(f"💰 Currencies detected: {currencies_detected}")

        # Save graph using pickle format to USER-SPECIFIC directory
        import pickle
        
        # Primary: User-specific graph directory (Consolidated storage)
        user_graph_dir = STORAGE_BASE / company_id / "graph"
        user_graph_dir.mkdir(parents=True, exist_ok=True)
        user_graph_path = user_graph_dir / f"{company_id}.gpickle"
        
        with open(user_graph_path, 'wb') as f:
            pickle.dump(G, f)
        print(f"✅ Graph saved to user directory: {user_graph_path}")
        
        # Also save to Settings.GRAPH_DIR for backward compatibility
        output_path = Settings.GRAPH_DIR / f"{company_id}.gpickle"
        output_path.parent.mkdir(parents=True, exist_ok=True)
        with open(output_path, 'wb') as f:
            pickle.dump(G, f)
        print(f"✅ Graph also saved to: {output_path}")
        
        return G