ディレクトリ内の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ランキング
-
Perlで特定行から特定行までを...
-
Perlの初心者です。2重ループ...
-
perlでcsvファイルを読む(ダブ...
-
Argument "\\\\n" isn't numeri...
-
CSVファイルのデータの行数を取...
-
VBAでCSVファイルを途中行まで...
-
openした後、closeしないでプロ...
-
VBAでCSVファイルの特定行を書...
-
エクセルVBA コードが同じでも...
-
awkスクリプトでダブルクォーテ...
-
バッチファイルの作り方(CSV→...
-
csvファイルの横方向への改行に...
-
ExcelをCSV書き出す場合のシー...
-
drtファイルはどうしたら開...
-
無料配布の郵便番号自動入力cgi...
-
ファイルからある文字列の個数...
-
close()で例外が投げられる理由
-
Perlで フォルダ内の全て...
-
Perl 特定のフォルダ以外削除
-
JSP URLに表示される拡張子 .jsp
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
Perlで特定行から特定行までを...
-
perlでcsvファイルを読む(ダブ...
-
CSVデータの編集の際の重複チェ...
-
CSVファイルのデータの行数を取...
-
Perlの初心者です。2重ループ...
-
Perlで空白行を削除
-
CSVファイルをperlプログラムで...
-
MATLAB std::exceptionエラー
-
Perlでの重複要素カウントにつ...
-
cgiのログの書き方
-
Argument "\\\\n" isn't numeri...
-
バッチファイルの作り方(CSV→...
-
awkスクリプトでダブルクォーテ...
-
VBAでCSVファイルの特定行を書...
-
DOSコマンドで、標準出力を出力...
-
ExcelをCSV書き出す場合のシー...
-
openした後、closeしないでプロ...
-
close()で例外が投げられる理由
-
batファイルでrenameができませ...
-
VBAでCSVファイルを途中行まで...
おすすめ情報