foreach (my $word = <$inwp>) { $countp{$word}++ ; }

You appear to be just counting the words in the words file Positive2.txt and I can't see where you are spitting the sentences in Positive.txt into words.

Perhaps this will help you progess

#!/usr/bin/perl use strict; # create positive word hash my $pos_words = '/root/Positive2.txt'; open my $fh_in,'<:encoding(utf8)',$pos_words or die "Could not open $pos_words : $!"; my %pos_word = map { s/^\s+|\s+$//g; $_=>1 } <$fh_in>; close $fh_in; # open positive word count output file my $pos_outfile = '/root/wordsbagp.txt'; open my $fh_out,'>',$pos_outfile or die "Could not open $pos_outfile\n"; # read positive sentence file my $pos_text = '/root/Positive.txt'; open my $fh_in,'<:encoding(utf8)',$pos_text or die "Could not open $pos_text : $!"; my @train = <$fh_in>; close $fh_in; # split into 2 arrays 70% / 30% my $offset = int 0.7 * @train; push my @test, splice @train,$offset; my $lineno = 0; for my $line (@train){ ++$lineno; # split sentence into words chomp($line); my @words = split /\b/,$line; # count positive words only my %count = (); for my $word (@words){ ++$count{$word} if exists $pos_word{$word}; } # print results print $fh_out "\nline $lineno: $line\n"; for my $word (sort keys %count){ printf $fh_out " %-10s => %d\n",$word,$count{$word}; } }; close $fh_in; close $fh_out;
poj

In reply to Re^5: problem count the number of words by poj
in thread problem count the number of words by GHMON

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.