blib/lib/Mhouse/Reports.pm untested

filter: Statementstmt 0.0% 0 / 183 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 254 CC 10 SCAR 20.1
CC
10
Cov
0%
report_based_on_config
20.0
read_mhouse_annotations_from_folder
12.0
traverse_folder_tree_jsontxt
12.0
CRAP 7.5
1 package Mhouse::Reports;
2 =pod
3 # PROGRAM: Mhouse_Reports.pm
4 # PURPOSE: Use mhouse business data for scoring and reporting.
5 # DATE CREATED: 2025 07 16
6 # PARAMETERS:
7 # USAGE:
8 my @folder_list = qw(sg ti);
9 my @scored_field_list = qw(e_data communication tourism_sector industry_1 industry_2);
10 my @cumulative_llm_answers = Mhouse_Reports::read_mhouse_scored_data(\@folder_list, \@scored_field_list);
11
12 printf "Total selected before grep: %d\n", scalar @cumulative_llm_answers;
13
14 my @sorted_arr = sort {
15 $b->{e_data} <=> $a->{e_data} || $b->{communication} <=> $a->{communication}
16 } grep {
17 $_->{company_employee_count_range} =~ /^(1-3)$/ && (scalar $_->{e_data} > 0.8 || scalar $_->{communication} > 0.8 )
18 }@cumulative_llm_answers;
19
20 my $annotation_hashref = Mhouse_Reports::read_mhouse_annotations_from_folder("./RESULTS");
21 print Dumper $annotation_hashref ;
22 my $displayed_field_arrref = [qw(energy_sector automation e_data programming)];
23 Mhouse_Reports::output_llm_scored_mhouse_json_annotated(\@sorted_arr, "./displayed_list_8.html", $annotation_hashref, $displayed_field_arrref);
24 =cut
25
26 # binmode(STDOUT, ":utf8");
27 use File::Path qw(make_path);
28 use File::Find;
29 use Cwd 'cwd';
30 use File::Spec;
31 use JSON;
32 use DateTime;
33 use strict;
34
35 our $VERSION = '0.05';
36
37 sub get_timestamp{
38 my $local_time_zone = DateTime::TimeZone->new( name => 'local' );
39 my $dt = DateTime->now(time_zone => $local_time_zone);
40 my $timestamp = sprintf("%4d%02d%02d_%02d%02d_%02d", $dt->year, $dt->month, $dt->day, $dt->hour, $dt->minute, $dt->second );
41 return $timestamp;
42 }
43
44 sub get_array_from_authenticated_json_file{
45 my $input_data_fn = shift;
46 open(my $input_data_fh, '<', $input_data_fn) or die $!;
47 my $document = do { local $/ = undef; <$input_data_fh> };
48 my $parsed_json = decode_json($document);
49 my @arr_to_iterate = @{$parsed_json};
50 my $arr_size = @arr_to_iterate;
51 # print "\@arr_to_iterate size=$arr_size\n";
52 return @arr_to_iterate;
53 }
54
55 sub read_mhouse_llm_scored_data{
56 my ($subfolder_list_ref, $scored_field_ref) = @_;
57 my @cumulative_llm_answers = ();
58 for my $root_dir (@{$subfolder_list_ref}){
59 my @llm_answers;
60 traverse_folder_tree_jsontxt(sub{push @llm_answers, get_array_from_authenticated_json_file(shift);}, $root_dir);
61 printf "%s: total recs = %d\n", $root_dir, scalar @llm_answers;
62 push @cumulative_llm_answers, @llm_answers;
63 };
64
65 foreach my $rec (@cumulative_llm_answers){
66 my ($llm_answer_json_part) = ($rec->{llm_answer} =~ /(\{.*\})/s);
67 my ($address_composed_string) = ($rec->{address_composed_string} =~ /(\{.*\})/s);
68 $rec->{company_employee_count_range} = $rec->{employees};
69
70 $llm_answer_json_part =~ s/\R/ /g;
71 my $data;
72 eval{$data = decode_json($llm_answer_json_part);};
73 if($@){
74 printf "Exception in decode_json : %s %s\n", $rec->{all_preexisting_data}->{address}->{addressRegion}, $rec->{legalName} ;
75 next;
76 }
77 foreach my $kw (@{$scored_field_ref}){
78 $rec->{$kw} = $data->{$kw};
79 }
80 }
81 return @cumulative_llm_answers;
82 }
83
84
85 =pod
86 FUNCTION: read_mhouse_annotations
87 USAGE:
88 my $return_href = read_mhouse_annotations("./RESULTS/20250520_1300_bl_1_3.txt");
89 print Dumper $return_href ;
90 =cut
91
92 sub read_mhouse_annotations{
93 my $anno_fn = shift;
94 my %hash_out;
95 open my $anno_fh, "<", $anno_fn or die $!;
96 binmode($anno_fh, ":utf8");
97
98 while(<$anno_fh>){
99 my ($legal_name, $annotation) = (/Google\s*(\S.*\S)\s*->\s*(.*)$/) or next;
100 next if $annotation =~ /^\s*$/;
101 $hash_out{$legal_name} = $annotation;
102 }
103 return \%hash_out;
104 }
105
106 sub read_mhouse_annotations_from_folder{
107 my $dirname = shift;
108
109 opendir(D, "$dirname") || die "Can't open directory $dirname: $!\n";
110 my @file_list = readdir(D);
111 my %cumul_hash;
112 foreach my $entry (@file_list) {
113 next if $entry =~ /^\.{1,2}$/;
114 next unless $entry =~ /\.txt$/;
115
116 printf "%s\n", $entry;
117 open(my $fh, "<", "$dirname/$entry") or die $!;
118 my $href = read_mhouse_annotations("$dirname/$entry");
119 %cumul_hash = (%cumul_hash, %$href);
120 }
121 return \%cumul_hash;
122 }
123
124 sub get_header{
125 my $config_fn = shift;
126 my $arrref_before = shift;
127 my $arrref_after = shift;
128 open my $config_fh, "<", $config_fn or die "Cannot open $config_fn: $!";
129 my $config_file_contents = do {local $/; <$config_fh>; };
130
131 my $time = sprintf "\nTime generated : %s \n", get_timestamp() ;
132
133 my $freq_table = get_freq_table(@{$arrref_before});
134 my $before_grep = sprintf "Total before grep : %d (%s)\n", scalar @{$arrref_before}, $freq_table;
135 my $displayed = sprintf "Total displayed : %d \n\n", scalar @{$arrref_after};
136 my $header =
137 $config_file_contents .
138 $time .
139 $before_grep .
140 $displayed
141 ;
142 return $header;
143 }
144
145
146 sub output_llm_scored_mhouse_json_annotated{
147 my $arref = shift;
148 my $out_fn = shift;
149 my $anno_hashref = shift;
150 my $displayed_field_arrref = shift;
151 my $config_fn = shift;
152 my $header = shift;
153 my @cumulative_llm_answers = @{$arref};
154 open my $outfh, ">", $out_fn or die $!;
155 binmode($outfh, ":utf8");
156 print $outfh "<pre>";
157
158 open my $config_fh, "<", $config_fn or die "Cannot open $config_fn: $!";
159 my $config_file_contents = do {local $/; <$config_fh>; };
160
161 print $outfh $header;
162 # printf $outfh "Time generated : %s \n", get_timestamp();
163 # printf $outfh "Total before grep : %d (%s)\n", $total_before_grep, $freq_table;
164 # printf $outfh "Total displayed : %d \n\n", scalar @cumulative_llm_answers;
165
166 my $counter = 1;
167 foreach my $company_data(@cumulative_llm_answers){
168 printf $outfh "%5d ", $counter++;
169 foreach my $field (@{$displayed_field_arrref}){
170 printf $outfh "%5s ", $company_data->{$field};
171 }
172 printf $outfh "%10s : %40s : ", $company_data->{company_employee_count_range}, do { my $s = $company_data->{address_composed_string}; chomp $s; $s };
173 my $legalName = $company_data->{legalName};
174 my $legalName_plus = $legalName =~ s/\s/+/gr;
175 my $address_composed_string_plus = $company_data->{address_composed_string} =~ s/\s/+/gr;
176 my $hyperlink_google = sprintf "<a href='https://www.google.com/search?q=+" . $legalName_plus . "+" . $address_composed_string_plus . "' target='_blank'>Google</a>";
177 my $hyperlink_ducky = sprintf "<a href='https://duckduckgo.com/?q=!ducky+%s+%s' target='_blank'>%-50s</a>", $legalName_plus, $address_composed_string_plus, $legalName;
178 printf $outfh "%-10s %s -> %s \n", $hyperlink_google, $hyperlink_ducky, $anno_hashref->{$legalName};
179 }
180 close $outfh;
181 }
182
183 =pod
184 USAGE:
185 # creating a hash
186 my @folder_list_1 = (
187 '../ag',
188 '../bl',
189 );
190 my %returned_hash = Mhouse::Reports::get_hash_by_legalname(\@folder_list_2);
191 my $json = encode_json(\%returned_hash);
192 open(my $fh, '>', 'hash_by_legalname_1.json') or die "Cannot open file: $!";
193 print $fh $json;
194 close $fh;
195
196 # using a hash
197 open(my $fh_in, '<', 'hash_by_legalname_1.json') or die "Cannot open file: $!";
198 my $json_file_contents = do{local $/; <$fh_in>};
199 my $hash_to_use = decode_json($json_file_contents);
200 my $legalName = 'Sedinum Stiftung';
201 print "$legalName : ";
202 print Dumper $hash_to_use->{$legalName};
203
204 =cut
205 sub get_hash_by_legalname{
206 my $folder_list_ref = shift;
207 my @folder_list = @{$folder_list_ref};
208 my @data_extracted = read_mhouse_llm_scored_data(\@folder_list);
209 my %returned_hash = ();
210 foreach my $d (@data_extracted){
211 my ($llm_answer_json_part) = ($d->{llm_answer} =~ /(\{.*\})/s);
212 $llm_answer_json_part =~ s/\R/ /g;
213 my $llm_answer;
214 eval{$llm_answer = decode_json($llm_answer_json_part);};
215 $returned_hash{$d->{legalName}} = {
216 'employees' => $d->{employees},
217 'personnel' => $llm_answer -> {personnel},
218 'e_data' => $llm_answer -> {e_data},
219 } ;
220 }
221 return %returned_hash;
222 }
223
224
225
226 sub get_freq_table{
227 my @llm_scored_arr = @_;
228 my %counts;
229 foreach my $rec (@llm_scored_arr) {
230 $counts{ $rec->{employees} }++;
231 }
232 return join ", ", map { "$_ => $counts{$_}" } sort keys %counts;
233 }
234
235 =pod
236 FUNCTION: traverse_folder_tree_jsontxt
237 USAGE:
238 my @cumulative_llm_answers = ();
239 traverse(sub{push @cumulative_llm_answers, get_array_from_authenticated_json_file(shift);}, '../zh');
240 printf "Total company count = %d\n", scalar @cumulative_llm_answers;
241 =cut
242 sub traverse_folder_tree_jsontxt{
243 my ($callback, $root_dir) = @_;
244 my $cwd = cwd();
245 find(
246 sub {
247 return unless -f $_;
248 return unless $_ =~ /\.json\.txt$/;
249 my $fn = $File::Find::name;
250 my $full = File::Spec->rel2abs($fn, $cwd);
251 $callback->($full);
252 },
253 $root_dir
254 );
255 }
256
257 sub report_based_on_config{
258 my $config_fn = shift;
259 my $config = do $config_fn;
260 die "Config error: $@" if !$config;
261
262 my $sort_logic = eval $config->{sort_logic};
263 die "Sort logic error: $@" if !$sort_logic;
264
265 my $grep_logic = eval $config->{grep_logic};
266 die "Grep logic error: $@" if !$grep_logic;
267
268 my @folder_list = @{$config->{data_folder_list}};
269 my $displayed_scores_arrref = $config->{displayed_scores};
270 my $annotation_folder = $config->{annotation_folder};
271
272 my @scored_field_list = qw(personnel e_data documentation programming automation sysadmin energy_sector e_commerce logistics_supply construction agri_tech ar_vr industry_1 industry_2);
273 my @cumulative_llm_answers = read_mhouse_llm_scored_data(\@folder_list, \@scored_field_list);
274 printf "Total selected before grep: %d\n", scalar @cumulative_llm_answers;
275 my @sorted_arr = sort { $sort_logic->($a, $b) } grep { $grep_logic->($_) } @cumulative_llm_answers;
276 my $annotation_hashref = read_mhouse_annotations_from_folder($annotation_folder);
277 my $out_fn = "displayed_list_" . get_timestamp() . ".html";
278 my $header = get_header($config_fn, \@cumulative_llm_answers, \@sorted_arr);
279 output_llm_scored_mhouse_json_annotated(\@sorted_arr, $out_fn, $annotation_hashref, $displayed_scores_arrref, $config_fn, $header);
280 }
281
282
283 1;