-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.pl
125 lines (123 loc) · 4.29 KB
/
parse.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
#!/usr/bin/perl
use strict;
use warnings;
use MongoDB;
use File::Spec;
use boolean;
my $client = MongoDB::MongoClient->new or die("Couldn't connect");
my $db = $client->get_database('shakespeare') or die ("No db");
my $plays = $db->get_collection('plays') or die("no collection");
my $id = 0;
foreach my $arg (@ARGV) {
my @files = <$arg/*>;
foreach my $file (@files) {
print "$file\n";
my $type = (File::Spec->splitdir($arg))[-1];
open(my $lines, $file) or die "Can't open $file: $!";
my $title;
my $actI = 0;
my $act;
my $speaker;
my $scene;
my $locale;
my $speechline;
my $direction = 0;
my $count = 0;
my $linecount = 0;
my $speechNumber = 0;
my $stillInScene = 0;
my $stageDirection = 0;
my $stageLine;
while(defined(my $line = <$lines>)) {
$count++;
if($line =~ m/^\s+$/) {
$stillInScene = 0;
next;
}
if(!defined($title)) {
chomp(($title) = ($line =~ m/\s*(.+)\s*/));
next;
}
if($line =~ m/ACT I/i) {
$actI = 1;
}
if($actI == 1) {
next if($line =~ m/^\s+?$title\s*?/);
if($line =~ m/^ACT/) {
($act) = ($line =~ m/act ([a-z]+)/i);
}
elsif($line =~ m/^SCENE/) {
$stillInScene = 1;
($scene) = ($line =~ m/scene\s+([a-z]+)/i);
($locale) = ($line =~ m/scene\s+[a-z]+:*\s+?(.+)/i);
#Often times locale will not be specified
if(!defined($locale)) {
$locale = "";
}
$speaker = undef;
$speechNumber++;
}
elsif($line =~ m/^[^\t]/) {
if($line =~ m/[^\t]+\t.+/) {
if(defined($speaker)) {
$speechNumber++;
}
($speaker, $speechline) = ($line =~ m/^([^\t]+)\t(.+)/);
$linecount++;
$plays->save({"title"=>"$title", "act"=>"$act", "scene"=>"$scene", "location"=>"$locale", "speaker"=>"$speaker", "line"=>"$speechline","lineNumber"=>$linecount, "type"=>"$type", "speech"=>$speechNumber, "_id"=>$id}) or die("Something wrong!");
$id++;
}
}
elsif($line =~ m/\t/) {
if($line =~ m/^\s*\[.+\]\s*$/) {
if($line =~ m/enter/i and !defined($speaker)) {
$stageDirection = () = $line =~ /[A-Z][A-Z|\s]{2,}/g;
if($stageDirection == 1) {
($speaker) = ($line =~ m/([A-Z][A-Z|\s]{2,})/);
}
}
my ($action) = ($line =~ m/(\[.+\])/);
$plays->save({"title"=>"$title", "act"=>"$act", "scene"=>"$scene", "location"=>"$locale", "stagedirection"=>true, "line"=>"$action","lineNumber"=>$linecount, "type"=>"$type", "speech"=>$speechNumber, "_id"=>$id++}) or die("Something wrong!");
$linecount++;
}
elsif($stillInScene == 1) {
my ($temp) = ($line =~ m/^\s*(.+)/);
$scene .= $temp;
}
elsif($line =~ m/\[/ && $line !~m/\]/) {
my ($action) = ($line =~ m/\[(.+)$/);
$stageLine = $action;
$plays->save({"title"=>"$title", "act"=>"$act", "scene"=>"$scene", "location"=>"$locale", "stagedirection"=>true, "line"=>"$action","lineNumber"=>$linecount, "type"=>"$type", "speech"=>$speechNumber, "_id"=>$id++}) or die("Something wrong!");
$linecount++;
$direction = 1;
}
elsif($line =~ m/\]/ && $line !~m/\[/) {
my ($action) = ($line=~m/\t(.+)\]/);
$stageLine .= $line;
if($stageLine =~ m/enter/i and !defined($speaker)) {
$stageDirection = () = $stageLine =~ /([A-Z][A-Z\s?]{2,})/g;
if($stageDirection == 1) {
($speaker) = ($stageLine =~ m/([A-Z][A-Z\s?]{2,})/);
}
}
$plays->save({"title"=>"$title", "act"=>"$act", "scene"=>"$scene", "location"=>"$locale", "stagedirection"=>true, "line"=>"$action","lineNumber"=>$linecount, "type"=>"$type", "speech"=>$speechNumber, "_id"=>$id++}) or die("Something wrong!");
$linecount++;
$direction = 0;
}
elsif($direction == 1) {
my ($action) = ($line =~ m/\t+(.+)/);
$plays->save({"title"=>"$title", "act"=>"$act", "scene"=>"$scene", "location"=>"$locale", "stagedirection"=>true, "line"=>"$action","lineNumber"=>$linecount, "type"=>"$type", "speech"=>$speechNumber, "_id"=>$id++}) or die("Something wrong!");
$linecount++;
$stageLine .= $line;
}
elsif($direction == 0) {
my ($temp) = ($line =~ m/\t(.+)/);
$linecount++;
$plays->save({"title"=>"$title", "act"=>"$act", "scene"=>"$scene", "location"=>"$locale", "speaker"=>"$speaker", "line"=>"$temp","lineNumber"=>$linecount, "type"=>"$type", "speech"=>$speechNumber, "_id"=>$id++}) or die("Something wrong!");
$speechline .= "\n".$temp;
}
}
}
}
}
}