Spaces:
Running
Running
File size: 2,805 Bytes
77db408 | 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 | 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")
|