#!/usr/bin/env python3
import sys,lzma,math,collections,os

def parse(path):
    opener=lzma.open if path.endswith('.xz') else open
    n=m=0; sizes=collections.Counter(); pos=collections.Counter(); neg=collections.Counter(); occ=collections.Counter(); clauses=[]
    with opener(path,'rt',encoding='ascii',errors='ignore') as f:
        cur=[]
        for line in f:
            if not line or line[0] in 'c%0': continue
            if line[0]=='p':
                sp=line.split(); n=int(sp[2]); m=int(sp[3]); continue
            for tok in line.split():
                lit=int(tok)
                if lit==0:
                    if cur:
                        clauses.append(tuple(cur)); sizes[len(cur)]+=1
                        for l in cur:
                            occ[abs(l)]+=1
                            (pos if l>0 else neg)[abs(l)]+=1
                        cur=[]
                else: cur.append(lit)
    return n,m,sizes,pos,neg,occ,clauses
for path in sys.argv[1:]:
    n,m,sizes,pos,neg,occ,clauses=parse(path)
    root=int(math.isqrt(n))
    print('\n',os.path.basename(path))
    print('nvars',n,'root',root,'square',root*root==n,'clauses',m,'parsed',len(clauses),'sizes',dict(sorted(sizes.items())))
    deg=[occ[i] for i in range(1,n+1)]
    print('occ min/max/avg',min(deg),max(deg),sum(deg)/n)
    print('pos min/max/avg',min(pos[i] for i in range(1,n+1)),max(pos[i] for i in range(1,n+1)),sum(pos.values())/n)
    print('neg min/max/avg',min(neg[i] for i in range(1,n+1)),max(neg[i] for i in range(1,n+1)),sum(neg.values())/n)
    print('sample sizes>3',[(len(c),c[:min(10,len(c))]) for c in clauses if len(c)>3][:3])
