File size: 6,044 Bytes
d736789
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const references = (function() {

    var minSimilarity = 0.8;
    var minReferenceLength = 20;
    var fields = ['author', 'title', 'booktitle', 'publisher', 'pages', 'year', 'doi', 'location', 'address', 'editors', 'series', 'journal', 'institution', 'volume', 'number'];

    var tokenEntryCache = {};

    return {

        readReferences: function () {
            var referenceLists = bib.referenceLists;
            console.log(referenceLists);
            $.each(referenceLists, function (id, references) {
                $.each(references, function (i, reference) {
                    if (reference.length > minReferenceLength) {
                        var tokenizedReferences = tokenizeSearchString(reference);
                        var max = 0;
                        var maxId = '';
                        var candidateCount = 0;
                        $.each(bib.entries, function (id2, entry) {
                            if (id2 != id) {
                                var similarity = computeSearchSimilarity(id2, entry, tokenizedReferences);
                                if (similarity > minSimilarity) {
                                    candidateCount++;
                                }
                                if (similarity > max) {
                                    max = similarity;
                                    maxId = id2;
                                }
                            }
                        });
                        if (max > minSimilarity && candidateCount == 1) {
                            if (!bib.entries[id].references) {
                                bib.entries[id].references = '';
                            }
                            if (bib.entries[id].references.split(' ').indexOf(maxId) == -1) {
                                bib.entries[id].references += ' ' + maxId;
                                bib.entries[id].references = bib.entries[id].references.trim();
                                console.log(id + ': ' + maxId + ' ' + max + ' ' + reference);
                            }
                        }
                    }
                });
            });
        },

        updateReferences: function () {
            if (!citations) {
                return;
            }
            bib.references = {};
            $.each(bib.entries, function (id, entry) {
                var referencesOutgoing = parseReferences(entry['references']);
                referencesOutgoing = (referencesOutgoing ? referencesOutgoing.filter(onlyUnique) : null);
                if (!bib.references[id]) {
                    bib.references[id] = {};
                }
                bib.references[id].referencesOutgoing = referencesOutgoing;
                if (referencesOutgoing) {
                    $.each(referencesOutgoing, function (i, id2) {
                        if (!bib.references[id2]) {
                            bib.references[id2] = {};
                        }
                        if (!bib.references[id2].referencesIncoming) {
                            bib.references[id2].referencesIncoming = [];
                        }
                        bib.references[id2].referencesIncoming.push(id);
                    });
                }
            });
            bib.filteredReferences = {};
            $.each(bib.filteredEntries, function (id, entry) {
                bib.filteredReferences[id] = {};
                if (bib.references[id].referencesOutgoing) {
                    bib.filteredReferences[id].referencesOutgoing = [];
                    $.each(bib.references[id].referencesOutgoing, function (i, id2) {
                        var passedFilter = Object.keys(bib.filteredEntries).indexOf(id2) >= 0;
                        if (passedFilter) {
                            bib.filteredReferences[id].referencesOutgoing.push(id2);
                        }
                    });
                }
                if (bib.references[id].referencesIncoming) {
                    bib.filteredReferences[id].referencesIncoming = [];
                    $.each(bib.references[id].referencesIncoming, function (i, id2) {
                        var passedFilter = Object.keys(bib.filteredEntries).indexOf(id2) >= 0;
                        if (passedFilter) {
                            bib.filteredReferences[id].referencesIncoming.push(id2);
                        }
                    });
                }
            });
        }
    };


    function computeSearchSimilarity(id, entry, tokens) {
        if (!tokenEntryCache[id]) {
            tokenEntryCache[id] = {};
        }
        var matchCount = 0;
        $.each(tokens, function (i, token) {
            var containedInValues = false;
            if (tokenEntryCache[id][token] != undefined) {
                containedInValues = tokenEntryCache[id][token];
            } else {
                $.each(fields, function (j, field) {
                    if (entry[field]) {
                        if (entry[field].toLowerCase().indexOf(token) >= 0) {
                            containedInValues = true;
                        }
                    }
                });
                containedInValues = id.toLowerCase().indexOf(token) >= 0 || containedInValues;
                tokenEntryCache[id][token] = containedInValues;
            }
            matchCount += containedInValues ? 1 : 0;
        });
        return matchCount / tokens.length;
    }

    function tokenizeSearchString(text) {
        text = text.toLowerCase();
        var re = /\W+/;
        var words = text.split(re);
        words = $.grep(words, function (word) {
            return word.length > 1 && !($.inArray(word, bib.stopwords) >= 0);
        });
        return words;
    }

    function parseReferences(refString) {
        if (!refString) {
            return null;
        }
        return refString.split(' ');
    }

    function onlyUnique(value, index, self) {
        return self.indexOf(value) === index;
    }

})();