Spaces:
Running
Running
Download toolkit/research/features.py from Giuliocc/tacitus-source-lab: direct link, hf CLI and curl.
- Browser
- Download file 3.76 kB
-
https://huggingface.co/spaces/Giuliocc/tacitus-source-lab/resolve/main/toolkit/research/features.py
- Command line
-
hf download hf://spaces/Giuliocc/tacitus-source-lab/toolkit/research/features.py
-
curl -L -o features.py https://huggingface.co/spaces/Giuliocc/tacitus-source-lab/resolve/main/toolkit/research/features.py
3.76 kB
| """Features shared with the browser runtime through parity fixtures.""" | |
| import math | |
| import zlib | |
| import regex | |
| WIDTH = 32768 | |
| PATTERN = regex.compile(r"[\p{L}\p{M}]+(?:['’][\p{L}\p{M}]+)*|\p{N}+(?:[.,]\p{N}+)*|[^\s]") | |
| NUMBER = regex.compile(r"^\p{N}+(?:[.,]\p{N}+)*$") | |
| NEGATION = frozenset(("no", "not", "never", "neither", "without", "cannot", "denied", "deny")) | |
| MODAL = frozenset(("will", "would", "might", "could", "plans", "planned", "scheduled", "proposed")) | |
| def tokenize(text): | |
| return [(m.group(), m.start(), m.end()) for m in PATTERN.finditer(text)] | |
| def evidence_features(text, claim, mode): | |
| passage = [word.lower() for word, *_ in tokenize(text)] | |
| hypothesis = [word.lower() for word, *_ in tokenize(claim)] | |
| terms, requested = set(passage), set(hypothesis) | |
| features = [(f"h:{word}", .25) for word in sorted(requested)] | |
| if mode == "hypothesis": | |
| return features | |
| overlap = terms & requested | |
| span = set(zip(passage, passage[1:])) | |
| pair = set(zip(hypothesis, hypothesis[1:])) | |
| numeric = {word for word in requested if NUMBER.fullmatch(word)} | |
| numeric_source = {word for word in terms if NUMBER.fullmatch(word)} | |
| ratios = {"recall": len(overlap) / max(1, len(requested)), | |
| "bigram": len(span & pair) / max(1, len(pair)), | |
| "numbers": len(numeric & numeric_source) / max(1, len(numeric))} | |
| for name, value in ratios.items(): | |
| features += [(f"val:{name}", value), (f"bin:{name}:{math.floor(value * 10)}", 1.)] | |
| for name, words in (("neg", NEGATION), ("modal", MODAL)): | |
| features.append((f"{name}:{int(bool(words & terms))}:{int(bool(words & requested))}", 1.)) | |
| features += [(f"{'match' if word in terms else 'missing'}:{word}", .5) for word in sorted(requested)] | |
| features += [(f"numbers-exact:{int(bool(numeric))}:{int(numeric <= numeric_source)}", 1.)] | |
| features += [(f"length:{min(len(passage) // 25, 12)}:{min(len(hypothesis) // 5, 10)}", 1.)] | |
| return features | |
| def token_features(tokens, i, mode, anchor=None, event_type=None): | |
| word = tokens[i][0] | |
| lower = word.lower() | |
| values = [(f"w:{lower}", 1.)] | |
| if mode == "lexical": | |
| return values | |
| shape = "upper" if word.upper() == word and word.lower() != word else ( | |
| "title" if word[0].upper() == word[0] and word[0].lower() != word[0] else | |
| "number" if NUMBER.fullmatch(word) else "other") | |
| values.extend(((f"shape:{shape}", 1.), (f"length:{min(len(word), 12)}", .5))) | |
| for length in (2, 3, 4): | |
| if len(lower) >= length: | |
| values.extend(((f"pre:{lower[:length]}", .5), (f"suf:{lower[-length:]}", .5))) | |
| for offset in (-2, -1, 1, 2): | |
| index = i + offset | |
| other = tokens[index][0].lower() if 0 <= index < len(tokens) else ("<start>" if index < 0 else "<end>") | |
| values.append((f"ctx:{offset}:{other}", .65)) | |
| if abs(offset) == 1: | |
| values.append((f"pair:{offset}:{lower}:{other}", .5)) | |
| if anchor is not None: | |
| distance = i - anchor | |
| bucket = min(abs(distance), 12) if abs(distance) <= 12 else 13 + min(abs(distance) // 20, 5) | |
| direction = -1 if distance < 0 else 1 | |
| values += [(f"event:{event_type}", 1.), (f"dist:{bucket * direction}", 1.), | |
| (f"role:{event_type}:{bucket * direction}", 1.), | |
| (f"anchor:{tokens[anchor][0].lower()}:{bucket * direction}", .65), | |
| (f"eventword:{event_type}:{lower}", .8)] | |
| return values | |
| def encode(features): | |
| values = {} | |
| for name, value in features: | |
| feature_hash = zlib.crc32(name.encode("utf-8")) | |
| column = feature_hash % WIDTH | |
| values[column] = values.get(column, 0.) + (-value if feature_hash & 0x80000000 else value) | |
| return values | |