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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#![allow(dead_code)]

use fs::read_to_string;
use std::collections::HashSet;
use std::fs;

fn contains_str(vec: &Vec<String>, s: &String) -> bool {
    vec.iter().any(|x| x == s)
}

#[derive(Clone)]
pub struct Vextract {
    punc: String,
    plist: Vec<String>,
    alist: Vec<String>,
    voc: HashSet<String>,
    pub text: String,
    pub vocab: Vec<String>,
}

impl Vextract {
    pub fn new(fileurl: &str, al: Vec<&str>, pl: Vec<&str>) -> Vextract {
        let mut vset: HashSet<String> = HashSet::new();
        let ftext = read_to_string(fileurl).expect("unable to read file");

        for i in ftext.split('\n') {
            for j in i.split(' ') {
                vset.insert(j.to_string());
            }
        }

        let mut tmp = Vextract {
            punc: "!\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~“”„‚‘’()".to_string(),
            plist: pl.iter().map(|s| s.clone().to_string()).collect(),
            alist: al.iter().map(|s| s.clone().to_string()).collect(),
            voc: vset,
            text: ftext,
            vocab: Vec::new(),
        };
        tmp.pstrip();
        tmp.make_lower();
        tmp.remove_nums();

        tmp
    }

    pub fn pstrip(&mut self) {
        let mut tmp: HashSet<String> = HashSet::new();
        let mut tmpv: Vec<String> = Vec::new();

        for i in self.voc.iter() {
            let mut cpunc = true;
            let mut j = i.clone();

            while cpunc {
                let mut x = (j.chars().count() as i64) - 1;
                if x < 0 {
                    x = 0;
                }

                let y = x as usize;

                if contains_str(&self.alist, i) {
                    tmp.insert((*i.clone()).to_string());
                    tmpv.push((*i.clone()).to_string())
                } else {
                    if self.punc.contains(j.chars().nth(y).unwrap_or('0')) {
                        j.pop();
                        continue;
                    }
                    if self.punc.contains(j.chars().nth(0).unwrap_or('0')) {
                        let b = j.chars().nth(0).unwrap();
                        j = j.replace(b, "");
                        continue;
                    }
                }

                cpunc = false;
            }

            tmp.insert(j.clone());
            tmpv.push(j);
        }

        self.voc.clear();
        self.voc.extend(tmp);
        self.vocab.clear();
        self.vocab.extend(tmpv);
    }

    pub fn make_lower(&mut self) {
        let mut tmp: HashSet<String> = HashSet::new();
        let mut tmpv: Vec<String> = Vec::new();

        for i in self.voc.iter() {
            let j = i.clone();

            if contains_str(&self.plist, &j) || contains_str(&self.alist, &j) {
                tmp.insert(j.clone());
                tmpv.push(j);
            } else {
                tmp.insert(j.clone().to_lowercase());
                tmpv.push(j.to_lowercase());
            }
        }

        self.voc.clear();
        self.voc.extend(tmp);
        self.vocab.clear();
        self.vocab.extend(tmpv);
    }

    pub fn remove_nums(&mut self) {
        let mut tmp: HashSet<String> = HashSet::new();
        let mut tmpv: Vec<String> = Vec::new();

        for i in self.voc.iter() {
            let j = i.clone();
            match j.parse::<f64>() {
                Ok(_s) => (),
                Err(_s) => {
                    tmp.insert(j.clone());
                    tmpv.push(j);
                }
            }
        }

        self.voc.clear();
        self.voc.extend(tmp);
        self.vocab.clear();
        self.vocab.extend(tmpv);
    }

    pub fn add_punctuation(&mut self, s: &str) {
        self.punc += s;
    }

    pub fn get_vocab(&self) -> Vec<String> {
        self.vocab.clone()
    }

    pub fn get_pretty_vocab(&self) -> String {
        let mut x = String::new();
        for y in self.vocab.clone() {
            x += format!("{}\n", y).as_str();
        }

        x
    }

    pub fn get_sorted_vocab(&self) -> Vec<String> {
        let mut x = self.vocab.clone();
        x.sort();
        x
    }

    pub fn get_sorted_pretty_vocab(&self) -> String {
        let mut x = String::new();
        let mut y = self.vocab.clone();
        y.sort();

        for y in y {
            x += format!("{}\n", y).as_str();
        }

        x
    }

    pub fn get_len(&self) -> u32 {
        self.vocab.len() as u32
    }

    pub fn write_to_file(&self, fileurl: &str) {
        fs::write(fileurl, self.get_sorted_pretty_vocab()).expect("Err");
    }
}