<commit_before>import * as utils from './utils';

export function rougeN(cand, ref, opts = {
  n: 1,
  nGram: utils.nGram,
  tokenizer: utils.treeBankTokenize,
}) {
  const candGrams = opts.nGram(opts.tokenizer(cand), n);
  const refGrams = opts.nGram(opts.tokenizer(ref), n);

  const match = utils.intersection(candGrams, refGrams);
  return match.length / candGrams.length;
}

export function rougeS(cand, ref, opts = {
  beta: 0.5,
  skipBigram: utils.skipBigram,
  tokenizer: utils.treeBankTokenize,
}) {
  const candGrams = opts.skipBigram(opts.tokenizer(cand));
  const refGrams = opts.skipBigram(opts.tokenizer(ref));

  const skip2 = utils.intersection(candGrams, refGrams);
  const skip2Recall = skip2 / utils.comb2(ref.length);
  const skip2Prec = skip2 / utils.comb2(cand.length);

  return utils.fMeasure(skip2Prec, skip2Recall, opts.beta);
}

export function rougeL(cand, ref, opts = {
  beta: 0.5,
  lcs: utils.lcs,
  segmenter: utils.sentenceSegment,
  tokenizer: utils.treeBankTokenize,
}) {
  const candSents = opts.segmenter(cand);
  const refSents = opts.segmenter(ref);

  const candWords = opts.tokenizer(cand);
  const refWords = opts.tokenizer(ref);

  const lcsAcc = refSents.map(r => {
    const rTokens = opts.tokenizer(r);

    const lcsUnion = new Set(candSents.map(c => {
      const cTokens = opts.tokenizer(c);
      return opts.lcs(cTokens, rTokens);
    }));

    return lcsUnion.size;
  });

  const lcsSum = lcsAcc.reduce((x, y) => x + y);
  const lcsRecall = lcsSum / candWords;
  const lcsPrec = lcsSum / refWords;

  return utils.fMeasure(lcsPrec, lcsRecall, opts.beta);
}
<commit_msg>Fix error in LCS calculation<commit_after>import * as utils from './utils';

export function rougeN(cand, ref, opts = {
  n: 1,
  nGram: utils.nGram,
  tokenizer: utils.treeBankTokenize,
}) {
  const candGrams = opts.nGram(opts.tokenizer(cand), n);
  const refGrams = opts.nGram(opts.tokenizer(ref), n);

  const match = utils.intersection(candGrams, refGrams);
  return match.length / candGrams.length;
}

export function rougeS(cand, ref, opts = {
  beta: 0.5,
  skipBigram: utils.skipBigram,
  tokenizer: utils.treeBankTokenize,
}) {
  const candGrams = opts.skipBigram(opts.tokenizer(cand));
  const refGrams = opts.skipBigram(opts.tokenizer(ref));

  const skip2 = utils.intersection(candGrams, refGrams);
  const skip2Recall = skip2 / utils.comb2(ref.length);
  const skip2Prec = skip2 / utils.comb2(cand.length);

  return utils.fMeasure(skip2Prec, skip2Recall, opts.beta);
}

export function rougeL(cand, ref, opts = {
  beta: 0.5,
  lcs: utils.lcs,
  segmenter: utils.sentenceSegment,
  tokenizer: utils.treeBankTokenize,
}) {
  const candSents = opts.segmenter(cand);
  const refSents = opts.segmenter(ref);

  const candWords = opts.tokenizer(cand);
  const refWords = opts.tokenizer(ref);

  const lcsAcc = refSents.map(r => {
    const rTokens = opts.tokenizer(r);

    const lcsUnion = new Set(candSents.map(c => {
      const cTokens = opts.tokenizer(c);
      return opts.lcs(cTokens, rTokens);
    }));

    return lcsUnion.size;
  });

  const lcsSum = lcsAcc.reduce((x, y) => x + y);
  const lcsRecall = lcsSum / candWords.length;
  const lcsPrec = lcsSum / refWords.length;

  return utils.fMeasure(lcsPrec, lcsRecall, opts.beta);
}

{'fraction_non_alphanumeric': 0.1330485199877937, 'fraction_numerical': 0.008544400366188587, 'mean_word_length': 4.06646058732612, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2792161', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1239, 'n_words': 254}