Repository: wagtail/wagtail
File: client/src/includes/contentMetrics.ts (TypeScript)

Code before:
  sv: 218, // Swedish
};

export const getReadingTime = (lang: string, wordCount: number): number => {
  const locale = lang.split('-')[0];
  // Fallback to English reading speed if the locale is not found
  const readingSpeed = readingSpeeds[locale] || readingSpeeds.en;
  const readingTime = Math.round(wordCount / readingSpeed);

  return readingTime;
};

export const getReadabilityScore = (lang: string, text: string): string => {
  const segmenter = new Intl.Segmenter(lang, { granularity: 'sentence' });
  const sentences: Intl.SegmentData[] = Array.from(segmenter.segment(text));
  const sentenceCount = sentences.length;

  const wordSegmenter = new Intl.Segmenter(lang, { granularity: 'word' });
  const words: Intl.SegmentData[] = Array.from(wordSegmenter.segment(text));
  const wordCount = words.reduce(
    (count, segment) => (segment.isWordLike ? count + 1 : count),
    0,
  );
  // Count long words (6+ characters)
  const longWordCount = words.reduce((count, segment) => {
    if (segment.isWordLike && segment.segment.length > 6) {
      return count + 1;
    }
    return count;
  }, 0);

  // If no sentences found, return "Poor"
  if (sentenceCount === 0 || wordCount === 0) {
    return gettext('Good');
  }

  // LIX formula: (A / B) + (C * 100 / A)
  // A = number of words, B = number of sentences, C = number of long words
  const avgWordsPerSentence = wordCount / sentenceCount;
  const percentLongWords = (longWordCount / wordCount) * 100;
  const lixScore = avgWordsPerSentence + percentLongWords;

  // Convert numerical score to translated label
  if (lixScore < 30) {
    return gettext('Good');
  } else if (lixScore < 40) {
    return gettext('Fair');
  }

  return gettext('Poor');
};

Review comment (suggestion):
```suggestion
    if (segment.isWordLike && segment.segment.length >= 7) {
```

Code after:
  sv: 218, // Swedish
};

export const getReadingTime = (lang: string, wordCount: number): number => {
  const locale = lang.split('-')[0];
  // Fallback to English reading speed if the locale is not found
  const readingSpeed = readingSpeeds[locale] || readingSpeeds.en;
  const readingTime = Math.round(wordCount / readingSpeed);

  return readingTime;
};

/**
 * Calculate LIX (läsbarhetsindex) score for a given text.
 * LIX = (Number of Words / Number of Sentences) + (Number of Long Words * 100 / Number of Words)
 * Long words = more than 6 letters.
 */
export const getLIXScore = (lang: string, text: string): number => {
  const segmenter = new Intl.Segmenter(lang, { granularity: 'sentence' });
  const sentences = Array.from(segmenter.segment(text)).length;

  const wordSegmenter = new Intl.Segmenter(lang, { granularity: 'word' });
  let words = 0;
  let longWords = 0;
  for (const { segment, isWordLike } of wordSegmenter.segment(text)) {
    if (isWordLike) {
      words += 1;
      if (segment.length > 6) {
        longWords += 1;
      }
    }
  }

  // If 0 or too little content is found, return 0.
  // Readability scoring is not relevant on very short content.
  if (sentences === 0 || words < 10) {
    return 0;
  }

  const avgWordsPerSentence = words / sentences;
  const percentLongWords = (longWords / words) * 100;

  return avgWordsPerSentence + percentLongWords;
};

/**
 * Lenient interpretation of standard LIX score ranges.
 */
export const getReadabilityScore = (lixScore: number): string => {
  if (lixScore < 35) {
    return gettext('Good');