<commit_before>import SpellChecker from 'spellchecker';
import path from 'path';
import fs from 'fs';

import platform from './platform';
import languageCodes from './language-codes';
import files from './files';

let hunspellDictionaries = null;

export function getDictionaryPath() {
  let dict = path.join(__dirname, '..', 'node_modules', 'spellchecker', 'vendor', 'hunspell_dictionaries');
  try {
    if (platform.isLinux) {
      let usrDict = path.join('/usr', 'share', 'hunspell');
      if (fs.statSyncNoException(usrDict)) {
        dict = usrDict;
      }
    }

    // HACK: Special case being in an asar archive
    const unpacked = dict.replace('.asar' + path.sep, '.asar.unpacked' + path.sep);
    if (fs.statSyncNoException(unpacked)) {
      dict = unpacked;
    }
  } catch (ex) {
    // ignore
  }
  return dict;
}

export function getAvailableDictionaries() {
  let availableDictionaries = SpellChecker.getAvailableDictionaries();
  if (availableDictionaries.length) {
    return availableDictionaries;
  }

  if (!hunspellDictionaries) {
    try {
      hunspellDictionaries = files.getDictionariesSync(getDictionaryPath());
    } catch (err) {
      logError(err);
    }
  }
<commit_msg>Use default dictionaries if hunspell is not installed (Linux)<commit_after>import SpellChecker from 'spellchecker';
import path from 'path';
import fs from 'fs';

import platform from './platform';
import languageCodes from './language-codes';
import files from './files';

let hunspellDictionaries = null;

export function getDictionaryDefaultPath() {
  let dict = path.join(__dirname, '..', 'node_modules', 'spellchecker', 'vendor', 'hunspell_dictionaries');
  try {
    // HACK: Special case being in an asar archive
    const unpacked = dict.replace('.asar' + path.sep, '.asar.unpacked' + path.sep);
    if (fs.statSyncNoException(unpacked)) {
      dict = unpacked;
    }
  } catch (ex) {
    // ignore
  }
  return dict;
}

export function getDictionaryPath() {
  let dict = path.join(__dirname, '..', 'node_modules', 'spellchecker', 'vendor', 'hunspell_dictionaries');
  try {
    if (platform.isLinux) {
      let usrDict = path.join('/usr', 'share', 'hunspell');
      if (fs.statSyncNoException(usrDict)) {
        dict = usrDict;
      }
    }

    // HACK: Special case being in an asar archive
    const unpacked = dict.replace('.asar' + path.sep, '.asar.unpacked' + path.sep);
    if (fs.statSyncNoException(unpacked)) {
      dict = unpacked;
    }
  } catch (ex) {
    // ignore
  }
  return dict;
}

export function getAvailableDictionaries() {
  let availableDictionaries = SpellChecker.getAvailableDictionaries();
  if (availableDictionaries.length) {
    return availableDictionaries;
  }

  const dictionariesPath = getDictionaryPath();
  if (!hunspellDictionaries) {
    try {
      hunspellDictionaries = files.getDictionariesSync(dictionariesPath);
    } catch (err) {
      logError(err);
    }
  }

  if (platform.isLinux && (!hunspellDictionaries || hunspellDictionaries.length == 0)) {
    const dictionariesDefaultPath = getDictionaryDefaultPath();
    if (dictionariesDefaultPath != dictionariesPath) {
      try {
        hunspellDictionaries = files.getDictionariesSync(getDictionaryDefaultPath());
      } catch (err) {
        logError(err);
      }
    }
  }

{'fraction_non_alphanumeric': 0.12806539509536785, 'fraction_numerical': 0.0003027550711474417, 'mean_word_length': 3.5075034106412004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1447232', 'n_tokens_mistral': 1036, 'n_tokens_neox': 1027, 'n_words': 242}