| 1 | #!/usr/bin/perl | ||||||||||
| 2 | # NanoB2B::UniversalRoutines | ||||||||||
| 3 | # | ||||||||||
| 4 | # Just universally used subroutines (i.e. printColor, inArray, wordIndex) | ||||||||||
| 5 | # Version 1.0 | ||||||||||
| 6 | # | ||||||||||
| 7 | # Program by Milk | ||||||||||
| 8 | |||||||||||
| 9 | |||||||||||
| 10 | package NanoB2B::UniversalRoutines; | ||||||||||
| 11 | |||||||||||
| 12 | ####################### IMPORTS #################### | ||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use Term::ANSIColor; #color coding the output | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use List::MoreUtils qw(first_index); #check for first occurrence of a word | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use List::MoreUtils qw(indexes); #get all of the indexes of a word | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use File::Path qw(make_path); #makes sub directories | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | ##### GLOBAL VARIABLES ##### | ||||||||||
| 23 | |||||||||||
| 24 | 1 | my $debug = 0; | |||||||||
| 25 | |||||||||||
| 26 | #---------------------------------------- | ||||||||||
| 27 | # constructor | ||||||||||
| 28 | #---------------------------------------- | ||||||||||
| 29 | # constructor method to create a new UR object | ||||||||||
| 30 | # input : $params <- reference to hash containing the parameters | ||||||||||
| 31 | # output: $self <- a UR object | ||||||||||
| 32 | |||||||||||
| 33 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 34 | #grab class and parameters | ||||||||||
| 35 | 0 | my $self = {}; | |||||||||
| 36 | 0 | my $class = shift; | |||||||||
| 37 | 0 | ▶ | return undef if(ref $class); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 0 | my $params = shift; | |||||||||
| 39 | |||||||||||
| 40 | #bless this object | ||||||||||
| 41 | 0 | bless $self, $class; | |||||||||
| 42 | |||||||||||
| 43 | # get some of the parameters | ||||||||||
| 44 | 0 | my $debugoption = $params->{'debug'}; | |||||||||
| 45 | 0 | ▶ | if(defined $debugoption){$debug = $debugoption;} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | |||||||||||
| 47 | 0 | return $self; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | ################# HERE BE SUB-ROUTINES ###################### | ||||||||||
| 51 | ################# (alphabatized for your convenience :D) ###################### | ||||||||||
| 52 | |||||||||||
| 53 | |||||||||||
| 54 | #excludes a value from a number array | ||||||||||
| 55 | # input : $e <-- the range of elements in the array from 1-$e ($e=5 => 1,2,3,4,5) | ||||||||||
| 56 | # $bully <-- the number to exclude from the set (3 => 1,2,4,5) | ||||||||||
| 57 | # output : @kids <-- the number set | ||||||||||
| 58 | 0 | ▶ | sub bully{ | ||||||||
|
Subroutine
bully: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 59 | 0 | my $self = shift; | |||||||||
| 60 | 0 | my $num = shift; | |||||||||
| 61 | 0 | my $bully = shift; | |||||||||
| 62 | |||||||||||
| 63 | 0 | my @kids = (1..$num); | |||||||||
| 64 | 0 | @kids = grep { $_ != $bully } @kids; | |||||||||
| 65 | 0 | return @kids; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | # cleans the line without getting rid of tags | ||||||||||
| 69 | # input : $input <-- the line to clean up | ||||||||||
| 70 | # output : $input <-- cleaned up input line | ||||||||||
| 71 | 0 | ▶ | sub cleanWords{ | ||||||||
|
Subroutine
cleanWords: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 72 | 0 | my $self = shift; | |||||||||
| 73 | 0 | my $input = shift; | |||||||||
| 74 | |||||||||||
| 75 | 0 | $input = lc($input); | |||||||||
| 76 | 0 | $input =~ s/[^a-zA-z0-9\:\.\s<>&#;\*\/]/ /og; #get rid of non-ascii | |||||||||
| 77 | #$input =~ s/([0-9]+(\.[0-9]*)?)-[0-9]+(\.[0-9]*)?/RANGE/g; #get rid of range num (#-#) | ||||||||||
| 78 | 0 | $input =~ s/[0-9]+(.[0-9]+)?/NUM/og; #get rid of normal num (#.#) | |||||||||
| 79 | 0 | $input =~ s/\s?=\s?/eq/og; #get rid of = | |||||||||
| 80 | 0 | $input =~ s/<Node id.*?\/>//og; #get rid of <NODE id=##/> | |||||||||
| 81 | 0 | $input =~ s/[\*\/]//og; #get rid of * and / | |||||||||
| 82 | #$input =~ s/[,\)\(\\\'\/\=\*\-]/ /g; | ||||||||||
| 83 | 0 | $input =~ s/\s\+/_/og; #get rid of _+ space | |||||||||
| 84 | 0 | $input =~ s/\s+\.\s+/ /og; #get rid of _._ periods | |||||||||
| 85 | 0 | $input =~ s/\.\s+/ /og; #get rid of ._ space | |||||||||
| 86 | 0 | $input =~ s/\s+/ /og; #get rid of excessive blank space | |||||||||
| 87 | 0 | return $input; | |||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | #helper function that retrieves all of the indexes of a word in a given set | ||||||||||
| 91 | # input : $word <-- an element or object | ||||||||||
| 92 | # @set <-- the array to look through | ||||||||||
| 93 | # output : array <-- returns a set of all indexes of the word; returns {-1} if none found | ||||||||||
| 94 | 0 | ▶ | sub getAllIdxs{ | ||||||||
|
Subroutine
getAllIdxs: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 95 | 0 | my $self = shift; | |||||||||
| 96 | 0 | my $word = shift; | |||||||||
| 97 | 0 | my $set_ref = shift; | |||||||||
| 98 | 0 | my @set = @$set_ref; | |||||||||
| 99 | |||||||||||
| 100 | 0 | ▶ | my @idxs = indexes{$_ eq $word} @set; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 101 | 0 | return @idxs; | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | #gets the line's index | ||||||||||
| 105 | # input : $keyword <-- the regex to use to search for the specific line | ||||||||||
| 106 | # @lines <-- the set of lines to look through | ||||||||||
| 107 | # output : $a <-- return the index of the line based on the regex; returns -1 if not found | ||||||||||
| 108 | 0 | ▶ | sub getIndexofLine{ | ||||||||
|
Subroutine
getIndexofLine: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 109 | 0 | my $self = shift; | |||||||||
| 110 | 0 | my $keyword = shift; | |||||||||
| 111 | 0 | my $lines_ref = shift; | |||||||||
| 112 | 0 | my @lines = @$lines_ref; | |||||||||
| 113 | |||||||||||
| 114 | 0 | my $len = @lines; | |||||||||
| 115 | 0 | ▶ | for(my $a = 0; $a < $len; $a++){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 0 | my $line = $lines[$a]; | |||||||||
| 117 | 0 | ▶ | if ($line =~ /($keyword)/){ #regex checking if string contains keyword | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 0 | return $a; | |||||||||
| 119 | } | ||||||||||
| 120 | } | ||||||||||
| 121 | 0 | return -1; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | #helper function to check if an element is in an array | ||||||||||
| 125 | # input : $e <-- an element or object | ||||||||||
| 126 | # @array <-- the array to look through | ||||||||||
| 127 | # output : boolean <-- 1 if it is in the array, 0 if it isn't | ||||||||||
| 128 | 0 | ▶ | sub inArr{ | ||||||||
|
Subroutine
inArr: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 129 | 0 | my $self = shift; | |||||||||
| 130 | 0 | my $e = shift; | |||||||||
| 131 | 0 | my $arr_ref = shift; | |||||||||
| 132 | 0 | my @arr = @$arr_ref; | |||||||||
| 133 | |||||||||||
| 134 | 0 | ▶ | my $ans = first_index {$_ eq $e} @arr; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 135 | 0 | ▶ | if($ans > -1){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | return 1; | |||||||||
| 137 | }else{ | ||||||||||
| 138 | 0 | return 0; | |||||||||
| 139 | } | ||||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | #prints to a file called debug | ||||||||||
| 143 | # input : $output <-- the text to output to the debug file | ||||||||||
| 144 | # output : a local file named 'debug' | ||||||||||
| 145 | 0 | ▶ | sub print2DebugFile{ | ||||||||
|
Subroutine
print2DebugFile: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 146 | 0 | my $self = shift; | |||||||||
| 147 | 0 | my $output = shift; | |||||||||
| 148 | |||||||||||
| 149 | 0 | ▶ | open(DEBUG, ">>", "debug") || die "**ERROR: Unable to create debug file!**\n$!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | 0 | print2File(<DEBUG>, $output); | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | #prints to a file w/ line skip | ||||||||||
| 154 | # input : $file <-- the file to print to (must already be opened!) | ||||||||||
| 155 | # @array <-- the text to print to the file (includes next line) | ||||||||||
| 156 | # output : the file with the text printed to it (with a return character) | ||||||||||
| 157 | 0 | ▶ | sub print2File{ | ||||||||
|
Subroutine
print2File: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 158 | 0 | my $self = shift; | |||||||||
| 159 | 0 | my $file = shift; | |||||||||
| 160 | 0 | my $txt = shift; | |||||||||
| 161 | |||||||||||
| 162 | 0 | print $file "$txt\n"; | |||||||||
| 163 | } | ||||||||||
| 164 | |||||||||||
| 165 | #prints to a file as is | ||||||||||
| 166 | # input : $file <-- the file to print to (must already be opened!) | ||||||||||
| 167 | # @array <-- the text to print to the file | ||||||||||
| 168 | # output : the file with the text printed to it without a return character | ||||||||||
| 169 | 0 | ▶ | sub print2FileNoLine{ | ||||||||
|
Subroutine
print2FileNoLine: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 170 | 0 | my $self = shift; | |||||||||
| 171 | 0 | my $file = shift; | |||||||||
| 172 | 0 | my $txt = shift; | |||||||||
| 173 | |||||||||||
| 174 | 0 | print $file "$txt"; | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | #shows an array | ||||||||||
| 178 | # input : $delim <-- string to separate the elememts by | ||||||||||
| 179 | # @array <-- the array to print | ||||||||||
| 180 | # output : string <-- returns the array elements in a string format separated by the delimiter | ||||||||||
| 181 | 0 | ▶ | sub printArr{ | ||||||||
|
Subroutine
printArr: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 182 | 0 | my $self = shift; | |||||||||
| 183 | 0 | my $delim = shift; | |||||||||
| 184 | 0 | my $parr_ref = shift; | |||||||||
| 185 | 0 | my @parr = @$parr_ref; | |||||||||
| 186 | |||||||||||
| 187 | 0 | my $combo = join ($delim, @parr); | |||||||||
| 188 | 0 | print "$combo\n"; | |||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | |||||||||||
| 192 | #prints input with color | ||||||||||
| 193 | # input : $color <-- color to print the text in (ex. 'red', 'bold blue', 'on_green') | ||||||||||
| 194 | # $text <-- the text to print | ||||||||||
| 195 | # output : -- | ||||||||||
| 196 | 0 | ▶ | sub printColor{ | ||||||||
|
Subroutine
printColor: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 197 | 0 | my $self = shift; | |||||||||
| 198 | 0 | my $color = shift; | |||||||||
| 199 | 0 | my $text = shift; | |||||||||
| 200 | |||||||||||
| 201 | 0 | ▶ | if($color =~ /on_\w+/){print color($color), "$text", color("reset"), "\n";} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 202 | 0 | else{print color($color), "$text", color("reset");} | |||||||||
| 203 | } | ||||||||||
| 204 | |||||||||||
| 205 | #prints input with color for debug mode only | ||||||||||
| 206 | # input : $color <-- color to print the text in (ex. 'magenta', 'bright_cyan', 'on_bright_yellow') | ||||||||||
| 207 | # $text <-- the text to print | ||||||||||
| 208 | # output : -- | ||||||||||
| 209 | 0 | ▶ | sub printColorDebug{ | ||||||||
|
Subroutine
printColorDebug: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||
| 210 | 0 | my $self = shift; | |||||||||
| 211 | 0 | my $color = shift; | |||||||||
| 212 | 0 | my $text = shift; | |||||||||
| 213 | |||||||||||
| 214 | 0 | ▶ | if($debug){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 215 | 0 | ▶ | if($color =~ /on_\w+/){print color($color), "$text", color("reset"), "\n";} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 216 | 0 | else{print color($color), "$text", color("reset");} | |||||||||
| 217 | |||||||||||
| 218 | } | ||||||||||
| 219 | } | ||||||||||
| 220 | |||||||||||
| 221 | #prints only if debug mode is on | ||||||||||
| 222 | # input : $text <-- the text to print | ||||||||||
| 223 | # output : -- | ||||||||||
| 224 | 0 | ▶ | sub printDebug{ | ||||||||
|
Subroutine
printDebug: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 225 | 0 | my $self = shift; | |||||||||
| 226 | 0 | my $text = shift; | |||||||||
| 227 | |||||||||||
| 228 | 0 | ▶ | if($debug){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 229 | 0 | print ($text); | |||||||||
| 230 | } | ||||||||||
| 231 | } | ||||||||||
| 232 | |||||||||||
| 233 | #helper function that checks if an array set is in another array | ||||||||||
| 234 | # input : @arr1 <-- the subject array element | ||||||||||
| 235 | # @arr2 <-- the array to look through | ||||||||||
| 236 | # output : boolean <-- 1 if it is in the array, 0 if it isn't | ||||||||||
| 237 | 0 | ▶ | sub setInArr{ | ||||||||
|
Subroutine
setInArr: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 238 | 0 | my $self = shift; | |||||||||
| 239 | 0 | my $arr1_ref = shift; | |||||||||
| 240 | 0 | my $arr2_ref = shift; | |||||||||
| 241 | |||||||||||
| 242 | 0 | my @arr1 = @$arr1_ref; | |||||||||
| 243 | 0 | my @arr2 = @$arr2_ref; | |||||||||
| 244 | |||||||||||
| 245 | #combine the arrays as a string | ||||||||||
| 246 | 0 | my $str1 = join " ", @arr1; | |||||||||
| 247 | 0 | my $str2 = join " ", @arr2; | |||||||||
| 248 | |||||||||||
| 249 | #check for the substring of arr2 in arr1 | ||||||||||
| 250 | 0 | ▶ | if(index($str2, $str1) != -1){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 251 | 0 | return 1; | |||||||||
| 252 | }else{ | ||||||||||
| 253 | 0 | return 0; | |||||||||
| 254 | } | ||||||||||
| 255 | } | ||||||||||
| 256 | |||||||||||
| 257 | #counts how many times a word appears in a set | ||||||||||
| 258 | # input : $word <-- the word to look for in the array | ||||||||||
| 259 | # @arr <-- the array to look through | ||||||||||
| 260 | # output : $num <-- the total number of times the element occurs in the array | ||||||||||
| 261 | 0 | ▶ | sub wordCount{ | ||||||||
|
Subroutine
wordCount: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 262 | 0 | my $self = shift; | |||||||||
| 263 | 0 | my $word = shift; | |||||||||
| 264 | 0 | my $arr_ref = shift; | |||||||||
| 265 | 0 | my @arr = @$arr_ref; | |||||||||
| 266 | |||||||||||
| 267 | 0 | my @idx = getAllIdxs($word, @arr); | |||||||||
| 268 | 0 | my $num = @idx; | |||||||||
| 269 | 0 | return $num; | |||||||||
| 270 | } | ||||||||||
| 271 | |||||||||||
| 272 | #makes a counter for each word in a line | ||||||||||
| 273 | # input : @words <-- the set of words to count | ||||||||||
| 274 | # output : @index_set <-- the set of counter numbers correlating to each word | ||||||||||
| 275 | 0 | ▶ | sub wordIndex{ | ||||||||
|
Subroutine
wordIndex: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 276 | 0 | my $self = shift; | |||||||||
| 277 | 0 | my $words_ref = shift; | |||||||||
| 278 | 0 | my @words = @$words_ref; | |||||||||
| 279 | |||||||||||
| 280 | 0 | my @curWords = (); | |||||||||
| 281 | 0 | my @index_set = (); | |||||||||
| 282 | #for each word in the set count it's frequency | ||||||||||
| 283 | 0 | foreach my $word (@words){ | |||||||||
| 284 | 0 | my $index = wordCount($word, @curWords) + 1; | |||||||||
| 285 | 0 | push(@curWords, $word); | |||||||||
| 286 | 0 | push (@index_set, $index); | |||||||||
| 287 | } | ||||||||||
| 288 | |||||||||||
| 289 | 0 | return @index_set; | |||||||||
| 290 | } | ||||||||||
| 291 | |||||||||||
| 292 | 1 | 1; | |||||||||