epsilon-compositionality

Code for our paper Measuring Idiomaticity in Text Embedding Models with 𝜀-compositionality
git clone https://esimon.eu/repos/epsilon-compositionality.git
Log | Files | Refs | Submodules | README

commit d7b80318340fa4315f6c693ffd592ebae8e6fdee
parent e6d15a7c6d4173253ae34d4ac3c99297eb7a796a
Author: Sondre Wold <sondrewold1@gmail.com>
Date:   Mon, 21 Jul 2025 16:20:49 +0200

update experiment class

Diffstat:
Mexperiment.py | 29++++++++++++++++++++++++++---
1 file changed, 26 insertions(+), 3 deletions(-)

diff --git a/experiment.py b/experiment.py @@ -26,7 +26,7 @@ def parse_args() -> argparse.Namespace: parser.add_argument("-data_path", type=pathlib.Path) parser.add_argument("-result_path", type=pathlib.Path) parser.add_argument("-normalization_value", type=float) - parser.add_argument("-experiment_type", type=str, choices=["sick", "ncip"]) + parser.add_argument("-experiment_type", type=str, choices=["sick", "ncip", "ncip-non"]) parser.add_argument("-model", type=str, choices=["BERT_cls", "BERT_mean", "S_BERT", "S_GPT", "E5", "QWEN"]) return parser.parse_args() @@ -50,6 +50,16 @@ class SentencePairs(NamedTuple): sentence_B: list[str] +def normalize_v2(): + """ + For a context c and a substring x (to be replaced), replace x with n candidates, embed them, calculate the mean the std.dev. + Subtract the mean and divide by the std.dev (gaussian assumption) (mu(ac) - (mean(c with x)) / std.dev + + Sample x from a list of random words. + """ + pass + + class Model(ABC): @abstractmethod @@ -208,9 +218,14 @@ class E5(Model): def __init__(self): self.encoder = SentenceTransformer("intfloat/multilingual-e5-large-instruct", device=DEVICE) + self.task = "Given a sentence, encode its semantic properties" + + def get_detailed_instruct(self, query: str) -> str: + return f'Instruct: {self.task}\nQuery: {query}' def embed(self, string: str) -> torch.Tensor: - return self.encoder.encode([string]) + prepared_string = self.get_detailed_instruct(string) + return self.encoder.encode([prepared_string]) def calculate_similarity(self, a: torch.Tensor, b: torch.Tensor) -> float: return self.encoder.similarity(a, b) @@ -373,7 +388,7 @@ class Experiment: def main() -> None: args = parse_args() sep = "\t" - if args.experiment_type == "ncip": + if args.experiment_type in ["ncip", "ncipv2"]: sep = "," df: pandas.DataFrame = pandas.read_csv(args.data_path, sep=sep) @@ -389,6 +404,14 @@ def main() -> None: sentence_B: list[str] = ( synonym_sentence + synonym_sentence2 + synonym_sentence3 + synonym_sentence4 ) + elif args.experiment_type == "ncip-non": + A: list[str] = df["synonym both"].values.tolist() + B: list[str]= df["both synonym alt2_1"].values.tolist() + C: list[str] = df["both synonym alt3_1"].values.tolist() + D: list[str] = df["both synonym alt4_1"].values.tolist() + + sentence_A = A + B + C + D + sentence_B = B + C + D + A elif args.experiment_type == "sick": lexA = df.loc[df["sentence_A_expRule"] == "S2_lex"] lexB = df.loc[df["sentence_B_expRule"] == "S2_lex"]