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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
extern crate crossref;
extern crate lazy_static;
extern crate reqwest;
extern crate serde_json;

use crate::*;
use regex::Regex;
use std::collections::HashMap;

#[derive(Debug, Clone, PartialEq)]
pub struct GenericAuthorInfo {
    pub name: Option<String>,
    pub prop2id: HashMap<String, String>,
    pub wikidata_item: Option<String>,
    pub list_number: Option<String>,
    pub alternative_names: Vec<String>,
}

impl WikidataInteraction for GenericAuthorInfo {}

impl GenericAuthorInfo {
    pub fn create_author_statement_in_paper_item(&self, item: &mut Entity) {
        let name = match &self.name {
            Some(s) => s.to_string(),
            None => "".to_string(),
        };
        let mut qualifiers: Vec<Snak> = vec![];
        match &self.list_number {
            Some(num) => {
                qualifiers.push(Snak::new_string("P1545", &num));
            }
            None => {}
        }
        let statement = match &self.wikidata_item {
            Some(q) => {
                if !name.is_empty() {
                    qualifiers.push(Snak::new_string("P1932", &name));
                }
                Statement::new_normal(Snak::new_item("P50", &q), qualifiers, vec![])
            }
            None => Statement::new_normal(Snak::new_string("P2093", &name), qualifiers, vec![]),
        };
        item.add_claim(statement);
    }

    pub fn amend_author_item(&self, item: &mut Entity) {
        // Set label, unless already set (then try alias)
        match &self.name {
            Some(name) => {
                if !name.is_empty() {
                    match item.label_in_locale("en") {
                        Some(s) => {
                            if s != name {
                                item.add_alias(LocaleString::new("en", name));
                            }
                        }
                        None => item.set_label(LocaleString::new("en", name)),
                    }
                }
            }
            None => {}
        }

        // Alternative names
        for n in &self.alternative_names {
            if !n.is_empty() {
                match item.label_in_locale("en") {
                    Some(s) => {
                        if s != n {
                            item.add_alias(LocaleString::new("en", n));
                        }
                    }
                    None => {
                        item.add_alias(LocaleString::new("en", n));
                    }
                }
            }
        }

        // Human
        if !item.has_target_entity("P31", "Q5") {
            item.add_claim(Statement::new_normal(
                Snak::new_item("P31", "Q5"),
                vec![],
                vec![],
            ));
        }

        // Researcher
        if !item.has_claims_with_property("P106") {
            item.add_claim(Statement::new_normal(
                Snak::new_item("P106", "Q1650915"),
                vec![],
                vec![],
            ));
        }

        // External IDs
        for (prop, id) in &self.prop2id {
            let existing = item.values_for_property(prop.to_string());
            let to_check = Value::StringValue(id.to_string());
            if existing.contains(&to_check) {
                continue;
            }
            /*
            println!(
                "Adding author statement {}:'{}' to {}",
                &prop,
                &id,
                item.id()
            );
            */
            let statement = Statement::new_normal(
                Snak::new_external_id(prop.to_string(), id.to_string()),
                vec![],
                vec![],
            );
            item.add_claim(statement);
        }
    }

    pub fn get_or_create_author_item(&self, mw_api: &mut mediawiki::api::Api) -> GenericAuthorInfo {
        let mut ret = self.clone();
        // Already has item?
        if ret.wikidata_item.is_some() {
            return ret;
        }
        // No external IDs
        if ret.prop2id.is_empty() {
            return ret;
        }

        // Use search
        for (prop, id) in &ret.prop2id {
            let items = self.search_external_id(prop, id, mw_api);
            if !items.is_empty() {
                ret.wikidata_item = Some(items[0].clone());
                return ret;
            }
        }

        // Labels/aliases
        let mut item = Entity::new_empty_item();
        ret.amend_author_item(&mut item);

        // Create new item and use its ID
        ret.wikidata_item = self.create_item(&item, mw_api);
        ret
    }

