-
Notifications
You must be signed in to change notification settings - Fork 0
/
populateFromRIS.pl
executable file
·416 lines (379 loc) · 10.2 KB
/
populateFromRIS.pl
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#! /usr/bin/perl
open($file_professors, "<",'lattes_out/sbc-membros.ris')
or die "não foi possivel abrir o arquivo";
open($file_proj, "<",'sbc.owl')
or die "não foi possivel abrir o arquivo";
open($file_out, ">",'populado.owl')
or die "não foi possivel abrir o arquivo";
open($file_publications, "<", 'lattes_out/sbc-publicacoes.ris')
or die "não foi possivel abrir o arquivo";
# constant strings
$indent = " ";
$new_ln = "\n";
$end_doc = '</rdf:RDF>'.$new_ln;
$about_prefix = '<owl:NamedIndividual rdf:about="&renata;FOAF-modified#';
$resource_prefix = ' rdf:resource="&renata;FOAF-modified';
$data_prop_prefix = 'renata:FOAF-modified';
#global data structures
%peopleNames;
%publishersNames;
%universitiesNames;
#########################################################################################
# #
# Individual Constructors #
# #
#########################################################################################
sub newEntry{
my $label = "@_";
chomp($label);
$label =~ s/^\s(.*)/$1/;
$label =~ s/\s+/_/g;
my $txt = $new_ln.'<!-- http://www.ime.usp.br/~renata/FOAF-modified#'."@_".' -->'.$new_ln.$new_ln;
$txt = $txt.$about_prefix.$label.'">'.$new_ln;
# <owl:NamedIndividual rdf:about="&renata;FOAF-modified#Arnaldo_Mandel">
}
sub class{
return '<rdf:type'.$resource_prefix."@_".'"/>'.$new_ln;
# <rdf:type rdf:resource="&renata;FOAF-modifiedPerson"/>
}
sub stringDataProps{
my $txt='';
if (@_ == undef){ return $txt;}
my %hash = @_;
foreach my $key ( keys %hash ){
my $value = $hash{$key};
chomp($value);
$value =~ s/\s/_/g;
$txt = $txt.'<'.$key.'>'.$value.'</'.$key.'>'.$new_ln;
# <renata:FOAF-modifiedfirstName>Arnaldo</renata:FOAF-modifiedfirstName>
}
return $txt;
}
sub intDataProps{
my $txt="";
if (@_ == undef){ return $txt;}
my %hash = @_;
foreach my $key ( keys %hash ){
my $value = $hash{$key};
my $value = $hash{$key};
$value =~ s/\s/_/g;
$txt = $txt.'<'.$key.' rdf:datatype="&xsd;integer">'.$value.'</'.$key.'>'.$new_ln;
# <studentFrom rdf:datatype="&xsd;integer">1970</studentFrom>
}
return $txt;
}
sub relationProps{
my $txt="";
if (@_ == undef){ return $txt;}
my %hash = @_;
foreach my $key ( keys %hash ){
my $value = $hash{$key};
chomp($value);
$value =~ s/\s/_/g;
$txt = $txt.'<'.$key.$resource_prefix.'#'.$value.'"/>'.$new_ln;
# <professorAt rdf:resource="&renata;FOAF-modified#Brasil"/>
}
return $txt;
}
sub indent{
my @txt = split(/\n/ , $_[0]);
my $s = 4;
my $e = @txt-2;
for (@txt){
$_ = $_."\n".$indent;
}
for my $i( $s...$e){
$txt[$i] = $indent.$txt[$i];
}
return @txt;
}
sub endEntry {
return '</owl:NamedIndividual>'.$new_ln.$new_ln.$new_ln;
}
#########################################################################################
# #
# getters of individuals data #
# #
#########################################################################################
sub thesisTitle{
$t = $_[0];
if ($t =~ s/\s*Título:\s*(.*)/$1/) {
$t =~ s/(.*),\s*Ano.*/$1/;
#print $t."\n";
return $t;
}
}
sub supervisor{
my @thesis = split (/\./,$_[0]);
for my $t (@thesis) {
if ($t =~ s/\s*Orientador:\s*(.*)/$1/) {
$t =~ s/\s*com período.*//;
chomp $t;
return $t;
}
}
return undef;
}
sub name{
if( s/NOME\s*-\s*(.*)/$1/) { #find name
return split(/\s+/);
}
return undef;
}
sub nameProps{
return (
'renata:FOAF-modifiedfirstName' => $_[0],
'renata:FOAF-modifiedfamilyName' => $_[-1],
);
}
sub citation{
my $cit = <$file_professors>;
if( $cit =~ s/CITA\s+-\s+(.*)/$1/){
return split(/;/, $cit);
}
}
sub startDate{
my $date = "@_";
if( $date =~ s/FO[0-9]a\s*-\s*(.*)/$1/) { # start year
return $date;
}
return 0;
}
sub endDate{
my $date = "@_";
if( $date =~ s/FO[0-9]b\s*-\s*(.*)/$1/) { # end year
return $date;
}
return 0;
}
sub gradLevel{
my $level = "@_";
if( $level =~ s/FO[0-9]c\s*-\s*(.*)/$1/) { #graduation level(undergraduate/ masters/ doctorade)
if($level =~ m/\s*Doutorado.*/i){
return "doctorateStudent";}
elsif($level =~ m/\s*Mestrado.*/i){
return "mastersStudent";}
elsif($level =~ m/\s*Graduação.*/i){
return "undergraduateStudent";}
}
return 0;
}
sub university{
my $uni = "@_";
if($uni =~ s/FO[0-9]d\s*-\s*(.*)/$1/) { # University name
@uni = split(/\s*,\s*/, $uni);
chomp(@uni);
return @uni;
} #spli
return undef;
}
sub thesisInfo{
my $d = $_[0];
my $t = $_[1];
if($t =~ s/FO[0-9]e\s+-\s+(.*)/$1/) { # thesis data
my $title = thesisTitle $t;
my $new = newEntry($title).class("#Thesis").stringDataProps("documentTitle"=> $title).intDataProps("publicationYear" => $d);
if( my $supervisor = supervisor $t) {
#$supervisor = addPerson($supervisor);
$peopleNames{ $supervisor} = 1;
my $props = relationProps(('supervisedBy'=>$supervisor, "authorOf" => $title));
print $file_out indent( $new.relationProps('supervisedBy'=>$supervisor).endEntry);
return $props;
}
}
return "";
}
sub nextLine{
my $f = $_[0];
my $line = <$f>;
chomp $line;
return $line;
}
sub studyHistoric{
my $hist = "";
while(my $line = (nextLine $file_professors)){
if ( my $temp = startDate $line) { # beginning of a graduation register
$startDate = $temp; # first line is the admission year
$endDate = endDate( nextLine $file_professors); # year of graduation
$gradLevel = gradLevel( nextLine $file_professors); # graduation type
if ($gradLevel){ # should be masters, doctorade or undergraduate
@uni = university( nextLine $file_professors); # @uni = {name, country}
addUni(@uni);
$relProps{$gradLevel."At"} = $uni[0]; #ex Person doctoradeAt Organization
$date{$gradLevel."From"} = $startDate; #ex Person doctoradeFrom Organization
$date{$gradLevel."To"} = $endDate; #ex Person doctoradeTo Organization
$hist = $hist.( thesisInfo( $endDate, nextLine $file_professors));
}
}
if ($line =~ m/TY\s*-\s*MEMBRO.*/) {
$hist = $hist.( intDataProps %date).( relationProps %relProps);
print $hist;
return $hist;
}
}
return $hist.( intDataProps %date).(relationProps %relProps);
}
sub isNewProfessorEntry{
# if( $_[0] =~ m/TY\s*-\s*MEMBRO/) {
if( $_[0] =~ m/NOME\s*-.*/) {
return 1;
}
else {
return 0;
}
}
sub keepName{
$p = $_[0];
$p = lc($p);
my @name = split(/ /,$p);
foreach (@name) {
s/^(.)/\U$1/;
}
$peopleNames{"@name"} = 1;
}
sub findFullNames{
my @fullNames;
my @citations = split(/;/, $_[0]); #split per author reference
# print "citations: @citations\n";
foreach( @citations) {
chomp;
my $t = $_;
$t =~ s/.//;
my @cit = split(/[, ]/, $t); #split last name from rest of names
$t = @cit[0]; #put name in sequential order
@cit[0] = @cit[-1];
@cit[-1] = $t;
foreach my $name ( keys %peopleNames){
if ( $name =~ m/.*\s*$cit[-1]/i){ #try to match last name
if ($name =~ m/$cit[0].*/i or $name =~ m/.*$cit[1].*/i){ #try to match first or second name
push( @fullNames, $name); #found author
}
}
}
}
return @fullNames;
}
sub populateProfessors{
print "adding professors\n";
while( <$file_professors>){
if( isNewProfessorEntry($_)){
my @name = name($_); #get name
keepName("@name");
my $new = newEntry("@name").class ('Person'). stringDataProps( nameProps @name).studyHistoric.relationProps("professorAt" => "IME").endEntry;
print $file_out $new_ln;
print $file_out indent($new);
print $file_out $new_ln;
}
}
close($file_professors);
print "finished adding professors\n";
}
sub authors{
my $l = "@_";
my @authors;
if ($l =~ s/AU\s*-\s*(.*)/$1/){
@authors = findFullNames($l);
}
return @authors;
}
#receives where it was published and line to extract title from
sub pubTitle{
my $title = $_[0];
if ($title =~ s/T1\s*-\s*(.*)/$1/){
return $title;
}
return "";
}
sub publisherType{
if ($_[0] =~ s/^TY\s*-\s*(.*)/$1/){
if ($_[0] =~ m/CONF\s+/){
return "Conference";
}
if ($_[0] =~ m/MGZN\s+/){
return "Magazine";
}
}
return "";
}
sub publisherName{
my $name = $_[0];
if ($name =~ s/TI\s*-\s*(.)/$1/) {
return $name;
}
return "";
}
sub publicationYear{
if( $_[0] =~ s/PY\s*-\s*(.*)/$1/) {
return $_[0];
}
return 0;
}
sub addPublisher{
$name = $_[0];
$type = $_[1];
if( !$universitiesNames{$name} ) {
print $file_out indent( newEntry($name).class("#".$type).stringDataProps("publisherName" => $name).endEntry);
$universitiesNames{$names} = 1;
}
}
sub addPublications{
print "adding publications\n";
while( my $l = <$file_publications>) {
my $publisherType = publisherType $l;
if (!$publisherType) { next; } #conference or magazine
my @authors = authors( nextLine $file_publications);
my $pub_title = pubTitle( nextLine $file_publications);
my $publisherName = publisherName( nextLine $file_publications);
#print "name: $publisherName - type: $publisherType\n";
addPublisher( $publisherName, $publisherType);
my $new = newEntry($pub_title).class( '#Article');
$new = $new.relationProps( "publishedAt" => $publisherName);
$new = $new.stringDataProps( 'documentTitle' => "$pub_title");
foreach my $a( @authors){
$new = $new.relationProps("writtenBy" => $a);
}
while ($l = nextLine $file_publications){
my $publicationYear = publicationYear($l);
if( $publicationYear) {
$new = $new.intDataProps("publicationYear" => $publicationYear);
last;
}
}
print $file_out indent($new.endEntry);
}
close $file_publications;
print "finished adding publications\n"
}
sub addUni{
my %props;
if (scalar @_ > 1){
%props = ('situatedAt'=> $_[-1]);
}
if (@_) {
if (!$universitiesNames{$uni[0]}){
$universitiesNames{$uni[0]} = 1;
print $file_out indent( newEntry ($uni[0]).class("#University").relationProps( %props).endEntry );
}
}
}
sub copyLines {
print "copying project file\n";
while( <$file_proj> ) {
if( $_ =~ m/<\/rdf:RDF>/){
break;
}
else{
print $file_out $_;
}
#$line = <$file_proj>;
}
close $file_proj;
print "finished copying file\n";
}
copyLines; #copy ontology definition from file
print $file_out indent(newEntry("IME").class("#Department").relationProps("partOf" => "Universidade de São Paulo").stringDataProps('name' => "Instituto de Matemática e Estatística").endEntry);
populateProfessors;
#foreach my $k ( keys %peopleNames){
# print $k.new_ln;}
addPublications;
print $file_out $end_doc;