Phoebus2000 has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I am trying to process all text files in a directory through Lingua Fathom and output it into a spreadsheet.
I have adapted the code into what I thought was right but I have a very odd error. All files are read, but only the files I have already processed individually (as a test) get their text read for readability. The code sees all files but only outputs for files that have already been read, yet if I process individually it can read the text and will then register it next time I do a directory scan.
Any ideas what I need to change?
Thanks,#! /usr/local/bin/perl # use warnings; use Path::Class; use autodie; # die if problem reading or writing a file use Lingua::EN::Fathom; use Excel::Writer::XLSX; my $workbook = Excel::Writer::XLSX->new('output.xls'); my $worksheet = $workbook->add_worksheet(); my $file_handle = $worksheet->write(); my $text = new Lingua::EN::Fathom; opendir(DH, "./new_text"); my @files = readdir(DH); closedir(DH); foreach my $file (@files) { $text->analyse_file($file); $accumulate = 1; $text->analyse_block($text_string,$accumulate); $num_chars = $text->num_chars; $num_words = $text->num_words; $percent_complex_words = $text->percent_complex_words; $num_sentences = $text->num_sentences; $num_text_lines = $text->num_text_lines; $num_blank_lines = $text->num_blank_lines; $num_paragraphs = $text->num_paragraphs; $syllables_per_word = $text->syllables_per_word; $words_per_sentence = $text->words_per_sentence; %words = $text->unique_words; foreach $word ( sort keys %words ) { print("$words{$word} :$word\n"); } $fog = $text->fog; $flesch = $text->flesch; $kincaid = $text->kincaid; print($text->report); # Create a format for the book my $header = $workbook->add_format(); $header->set_bold(); my $percent_style = $workbook->add_format(); $percent_style->set_num_format('%'); # Add the line to the file foreach my $search ($text) { $worksheet->write(0, 0, "Filename",$header); $worksheet->write($count, 0, $file); $worksheet->write(0, 1, "Fog index",$header); $worksheet->write($count, 1, $fog); $worksheet->write(0, 2, "Flesch index",$header); $worksheet->write($count, 2, $flesch); $worksheet->write(0, 3, "Flesch-Kincaid index",$header); $worksheet->write($count, 3, $kincaid); $worksheet->write(0, 4, "Number of characters",$header); $worksheet->write($count, 4, $num_chars); $worksheet->write(0, 5, "Number of words",$header); $worksheet->write($count, 5, $num_words); $worksheet->write(0, 6, "Percent complex words",$header); $worksheet->write($count, 6, $percent_complex_words); $worksheet->write(0, 7, "Number of sentences",$header); $worksheet->write($count, 7, $num_sentences); $worksheet->write(0, 8, "Number of text lines",$header); $worksheet->write($count, 8, $num_text_lines); $worksheet->write(0, 9, "Number of blank lines",$header); $worksheet->write($count, 9, $num_blank_lines); $worksheet->write(0, 10, "Number of paragraphs",$header); $worksheet->write($count, 10, $num_paragraphs); $worksheet->write(0, 11, "Number of syllables per word",$header); $worksheet->write($count, 11, $syllables_per_word); $worksheet->write(0, 12, "Number of words per sentence",$header); $worksheet->write($count, 12, $words_per_sentence); $count++; } } $workbook->close();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Scanning a directory's files for readability
by Cristoforo (Curate) on Jan 18, 2014 at 19:35 UTC | |
by Phoebus2000 (Initiate) on Jan 19, 2014 at 12:14 UTC | |
|
Re: Scanning a directory's files for readability
by kcott (Archbishop) on Jan 19, 2014 at 09:01 UTC | |
by Cristoforo (Curate) on Jan 20, 2014 at 00:50 UTC | |
by choroba (Cardinal) on Jan 20, 2014 at 00:58 UTC | |
by Cristoforo (Curate) on Jan 20, 2014 at 02:26 UTC | |
by kcott (Archbishop) on Jan 20, 2014 at 12:41 UTC | |
|
Re: Scanning a directory's files for readability (Path::Class)
by Anonymous Monk on Jan 18, 2014 at 23:32 UTC |