-
Notifications
You must be signed in to change notification settings - Fork 2
/
import.js
168 lines (114 loc) · 4.2 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
// Libraries
const fs = require('fs')
const csv = require('csv-parser')
const accents = require('remove-accents')
const stringify = require("json-stringify-pretty-compact")
// 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.authors.split('; ')
// Filter
if (!record.abstract.includes('COVID-19') || !record.title.includes('COVID-19')) return records
if (!record.doi) return records
// Clean authors
record.authors = record.authors.reduce((authors, author) => {
let string = author
string = string.normalize('NFC')
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
// string = string.replace(/\./g, '') // remove dots
string = string.replace(/\,/g, '') // remove commas
// filter
if (
string.includes('039') ||
string.includes('Direccion') ||
string.includes('Ministerio') ||
string == 'undefined'
) {
console.log(string, 'removed')
return authors
}
authors.push(string)
return authors
}, [])
// Filter
if (record.authors.length == 0) return records
// Add
records.push(record)
return records
}, [])
// Grouping by author
const authors = records
// .slice(0, 1000) // 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} ${record.abstract} `
// Create author
const add = name => {
authors.push({
id: authors.length,
name: name,
docs: 1,
years: { [year]: 1 },
peers: [],
text: text
})
}
// Update author
const update = (author) => {
author.docs++
author.text += text
if (author.years[year]) (author.years[year])++
else (author.years[year]) = 1
}
// Create and update choice
record.authors.forEach(name => {
const found = authors.find(a => a.name === name)
if (found) update(found)
else add(name)
})
return authors
}, [])
// 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', stringify(authors, { maxLength: 200 }), err => {
if (err) throw err
})
}