
ディレクトリ内のtxtファイル中の英単語数等をカウントしたいのですがわかりません。
PERLを使って、テキストファイル中の段落数、文章数、単語数をカウントしたいと思っていて、splitをつかって頑張っていますがわかりません。
テキストファイル中では、
I stand here today humbled by the task before us, grateful for the trust you've bestowed, mindful of the sacrifices borne by our ancestors.
I thank President Bush for his service to our nation as well as the generosity and cooperation he has shown throughout this transition.
Forty-four Americans have now taken the presidential oath. The words have been spoken during rising tides of prosperity and the still waters of peace. Yet, every so often, the oath is taken amidst gathering clouds and raging storms. At these moments, America has carried on not simply because of the skill or vision of those in high office, but because we, the people, have remained faithful to the ideals of our forebears and true to our founding documents.
という具合に、
段落は改行で、文章の区切りは. で(.と半角スペース2つ)、単語はスペース1つでそれぞれ区切ってあります。
while($data = <>){
chomp($data);
@paragraph = split(/\n/, $data);
}
上記のように、ファイル全体を、改行を区切りに要素に分解することはできています。
が、これをどうやってカウントしていくのかがまったくわかりません。
ご教授願います。
A 回答 (3件)
- 最新から表示
- 回答順に表示
No.3
- 回答日時:
基本的に改行を数えて段落を、ピリオドを数えて文章を、スペース数えて単語を数えたらいいのだと思いますが、
段落は+1して調整して、
単語は「文章の終わりはスペース2つ」「改行があるところはスペースなし」なので調整してあげたらよいかと思います。最後はピリオドで改行なしなら合いそうですが単語の数が一個くらいずれるかもしれないです。
#!/usr/bin/perl
use strict;
use warnings;
my $sentence =
"I stand here today humbled by the task before us, grateful for the trust you've bestowed, mindful of the sacrifices borne by our ancestors.
I thank President Bush for his service to our nation as well as the generosity and cooperation he has shown throughout this transition.
Forty-four Americans have now taken the presidential oath. The words have been spoken during rising tides of prosperity and the still waters of peace. Yet, every so often, the oath is taken amidst gathering clouds and raging storms. At these moments, America has carried on not simply because of the skill or vision of those in high office, but because we, the people, have remained faithful to the ideals of our forebears and true to our founding documents.";
my ( $sent, $word, $para );
my @letters = split //, $sentence;
foreach (@letters) {
if (m/\n/) { $para++ }
if (m/\./) { $sent++ }
if (m/ /) { $word++ }
}
$word = $word + $para * 2 - $sent + 2;
$para++;
print "$para,$sent,$word";
No.2
- 回答日時:
うつし間違いでなければ
> @word=split(/ /,$sentence);
$sentenceについての記述がどこにもないので、$sentenceは空のはずです。
@word=split(/ /,$in1);
$tango = $tango + $#word +1;
ではどうですか?
No.1
- 回答日時:
ん?
$data = <>
は1行しか読みこまないし、それを
chomp($data);
として改行文字を削除しているので
@paragraph = split(/\n/, $data);
としても改行文字がないので分割されないと思うのですが。
$/を変えて全部読み込むようにしているのだとすると、こんどはwhileが意味をなさないです。
また、@paragraphをその都度上書きしていて、最後の行しか残らないです。
まずは、段落とかを考えずに「テキストファイルの行数を数えるには」を考えましょう
$lines =0;
while($data=<>){
$lines ++ ;
}
これで行数が数えられるのは、理解できますか?
では、取り込んだ$dataから計算された値の合計、となると
$nanka = 0;
while($data=<>){
$nanka += &KEISAN($data) ;
}
これも理解できますか?
では、問題の「文章」はどんなKEISANでできるでしょう?「単語」は?
この回答への補足
$lines =0;
$bun=0;
$tango=0;
while($data=<>){
$lines ++ ;
@sentence=split(/. /,$data);
foreach $in1(@sentence){
@word=split(/ /,$sentence);
$bun++;
}
}
これで行数と、文章数がカウントできるようになりました。
それぞれの文章をスペース1つを区切りに分解する、という作業を繰り返すごとに
文章に1を足すという方法です。
しかしこれでは単語数をカウントできません。
$bun++;
$tango = $tango + $#word +1;
でイケるかなと思ったのですが違いました。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
perlでcsvファイルを読む(ダブ...
-
Perl 順位の重複
-
Argument "\\\\n" isn't numeri...
-
Perlを利用してテキストフ...
-
エクセルVBA コードが同じでも...
-
Perl cgiの文字化けを直したい ...
-
drtファイルはどうしたら開...
-
@の配列変数ではなく$のスカラ...
-
JavaでCSVファイルを高速に読む...
-
【エラー】Unrecognized character
-
awkスクリプトでダブルクォーテ...
-
vba dir の相対パス
-
DOSコマンドで、標準出力を出力...
-
Perl エラーログを指定の場所...
-
MATLAB グローバル変数の宣言
-
MATLABのm-fileについて
-
Perlの変数に文字数制限(容量...
-
基本的な質問・・・(パスにつ...
-
FindFirstFileとFindNextFileで...
-
readdir()で得られるファイル・...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
Perlで特定行から特定行までを...
-
データ項目の並び替えについて
-
ファイルの入出力2
-
csvファイル(表計算的な処理)
-
データファイルからのデータの...
-
Cシェルでのファイル編集について
-
番号をつける
-
コマンド上の行間
-
データベースを使わず、テキス...
-
CSVファイルのデータの行数を取...
-
文字化け対策
-
Perlを利用してテキストフ...
-
CGIでのgrep関数においての質問
-
pealを用いたcsvファイルの抽出
-
perlでファイルの比較
-
perl:パターンマッチを使ったif...
-
ファイル分割2
-
MATLAB std::exceptionエラー
-
FTP接続時失敗時、接続先名をcs...
-
Perl 順位の重複
おすすめ情報