ws-segment-rs 0.1.1

Chinese word segmentation for web novels (Rust port of novel-segment)
Documentation
//! Chinese surname / given-name tables (`CHS_NAMES`).

use once_cell::sync::Lazy;
use std::collections::HashSet;

const FAMILY_NAME_1_RAW: &[&str] = &[
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "羿", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "",
];

const FAMILY_NAME_2_RAW: &[&str] = &[
    "司马", "上官", "欧阳", "夏侯", "诸葛", "闻人", "东方", "赫连", "皇甫", "尉迟", "公羊",
    "澹台", "公冶", "宗政", "濮阳", "淳于", "单于", "太叔", "申屠", "公孙", "仲孙", "轩辕",
    "令狐", "徐离", "宇文", "长孙", "慕容", "司徒", "司空", "万俟",
];

const DOUBLE_NAME_1_RAW: &[&str] = &[
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "广", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "耀", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "鸿", "", "", "", "", "", "", "", "", "", "", "怀",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "",
];

const DOUBLE_NAME_2_RAW: &[&str] = &[
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "鸿",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "",
];

const SINGLE_NAME_RAW: &[&str] = &[
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
    "", "", "", "",
];

const SINGLE_NAME_NO_REPEAT_RAW: &[&str] = &[""];

fn expand_set(raw: &[&str]) -> HashSet<String> {
    #[cfg(feature = "default-dict")]
    {
        novel_segment_dict::arr_cjk(raw.iter().copied()).into_iter().collect()
    }
    #[cfg(not(feature = "default-dict"))]
    {
        raw.iter().map(|s| (*s).to_string()).collect()
    }
}

pub static FAMILY_NAME_1: Lazy<HashSet<String>> = Lazy::new(|| {
    let mut s = expand_set(FAMILY_NAME_1_RAW);
    s.remove("");
    s
});

pub static FAMILY_NAME_2: Lazy<HashSet<String>> = Lazy::new(|| expand_set(FAMILY_NAME_2_RAW));
pub static DOUBLE_NAME_1: Lazy<HashSet<String>> = Lazy::new(|| expand_set(DOUBLE_NAME_1_RAW));
pub static DOUBLE_NAME_2: Lazy<HashSet<String>> = Lazy::new(|| expand_set(DOUBLE_NAME_2_RAW));
pub static SINGLE_NAME: Lazy<HashSet<String>> = Lazy::new(|| expand_set(SINGLE_NAME_RAW));
pub static SINGLE_NAME_NO_REPEAT: Lazy<HashSet<String>> =
    Lazy::new(|| expand_set(SINGLE_NAME_NO_REPEAT_RAW));

pub fn is_family_name(w: &str) -> bool {
    FAMILY_NAME_1.contains(w) || FAMILY_NAME_2.contains(w)
}