-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinal.pl
executable file
·151 lines (142 loc) · 4.13 KB
/
final.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
#!/usr/bin/perl
our %moviesPresent = (); #key: actor. Value: movies that the actor appears in
#our %visitedActors = ("Bacon, Kevin" => 1);
our @pathActors = (); # will be an array of arrays; keep track of all the paths found from KB
while(@ARGV) {
my $filename = shift(@ARGV);
open($FH, "zcat $filename |")
or die "cannot open $filename";
my $current_actor = "";
while(<$FH>) {
if ($_ =~ /^([^\t]+)\t.*/) { # capture the actor's name (don't come here unless the line starts with the actor's name)
$current_actor = $1;
}
if ($_ =~ /^[^\t]*\t* ( .*?[)] )/x) {
$current_movie = $1;
}
if ($current_movie =~ /^[^"].*$/ && $_ =~ /^(. (?!\(TV\)) (?!\(V\)) (?!\(VG\)) )*$/x) { #regex line 1: consume leading tabs and capture movie & date
#regex line 2: assert there are no instances of "(TV)", "(V)", or "(VG)"
my @array;
if (exists $moviesPresent{$current_actor}) {
@array = @{$moviesPresent{$current_actor}};
}
else {
@array = ();
}
push @array, $current_movie;
$moviesPresent{$current_actor} = \@array;
}
}
}
print "b\n";
findPaths();
$blah = $#pathActors + 1;
print "$blah actors are connected to KB\n";
print "Actor/Actress? ";
while(<>) {
chomp;
@keyWords = /([^\s]+)/ig; # whitespace is the delimiter
$potentials = findMatches(\@keyWords);
@potentList = @{$potentials};
$var = $#potentList + 1;
print "$var results found\n";
if (@potentList == 1) {
$path = search($potentList[0]);
if ($path) {
$actOne = pop @{$path};
print "$actOne\n";
while (@{$path} > 0) {
$actTwo = pop @{$path};
$commonM = commonMovie($actOne, $actTwo);
print "\t$commonM\n";
print "$actTwo\n";
$actOne = $actTwo;
}
}
else {
print "Whoops, looks like there's no path from $potentList[0] to Kevin Bacon :(\n";
}
}
else {
foreach $actor (@{$potentials}) {
print "$actor\n";
}
}
print "Actor/Actress? ";
}
# get a common movie between the two given actors.
sub commonMovie {
# print "Finding common movie\n";
my $firstAct = shift;
my $secondAct = shift;
# my $hash = shift;
# my %tempTable = %{$hash};
my %hash2 = map {$_ => 1} @{$moviesPresent{$secondAct}};
foreach $movie (@{$moviesPresent{$firstAct}}) {
if (exists $hash2{$movie}) {
return $movie;
}
}
return "";
}
# print all relevant actors, based on the "keywords" typed in by the user
# if a specific actor is specified, call search
sub findMatches {
my $ref = shift;
my @keyWords = @{$ref};
my @results = ();
foreach $actor (keys %moviesPresent) { # don't forget to omit the ,
my $flag = 1;
$modActor = $actor;
$modActor =~ s/,//g;
foreach $keyword (@keyWords) {
if ($modActor !~ /\b $keyword \b/xi || $actor !~ /\b $keyword \b/xi) {
$flag = 0;
last;
}
}
if ($flag) {
push @results, $actor; # we still want the comma in there
}
}
return \@results;
}
# borrows from Dijkstra's algorithm to find all of the shortest paths from KB
sub findPaths {
# my %mpCopy = %moviesPresent;
my @actQueue = ("Bacon, Kevin");
# print "$actQueue[0]\n";
my @pathQueue = (("Bacon, Kevin")); # these two lists should be synced together, that way we get the correct sub-path for each actor
while (@actQueue) {
# print "a\n";
my $currentAct = shift @actQueue;
my $currentPath = shift @pathQueue;
my $neighbors = findUnvisitedNeighbors($currentAct);
foreach $neighbor (@{$neighbors}) {
$copyPath = $currentPath;
push @{$copyPath}, $neighbor;
push @pathActors, $copyPath;
push @actQueue, $neighbor;
push @pathQueue, $copyPath;
$visitedActors{$neighbor} = 1;
}
# delete($mpCopy{$currentAct});
}
}
sub findUnvisitedNeighbors {
$main = shift;
# $hash = shift;
@results = grep {!(exists $visitedActors{$_}) && commonMovie($main, $_)} keys %moviesPresent;
# print "$#others neighbors of $main\n";
return \@results;
}
sub search {
$target = shift;
foreach $pathRef (@pathActors) {
@path = @{$pathRef};
if ($path[-1] eq $target) {
return $pathRef;
}
}
return "";
}