Download app/preprocessing.py from Websterfly/Ai-detector-websterfly: direct link, hf CLI and curl.
- Browser
- Download file 2.83 kB
-
https://huggingface.co/spaces/Websterfly/Ai-detector-websterfly/resolve/main/app/preprocessing.py
- Command line
-
hf download hf://spaces/Websterfly/Ai-detector-websterfly/app/preprocessing.py
-
curl -L -o preprocessing.py https://huggingface.co/spaces/Websterfly/Ai-detector-websterfly/resolve/main/app/preprocessing.py
2.83 kB
| """Auditable English research preprocessing with original-character mapping.""" | |
| import re,unicodedata | |
| INVISIBLE=set('\u200b\u200c\u200d\u2060\ufeff\u00ad') | |
| def prepare(text, scope='all'): | |
| # Remove invisibles before grouping combining marks: otherwise an inserted | |
| # zero-width character can prevent canonical composition of accented letters. | |
| filtered=[(c,i,i+1) for i,c in enumerate(text) if c not in INVISIBLE] | |
| chars=[];mapping=[];i=0;changes=len(text)-len(filtered) | |
| while i<len(filtered): | |
| j=i+1 | |
| while j<len(filtered) and unicodedata.combining(filtered[j][0]):j+=1 | |
| original=''.join(c[0] for c in filtered[i:j]) | |
| folded=unicodedata.normalize('NFKC',original) | |
| changes+=folded!=original | |
| for c in folded:chars.append(c);mapping.append([filtered[i][1],filtered[j-1][2]]) | |
| i=j | |
| normalized=''.join(chars) | |
| # Collapse horizontal whitespace, retaining paragraph structure and a source map. | |
| out=[];orig=[];last_space=False | |
| for c,m in zip(normalized,mapping): | |
| if c.isspace() and c!='\n': | |
| if last_space:continue | |
| c=' ' | |
| out.append(c);orig.append(m);last_space=c==' ' | |
| begin=0;end=len(out) | |
| while begin<end and out[begin].isspace():begin+=1 | |
| while end>begin and out[end-1].isspace():end-=1 | |
| out=out[begin:end];orig=orig[begin:end] | |
| normalized=''.join(out) | |
| excluded=[] | |
| if scope=='body': | |
| hit=re.search(r'(?im)^\s*(?:#+\s*|[IVX\d]+[.)]?\s*)?(?:references|bibliography)\s*$',normalized) | |
| if hit:excluded.append({'start':hit.start(),'end':len(normalized),'reason':'reference_section'}) | |
| if scope not in ('all','body'):raise ValueError('scope must be all or body') | |
| end=excluded[0]['start'] if excluded else len(normalized) | |
| analysis=normalized[:end] | |
| sentences=[] | |
| for m in re.finditer(r'\S[\s\S]*?(?:[.!?](?=\s|$)|$)',analysis): | |
| if not m.group().strip():continue | |
| a,b=m.span() | |
| sentences.append({'start':a,'end':b,'original_start':orig[a][0],'original_end':orig[b-1][1],'text':m.group().strip()}) | |
| warnings=[] | |
| if len(analysis.split())<300:warnings.append('Under 300 words: limited context; treat interpretations as uncertain.') | |
| if re.search('[\u0370-\u03ff\u0400-\u04ff]',analysis):warnings.append('Greek/Cyrillic characters retained; no blanket homoglyph folding was applied.') | |
| return {'text':analysis,'sentences':sentences,'excluded':excluded,'scope':scope, | |
| 'original_characters':len(text),'normalized_characters':len(normalized),'analyzed_characters':len(analysis), | |
| 'words':len(analysis.split()),'normalization_changes':changes,'warnings':warnings, | |
| 'normalization':'NFKC per base/combining cluster, remove selected invisibles, collapse horizontal whitespace; retain paragraph breaks.'} | |