    pub fn merge_from(&mut self, author2: &GenericAuthorInfo) {
        if self.name.is_none() {
            self.name = author2.name.clone();
        }
        if self.wikidata_item.is_none() {
            self.wikidata_item = author2.wikidata_item.clone();
        }
        if self.list_number.is_none() {
            self.list_number = author2.list_number.clone();
        }
        for (k, v) in &author2.prop2id {
            self.prop2id.insert(k.to_string(), v.to_string());
        }
        for name in &author2.alternative_names {
            self.alternative_names.push(name.to_string());
        }
        self.alternative_names.sort();
        self.alternative_names.dedup();
    }

    fn asciify_string(&self, s: &str) -> String {
        // As long as some sources insist on using ASCII only for names :-(
        s.to_lowercase()
            .replace('ä', "a")
            .replace('ö', "o")
            .replace('ü', "u")
            .replace('á', "a")
            .replace('à', "a")
            .replace('â', "a")
            .replace('é', "e")
            .replace('è', "e")
            .replace('ñ', "n")
            .replace('ï', "i")
            .replace('ç', "c")
            .replace('ß', "ss")
    }

    /// Compares long (3+ characters) name parts
    fn author_names_match(&self, name1: &str, name2: &str) -> u16 {
        let mut ret = 0;
        lazy_static! {
            static ref RE1: Regex = Regex::new(r"\b(\w{3,})\b").unwrap();
        }
        let name1_mod = self.asciify_string(name1);
        let name2_mod = self.asciify_string(name2);
        if RE1.is_match(&name1_mod) && RE1.is_match(&name2_mod) {
            let mut parts1: Vec<String> = vec![];
            for cap in RE1.captures_iter(&name1_mod) {
                parts1.push(cap[1].to_string());
            }
            parts1.sort();
            let mut parts2: Vec<String> = vec![];
            for cap in RE1.captures_iter(&name2_mod) {
                parts2.push(cap[1].to_string());
            }
            parts2.sort();
            parts1.iter().for_each(|part| {
                if parts2.contains(part) {
                    ret += 1;
                }
            });
        }
        ret
    }

    pub fn compare(&self, author2: &GenericAuthorInfo) -> u16 {
        match (&self.wikidata_item, &author2.wikidata_item) {
            (Some(q1), Some(q2)) => {
                if q1 == q2 {
                    return 100; // This is it
                } else {
                    return 0; // Different items
                }
            }
            _ => {}
        }

        let mut ret = 0;

        for (k, v) in &self.prop2id {
            match author2.prop2id.get(k) {
                Some(v2) => {
                    if v == v2 {
                        ret += 90;
                    }
                }
                None => {}
            }
        }

        // Name match
        match (&self.name, &author2.name) {
            (Some(n1), Some(n2)) => {
                ret += 50 * self.author_names_match(&n1.as_str(), &n2.as_str());
            }
            _ => {}
        }

        // List number
        match (&self.list_number, &author2.list_number) {
            (Some(n1), Some(n2)) => {
                if n1 == n2 {
                    ret += 30;
                }
            }
            _ => {}
        }

        ret
    }

    pub fn update_author_item(
        &self,
        entities: &entity_container::EntityContainer,
        mw_api: &mut mediawiki::api::Api,
    ) {
        let q = match &self.wikidata_item {
            Some(q) => q.to_string(),
            None => return,
        };
        let original_item = match entities.get_entity(q) {
            Some(i) => i.clone(),
            None => return,
        };
        let mut item = original_item.clone();
        self.amend_author_item(&mut item);

        let mut params = EntityDiffParams::none();
        params.labels.add = EntityDiffParamState::All;
        params.aliases.add = EntityDiffParamState::All;
        params.claims.add = EntityDiffParamState::All;
        let diff = EntityDiff::new(&original_item, &item, &params);
        if diff.is_empty() {
            return;
        }
        //println!("{}", diff.actions());
        let _new_json = diff.apply_diff(mw_api, &diff).unwrap();
        //EntityDiff::get_entity_id(&new_json);
        // TODO
    }
}