"""Aligns corpora based on the English text using Murmurhash3""" import mmh3 import numpy as np import polars as pl with open("raw/europarl-v7.da-en.en") as in_file: texts = list(in_file) eng_da = pl.DataFrame(dict(text=texts, da_id=np.arange(len(texts)))) eng_da = eng_da.select( pl.col("da_id"), pl.col("text").map_elements(mmh3.hash64).alias("hash") ) eng_da = eng_da.select( pl.col("da_id"), # This is necessary as Polars doesn't support 128 bit integers. pl.col("hash").list.first().alias("hash1"), pl.col("hash").list.last().alias("hash2"), ) with open("raw/europarl-v7.sv-en.en") as in_file: texts = list(in_file) eng_sv = pl.DataFrame(dict(text=texts, sv_id=np.arange(len(texts)))) eng_sv = eng_sv.select( pl.col("sv_id"), pl.col("text").map_elements(mmh3.hash64).alias("hash") ) eng_sv = eng_sv.select( pl.col("sv_id"), # This is necessary as Polars doesn't support 128 bit integers. pl.col("hash").list.first().alias("hash1"), pl.col("hash").list.last().alias("hash2"), ) alignment = eng_sv.join(eng_da, on=["hash1", "hash2"], how="inner") alignment = alignment.select(pl.col("da_id", "sv_id")) alignment.write_parquet("alignment.parquet")