<commit_before>import { createSelector } from 'reselect';
import isEmpty from 'lodash/isEmpty';
import sumBy from 'lodash/sumBy';
import { sortByKey } from 'utils/data';
import { format } from 'd3-format';
import endsWith from 'lodash/endsWith';

// get list data
const getData = state => state.data;
const getSettings = state => state.settings;
const getLocationNames = state => state.locationNames;
const getIndicatorWhitelist = state => state.whitelist;
const getColors = state => state.colors;

// get lists selected
export const getTreeCoverPlantationsData = createSelector(
  [getData, getSettings, getIndicatorWhitelist, getColors],
  (data, settings, whitelist, colors) => {
    if (isEmpty(data) || isEmpty(whitelist)) return null;
    const { plantations } = data;
    const totalPlantations = sumBy(plantations, 'plantation_extent');

    return sortByKey(
      plantations.filter(d => d.plantation_extent).map(d => ({
        label: d.label,
        value: d.plantation_extent,
        color: colors[d.label],
        percentage: d.plantation_extent / totalPlantations * 100
      })),
      'value',
      true
    );
  }
);

export const getSentence = createSelector(
  [getTreeCoverPlantationsData, getSettings, getLocationNames],
  (data, settings, locationNames) => {
    if (isEmpty(data)) return null;
    const locationLabel = locationNames.current && locationNames.current.label;
    let topTypesSentence = '';
    let top = null;
    let remainSentence = '';
    if (settings.type === 'bound2') {
      top = data.slice(0, 2);
      if (top.length === 1) {
        topTypesSentence = `<b>${
          top[0].label
        }</b> represent the largest plantation by <b>species</b>`;
      } else {
        topTypesSentence = `<b>${top[0].label}</b> and <b>${
          top[1].label
        }</b> represent the largest plantations by <b>species</b>`;
      }

      remainSentence = `The remaining <b>${format('.2s')(
        sumBy(data.slice(2), 'value')
      )}ha</b> of tree cover is distributed between <b>${data.length -
        top.length}</b> other plantation species.`;
    } else {
      top = data.slice(0, 1);
      topTypesSentence = `the largest plantation type by area are <b>${
        top[0].label
      }${endsWith(top[0].label, 's') ? '' : 's'}</b>`;
    }

    return `In <b>${locationLabel}</b>, ${topTypesSentence}, spanning <b>${format(
      '.2s'
    )(sumBy(top, 'value'))}ha</b>. ${remainSentence}`;<commit_msg>only show remaining plantations sentence when greater than 0<commit_after>import { createSelector } from 'reselect';
import isEmpty from 'lodash/isEmpty';
import sumBy from 'lodash/sumBy';
import { sortByKey } from 'utils/data';
import { format } from 'd3-format';
import endsWith from 'lodash/endsWith';

// get list data
const getData = state => state.data;
const getSettings = state => state.settings;
const getLocationNames = state => state.locationNames;
const getIndicatorWhitelist = state => state.whitelist;
const getColors = state => state.colors;

// get lists selected
export const getTreeCoverPlantationsData = createSelector(
  [getData, getSettings, getIndicatorWhitelist, getColors],
  (data, settings, whitelist, colors) => {
    if (isEmpty(data) || isEmpty(whitelist)) return null;
    const { plantations } = data;
    const totalPlantations = sumBy(plantations, 'plantation_extent');

    return sortByKey(
      plantations.filter(d => d.plantation_extent).map(d => ({
        label: d.label,
        value: d.plantation_extent,
        color: colors[d.label],
        percentage: d.plantation_extent / totalPlantations * 100
      })),
      'value',
      true
    );
  }
);

export const getSentence = createSelector(
  [getTreeCoverPlantationsData, getSettings, getLocationNames],
  (data, settings, locationNames) => {
    if (isEmpty(data)) return null;
    const locationLabel = locationNames.current && locationNames.current.label;
    let topTypesSentence = '';
    let top = null;
    let remainSentence = '';
    if (settings.type === 'bound2') {
      top = data.slice(0, 2);
      if (top.length === 1) {
        topTypesSentence = `<b>${
          top[0].label
        }</b> represent the largest plantation by <b>species</b>`;
      } else {
        topTypesSentence = `<b>${top[0].label}</b> and <b>${
          top[1].label
        }</b> represent the largest plantations by <b>species</b>`;
      }

      remainSentence =
        data.length > top.length
          ? `The remaining <b>${format('.2s')(
            sumBy(data.slice(2), 'value')
          )}ha</b> of tree cover is distributed between <b>${data.length -
              top.length}</b> other plantation species.`
          : '';
    } else {
      top = data.slice(0, 1);
      topTypesSentence = `the largest plantation type by area are <b>${
        top[0].label
      }${endsWith(top[0].label, 's') ? '' : 's'}</b>`;
    }

    return `In <b>${locationLabel}</b>, ${topTypesSentence}, spanning <b>${format(
      '.2s'
    )(sumBy(top, 'value'))}ha</b>. ${remainSentence}`;
{'fraction_non_alphanumeric': 0.14790549930514196, 'fraction_numerical': 0.007345642247369466, 'mean_word_length': 3.1601981833195705, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 62, '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': '7666437', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1559, 'n_words': 393}