<commit_before>import { queryFactory } from './category-topk.summarization';
import { getSummaries } from './utils/tests';
import { CategoricalPoint } from '../metas/types';

describe('queryFactory', () => {
  const hasHighValidity = ({ validity }) => validity >= 0.7;

  const points: CategoricalPoint[] = [
    { x: 'A', y: 100, },
    { x: 'B', y: 200, },
    { x: 'C', y: 300, },
    { x: 'D', y: 400, },
  ];

  it('should have validity value between 0 and 1.', () => {
    const summaries = getSummaries(queryFactory, points);
    for (const summary of summaries) {
      expect(summary.validity).toBeGreaterThanOrEqual(0.0);
      expect(summary.validity).toBeLessThanOrEqual(1.0);
    }
  });

  it('should describe top 3 categories by default.', () => {
    const summaries = getSummaries(queryFactory, points);
    const isTop1stSummary = ({ text }) => text.includes('weekday') && text.includes('linearly quickly increasing');
    const isTop2ndSummary = ({ text }) => text.includes('weekend') && text.includes('constant');
    const isTop3rdSummary = ({ text }) => text.includes('weekend') && text.includes('constant');

    const top1stSummary = summaries.filter(isTop1stSummary);
    const top2ndSummary = summaries.filter(isTop2ndSummary);
    const top3rdSummary = summaries.filter(isTop3rdSummary);

    expect(summaries.length).toBe(3);

    expect(top1stSummary.length).toBe(1);
    expect(top1stSummary.every(hasHighValidity)).toBeTrue();
    expect(top2ndSummary.length).toBe(1);
    expect(top2ndSummary.every(hasHighValidity)).toBeTrue();
    expect(top3rdSummary.length).toBe(1);
    expect(top3rdSummary.every(hasHighValidity)).toBeTrue();
  });
});
<commit_msg>Add tests for CategoryTopkSummarization<commit_after>import { queryFactory } from './category-topk.summarization';
import { getSummaries, hasHighValidity, isTextPartsInSummary } from './utils/tests';
import { CategoricalPoint } from '../metas/types';

describe('queryFactory', () => {

  const points: CategoricalPoint[] = [
    { x: 'A', y: 100, },
    { x: 'B', y: 200, },
    { x: 'C', y: 300, },
    { x: 'D', y: 400, },
  ];

  it('should have validity value between 0 and 1.', () => {
    const summaries = getSummaries(queryFactory, points);
    for (const summary of summaries) {
      expect(summary.validity).toBeGreaterThanOrEqual(0.0);
      expect(summary.validity).toBeLessThanOrEqual(1.0);
    }
  });

  it('should describe top 3 categories by default.', () => {
    const summaries = getSummaries(queryFactory, points);
    const isTop1stSummary = isTextPartsInSummary('D', '1st', 'sessions');
    const isTop2ndSummary = isTextPartsInSummary('C', '2nd', 'sessions');
    const isTop3rdSummary = isTextPartsInSummary('B', '3rd', 'sessions');

    const top1stSummary = summaries.filter(isTop1stSummary);
    const top2ndSummary = summaries.filter(isTop2ndSummary);
    const top3rdSummary = summaries.filter(isTop3rdSummary);

    expect(summaries.length).toBe(3);

    expect(top1stSummary.length).toBe(1);
    expect(top1stSummary.every(hasHighValidity)).toBeTrue();
    expect(top2ndSummary.length).toBe(1);
    expect(top2ndSummary.every(hasHighValidity)).toBeTrue();
    expect(top3rdSummary.length).toBe(1);
    expect(top3rdSummary.every(hasHighValidity)).toBeTrue();
  });

  it('should describe top 4 categories by providing config topk.', () => {
    const summaries = getSummaries(pts => queryFactory(pts, { topk: 4 }), points);
    const isTop1stSummary = isTextPartsInSummary('D', '1st', 'sessions');
    const isTop2ndSummary = isTextPartsInSummary('C', '2nd', 'sessions');
    const isTop3rdSummary = isTextPartsInSummary('B', '3rd', 'sessions');
    const isTop4thSummary = isTextPartsInSummary('A', '4th', 'sessions');

    const top1stSummary = summaries.filter(isTop1stSummary);
    const top2ndSummary = summaries.filter(isTop2ndSummary);
    const top3rdSummary = summaries.filter(isTop3rdSummary);
    const top4thSummary = summaries.filter(isTop4thSummary);

    expect(summaries.length).toBe(4);

    expect(top1stSummary.length).toBe(1);
    expect(top1stSummary.every(hasHighValidity)).toBeTrue();
    expect(top2ndSummary.length).toBe(1);
    expect(top2ndSummary.every(hasHighValidity)).toBeTrue();
    expect(top3rdSummary.length).toBe(1);
    expect(top3rdSummary.every(hasHighValidity)).toBeTrue();
    expect(top4thSummary.length).toBe(1);
    expect(top4thSummary.every(hasHighValidity)).toBeTrue();
  });

  it('should describe top 3 categories with metric as countries by providing config metric.', () => {
    const metric = 'countries';
    const summaries = getSummaries(pts => queryFactory(pts, { metric }), points);
    const isTop1stSummary = isTextPartsInSummary('D', '1st', metric);
    const isTop2ndSummary = isTextPartsInSummary('C', '2nd', metric);
    const isTop3rdSummary = isTextPartsInSummary('B', '3rd', metric);

    const top1stSummary = summaries.filter(isTop1stSummary);
    const top2ndSummary = summaries.filter(isTop2ndSummary);
    const top3rdSummary = summaries.filter(isTop3rdSummary);

    expect(summaries.length).toBe(3);

    expect(top1stSummary.length).toBe(1);
    expect(top1stSummary.every(hasHighValidity)).toBeTrue();
    expect(top2ndSummary.length).toBe(1);
    expect(top2ndSummary.every(hasHighValidity)).toBeTrue();
    expect(top3rdSummary.length).toBe(1);
    expect(top3rdSummary.every(hasHighValidity)).toBeTrue();
  });

  it('should describe top 3 categories with percentage by providing config showPercentage.', () => {
    const summaries = getSummaries(pts => queryFactory(pts, { showPercentage: true }), points);
    const isTop1stSummary = isTextPartsInSummary('D', '1st', '%)');
    const isTop2ndSummary = isTextPartsInSummary('C', '2nd', '%)');
    const isTop3rdSummary = isTextPartsInSummary('B', '3rd', '%)');

    const top1stSummary = summaries.filter(isTop1stSummary);
    const top2ndSummary = summaries.filter(isTop2ndSummary);
    const top3rdSummary = summaries.filter(isTop3rdSummary);

    expect(summaries.length).toBe(3);

    expect(top1stSummary.length).toBe(1);
    expect(top1stSummary.every(hasHighValidity)).toBeTrue();
    expect(top2ndSummary.length).toBe(1);
    expect(top2ndSummary.every(hasHighValidity)).toBeTrue();
    expect(top3rdSummary.length).toBe(1);
    expect(top3rdSummary.every(hasHighValidity)).toBeTrue();
  });
});

{'fraction_non_alphanumeric': 0.14782341178280264, 'fraction_numerical': 0.02430395323796339, 'mean_word_length': 4.2947882736156355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '137549', 'n_tokens_mistral': 2115, 'n_tokens_neox': 2029, 'n_words': 354}