1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::{
    Dictionary,
    Error::{DictionaryNotFound, SolitaireNotFound},
    Idiom, Result,
};
use pathfinding::directed::bfs::bfs;
use pinyin::ToPinyin;
use rand::{rngs::SmallRng, seq::IteratorRandom, SeedableRng};
use std::{
    collections::HashMap,
    fmt::{self, Debug, Formatter},
};

#[derive(Clone, Debug)]
pub enum SolitaireMode {
    /// 同字模式, 需要字符完全相同, 允许多音字
    Character = 0,
    /// 同调模式, 需要发音以及音调相同
    Tone = 1,
    /// 同音模式, 允许音调不同
    Sound = 2,
}

#[derive(Clone)]
pub struct SolitaireSolver {
    pub dict: Dictionary,
    pub state: HashMap<String, Vec<Idiom>>,
    pub mode: SolitaireMode,
    pub rng: SmallRng,
}

impl Default for SolitaireSolver {
    fn default() -> Self {
        Self { dict: Default::default(), state: Default::default(), mode: SolitaireMode::Tone, rng: SmallRng::from_entropy() }
    }
}

impl Debug for SolitaireSolver {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self { dict, state, mode, rng: _ } => f
                .debug_struct("SolitaireSolver")
                .field("dict", &dict.0.len())
                .field("state", &state.len())
                .field("mode", mode)
                .finish(),
        }
    }
}

impl SolitaireSolver {
    pub fn length(&self) -> usize {
        self.dict.0.len()
    }
    fn get_key(&self, input: &str) -> String {
        let last = input.chars().rev().next().unwrap();
        let key = match self.mode {
            SolitaireMode::Character => last.to_string(),
            SolitaireMode::Tone => last.to_pinyin().unwrap().with_tone().to_string(),
            SolitaireMode::Sound => last.to_pinyin().unwrap().plain().to_string(),
        };
        return key;
    }
    fn successors(&self, state: &Idiom) -> Vec<Idiom> {
        let key = self.get_key(&state.idiom);
        match self.state.get(&key) {
            None => vec![],
            Some(s) => s.to_owned(),
        }
    }
}

impl SolitaireSolver {
    pub fn load(&mut self, bytes: &[u8]) -> Result<()> {
        self.dict = Dictionary::load_bytes(bytes)?;
        Ok(self.refresh()?)
    }
    pub fn refresh(&mut self) -> Result<()> {
        if self.length() == 0 {
            return Err(DictionaryNotFound);
        }
        self.rng = SmallRng::from_entropy();
        match self.mode {
            SolitaireMode::Character => self.state = self.dict.char_map(),
            SolitaireMode::Sound => self.state = self.dict.sound_map(),
            SolitaireMode::Tone => self.state = self.dict.tone_map(),
        }
        Ok(())
    }

    pub fn solve_random(&mut self, input: &str) -> Result<Idiom> {
        if let Some(s) = self.state.get_mut(&self.get_key(input)) {
            if s.is_empty() {
                return Err(SolitaireNotFound);
            }
            let i = (0..s.len()).choose(&mut self.rng)?;
            return Ok(s.swap_remove(i));
        }
        Err(SolitaireNotFound)
    }

    pub fn solve_target(&mut self, input: &str, target: &str) -> Result<Vec<Idiom>> {
        let first = Idiom::from(input);
        let target = target.chars().next()?;
        let target = match self.mode {
            SolitaireMode::Character => target.to_string(),
            SolitaireMode::Tone => target.to_pinyin()?.with_tone().to_string(),
            SolitaireMode::Sound => target.to_pinyin()?.plain().to_string(),
        };
        let result = bfs(&first, |p| self.successors(p), |p| self.get_key(&p.idiom) == target)?;
        Ok(result)
    }

    pub fn solve_greedy(&mut self, input: &str) -> Result<Idiom> {
        let s = match self.state.get(&self.get_key(input)) {
            Some(s) => s,
            None => return Err(SolitaireNotFound),
        };
        if s.is_empty() {
            return Err(SolitaireNotFound);
        }
        let mut max = 0;
        for i in 0..s.len() {
            let this = unsafe { s.get_unchecked(i) };
            let len = match self.state.get(&self.get_key(&this.idiom)) {
                Some(v) => v.len(),
                None => 0,
            };
            if len > max {
                max = i
            }
        }
        let s = self.state.get_mut(&self.get_key(input))?;
        return Ok(s.swap_remove(max));
    }
    pub fn solve_chain(&mut self, head: &str, length: usize) -> Vec<Idiom> {
        let mut out = vec![];
        let mut this = String::from(head);
        for _ in 0..length {
            match self.solve_greedy(&this) {
                Ok(o) => {
                    this = o.idiom.clone();
                    out.push(o)
                }
                Err(_) => {
                    break;
                }
            }
        }
        return out;
    }
}