<commit_before>import { Injectable } from '@angular/core';
import { RadioButton, RadioButtonFactory } from '../components/setting/radiobutton/radiobutton';
import * as fs from 'fs';

let settingPath = '';
export function setSettingPath(path: string) {
    settingPath = path;
}

interface Setting {
    tokens: string[];
    hideButtons: boolean;
    imageExpansionSize: RadioButton;
}

@Injectable()
export class SettingService {
    setting: Setting;

    get tokens(): string[] {
        return this.setting.tokens;
    }

    set tokens(tokens: string[]) {
        this.setting.tokens = tokens;
    }

    get hideButtons(): boolean {
        return this.setting.hideButtons;
    }

    set hideButtons(hideButtons: boolean) {
        this.setting.hideButtons = hideButtons;
    }

    get imageExpansionSize(): RadioButton {
        return this.setting.imageExpansionSize;
    }

    set imageExpansionSize(imageExpansionSize: RadioButton) {
        this.setting.imageExpansionSize = imageExpansionSize;
    }

    constructor() {
        try {
            this.setting = JSON.parse(fs.readFileSync(settingPath, 'utf8'));
        } catch (e) {
            this.setting = {} as Setting;
        }

        if (this.tokens === undefined) { this.tokens = ['']; }
        if (this.hideButtons === undefined) { this.hideButtons = false; }
        if (this.imageExpansionSize === undefined) {
            this.imageExpansionSize = RadioButtonFactory.get('Image Expansion', ['Normal', 'Small', 'Never'], 'Normal');
        }
    }

    save() {
        fs.writeFileSync(settingPath, JSON.stringify(this.setting));
    }
}<commit_msg>fix critical bug: crashes when the setting is empty<commit_after>import { Injectable } from '@angular/core';
import { RadioButton, RadioButtonFactory } from '../components/setting/radiobutton/radiobutton';
import * as fs from 'fs';

let settingPath = '';
export function setSettingPath(path: string) {
    settingPath = path;
}

interface Setting {
    tokens: string[];
    hideButtons: boolean;
    imageExpansionSize: RadioButton;
}

@Injectable()
export class SettingService {
    setting: Setting;

    get tokens(): string[] {
        return this.setting.tokens;
    }

    set tokens(tokens: string[]) {
        this.setting.tokens = tokens;
    }

    get hideButtons(): boolean {
        return this.setting.hideButtons;
    }

    set hideButtons(hideButtons: boolean) {
        this.setting.hideButtons = hideButtons;
    }

    get imageExpansionSize(): RadioButton {
        return this.setting.imageExpansionSize;
    }

    set imageExpansionSize(imageExpansionSize: RadioButton) {
        this.setting.imageExpansionSize = imageExpansionSize;
    }

    constructor() {
        try {
            this.setting = JSON.parse(fs.readFileSync(settingPath, 'utf8'));
        } catch (e) {
            this.setting = {} as Setting;
        }

        if (this.tokens === undefined) { this.tokens = []; }
        if (this.hideButtons === undefined) { this.hideButtons = false; }
        if (this.imageExpansionSize === undefined) {
            this.imageExpansionSize = RadioButtonFactory.get('Image Expansion', ['Normal', 'Small', 'Never'], 'Normal');
        }
    }

    save() {
        fs.writeFileSync(settingPath, JSON.stringify(this.setting));
    }
}
{'fraction_non_alphanumeric': 0.12401455427531838, 'fraction_numerical': 0.0006064281382656155, 'mean_word_length': 2.8227114716106603, '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': '6936995', 'n_tokens_mistral': 938, 'n_tokens_neox': 922, 'n_words': 204}