![](http://oshiete.xgoo.jp/images/v2/pc/qa/question_title.png?5a7ff87)
ディレクトリ内の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で質問しましょう!
似たような質問が見つかりました
- 英語 この英文は平易な反面格調高いですか? 1 2023/01/15 12:04
- 英語 この英文は格調高いのでしょうか? 3 2022/06/03 18:55
- その他(SNS・コミュニケーションサービス) 自分のpcがハッキングされたようなメールが来たのですがどうすればいいですか? 4 2022/10/02 16:14
- 英語 できるだけ直訳で英語の翻訳をお願いします。(英語→日本語) 1 2022/10/15 20:59
- 英語 英文の添削をお願いします 4 2023/05/23 11:10
- 大学受験 英作文の添削をお願いしたいです。 2 2022/08/19 20:37
- 公的扶助・生活保護 保証人? 1 2022/05/17 22:42
- 英語 下記はなんていってますか? こんにちは以降、訳していただけたら助かります。 Hello, We’re 1 2023/01/14 00:09
- TOEFL・TOEIC・英語検定 英検準1級英作文の添削をお願いします。 ポイントはCurriculumとGlobalizationを 4 2023/05/29 11:33
関連するカテゴリからQ&Aを探す
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
パイソンでCSVファイル場所を指...
-
Perl初心者です。同一データを...
-
VBAでCSVファイルを途中行まで...
-
ExcelをCSV書き出す場合のシー...
-
C言語でファイル名を取得
-
ダイアモンド演算子<>に対するb...
-
VBAでCSVファイルの特定行を書...
-
ListBoxのデータを高速でファイ...
-
does not map to shiftjis は解...
-
ファイル名の変更
-
pushをすると行ができる
-
perlでの三次元配列の作り方
-
[perl5.8] SJISから読み込んだ~と
-
指定の行数目から行を抽出する
-
openした後、closeしないでプロ...
-
ファイル全てを .xlsm に変更し...
-
C言語のバイナリモードでのfsca...
-
フォルダーの深さの限界
-
batファイルでrenameができませ...
-
csvで順番の入れ替え
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
Perlで特定行から特定行までを...
-
Perlの初心者です。2重ループ...
-
ディレクトリ内のtxtファイル中...
-
Perlで空白行を削除
-
perlでcsvファイルを読む(ダブ...
-
Perlを利用してテキストフ...
-
ファイルの3行目までを出力したい
-
foreachの制御
-
Java CSVファイルをカンマごと...
-
Argument "\\\\n" isn't numeri...
-
エラーチェック、ファイルに特...
-
CSVデータの編集の際の重複チェ...
-
FTP接続時失敗時、接続先名をcs...
-
CSVファイルのデータの行数を取...
-
cgiのログの書き方
-
MATLAB std::exceptionエラー
-
VBAでCSVファイルを途中行まで...
-
awkスクリプトでダブルクォーテ...
-
VBAでCSVファイルの特定行を書...
-
バッチファイルの作り方(CSV→...
おすすめ情報