Spaces:
Running
Running
Andre
deploy: snapshot c96b9ab -- Inventory Journals (Lagerforandringsjournal) na Contabilidade (v1.3.61)
77db408 Download backend/scripts/analyze_classifications.py from inwave-gateway/hub: direct link, hf CLI and curl.
- Browser
- Download file 2.81 kB
-
https://huggingface.co/spaces/inwave-gateway/hub/resolve/main/backend/scripts/analyze_classifications.py
- Command line
-
hf download hf://spaces/inwave-gateway/hub/backend/scripts/analyze_classifications.py
-
curl -L -o analyze_classifications.py https://huggingface.co/spaces/inwave-gateway/hub/resolve/main/backend/scripts/analyze_classifications.py
2.81 kB
| import os | |
| import csv | |
| from io import StringIO | |
| from collections import Counter | |
| def _auto_decode(content: bytes) -> str: | |
| if not content: return "" | |
| if content.startswith(b'\xef\xbb\xbf'): | |
| return content.decode('utf-8-sig') | |
| try: | |
| return content.decode('utf-8') | |
| except: | |
| try: | |
| return content.decode('latin-1') | |
| except: | |
| return content.decode('cp1252', errors='replace') | |
| def analyze_file(path, fields): | |
| counts = {f: Counter() for f in fields} | |
| with open(path, 'rb') as f: | |
| text = _auto_decode(f.read()) | |
| reader = csv.DictReader(StringIO(text), delimiter=';') | |
| for row in reader: | |
| for f in fields: | |
| val = (row.get(f) or "").strip() | |
| counts[f][val] += 1 | |
| return counts | |
| assets_dir = r"backend/assets" | |
| print("Analyzing dim_articles_master.csv...") | |
| article_fields = ["Article_Group", "Article_Type_Code", "Accounting_Code", "Is_Parcel_Article", "Is_Stock_Item"] | |
| article_stats = analyze_file(os.path.join(assets_dir, "dim_articles_master.csv"), article_fields) | |
| print("Analyzing fact_invoice_lines.csv...") | |
| # For invoice lines, we also want to see the relationship between Account_Number and Row_Description for common accounts | |
| invoice_fields = ["Account_Number", "Row_Type", "Goods_Type", "Is_Labour_Cost"] | |
| invoice_stats = analyze_file(os.path.join(assets_dir, "fact_invoice_lines.csv"), invoice_fields) | |
| # Special analysis for Account_Number -> Row_Description mapping | |
| acct_desc_map = {} | |
| with open(os.path.join(assets_dir, "fact_invoice_lines.csv"), 'rb') as f: | |
| text = _auto_decode(f.read()) | |
| reader = csv.DictReader(StringIO(text), delimiter=';') | |
| for row in reader: | |
| acct = (row.get("Account_Number") or "").strip() | |
| desc = (row.get("Row_Description") or "").strip() | |
| if acct not in acct_desc_map: | |
| acct_desc_map[acct] = Counter() | |
| acct_desc_map[acct][desc] += 1 | |
| output = "Article Master Analysis:\n" | |
| for f, count in article_stats.items(): | |
| output += f"\nField: {f}\n" | |
| for val, freq in count.most_common(20): | |
| output += f" - {val}: {freq}\n" | |
| output += "\n" + "="*40 + "\n" | |
| output += "Invoice Lines Analysis:\n" | |
| for f, count in invoice_stats.items(): | |
| output += f"\nField: {f}\n" | |
| for val, freq in count.most_common(20): | |
| output += f" - {val}: {freq}\n" | |
| output += "\n" + "="*40 + "\n" | |
| output += "Account Number to Description Samples:\n" | |
| for acct in sorted(acct_desc_map.keys()): | |
| output += f"\nAccount: {acct}\n" | |
| for desc, freq in acct_desc_map[acct].most_common(3): | |
| output += f" - {desc}: {freq}\n" | |
| with open("backend/classification_analysis.txt", "w", encoding="utf-8") as f: | |
| f.write(output) | |
| print("Analysis complete. Results in backend/classification_analysis.txt") | |