-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimport.js
176 lines (123 loc) · 4.55 KB
/
import.js
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
// Libraries
const fs = require('fs')
const csv = require('csv-parser')
const natural = require('natural')
const accents = require('remove-accents')
const dice = require('fast-dice-coefficient')
// Time counter
const start = Date.now()
// Reading data
const results = []
fs.createReadStream('./data/metadata.csv').pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => parse(results))
// Parsing
const parse = (records) => {
// Filtering and inversion
records = records.reduce((records, record) => {
record.authors = record[Object.keys(record)[0]]
record.authors = record.authors.split('; ')
// Filter
// if (record.abstract.length > 3000) return records
// if (record.authors.length > 10) return records
// Clean authors
record.authors = record.authors.reduce((authors, author) => {
let string = author
string = string.normalize('NFC')
string = string.replace(/ *\([^)]*\) */g, "") // Remove parethesis and content
string = `${string.split(', ')[1]} ${string.split(', ')[0]}`
string = string.trim()
string = accents.remove(string)
string = string.replace(/\s\s+/g, ' ') // Replace multiple spaces
string = string.replace('undefined ', '') // Clean names already switched
// if (string == 'undefined') return authors
// string = string.replace(/\./g, '') // remove dots
authors.push(string)
return authors
}, [])
// Filter
// if (record.authors.length == 0) return records
// Add
// console.log(record.authors)
records.push(record)
return records
}, [])
// Grouping by author
const authors = records
// .slice(0, 100) // Trim for testing
.reduce((authors, record, i) => {
if ((i % 1000) === 0) console.log('Grouping record #', records.length - i)
// const year = parseInt(record.publish_time.split('-')[0])
const text = `${record.title_plain} ${record.keywords} ${record.abstract_plain} `
const update = author => {
author.docs++
author.text += text
// if (author.years[year]) (author.years[year])++
// else (author.years[year]) = 1
}
const add = name => {
authors.push({
name: name,
docs: 1,
// years: {
// [year]: 1
// },
peers: [],
variants: [],
text: text
})
}
record.authors.forEach(name => {
// Update same
const same = authors.find(a => a.name === name)
if (same) {
update(same)
return
}
// Update similar
const similar = authors.find(a => dice(a.name, name) > .9)
if (similar) {
if (!similar.variants.includes(similar.name)) similar.variants.push(similar.name)
update(similar)
return
}
// Create new
add(name)
})
return authors
}, [])
// Add ids
authors.forEach((author, i) => {
author.id = i
})
// Transform authors into ids
records.forEach((record, i) => {
if ((i % 1000) === 0)
console.log('Setting peers for record #', records.length - i)
const peers = authors.filter(author => {
let flag = false
if (record.authors.includes(author.name)) flag = true
author.variants.forEach(variant => {
if (record.authors.includes(variant)) {
flag = true
}
})
return flag
})
// console.log(peers)
const ids = peers.map(author => author.id)
peers.forEach(peer => {
ids.forEach(id => {
if (!peer.peers.includes(id)) peer.peers.push(id)
})
})
})
// Time end
const end = Date.now()
const d = new Date(end - start)
console.log(`Time computed ${d.getUTCHours()}h ${d.getUTCMinutes()}m ${d.getUTCSeconds()}s ${d.getUTCMilliseconds()}ms`)
// Write JSON
fs.writeFile('./data/authors.json', JSON.stringify(authors, null, '\t'), err => {
if (err) throw err
})
}