Readability

以下のテキストを理解するために必要なおおよそのグレード (等級レベル) を計算するプログラムを実装します。

$ python readability.py
Text: Congratulations! Today is your day. You're off to Great Places! You're off and away!
Grade 3

始め方

VS Codeを開きます。

ターミナルウィンドウ内をクリックすることから始めて、それからcdを実行します。
その後プロンプトは次のようになっていることがわかります。

$

ターミナルウィンドウの内側をクリックし、次のように入力します。

wget https://cdn.cs50.net/2021/fall/psets/6/sentimental-readability.zip

その後にEnterを押すと、sentimental-readability.zipというZIPがあなたのCodespaceにダウンロードされます。wgetと次のURLの間にあるスペースや、その他の文字を見落とさないように注意してください。

次に

unzip sentimental-readability.zip

を実行して、sentimental-readabilityというフォルダを作成します。
ZIPファイルは不要になったため、

rm sentimental-readability.zip

を実行し、プロンプトで “y “に続いてEnterで応答すると、ダウンロードしたZIPファイルが削除されます。

次に

cd sentimental-readability

の後にEnterを押して、そのディレクトリに移動する(つまり、開く)。これでプロンプトは以下のようになります。

sentimental-readability/ $

lsを実行するとreadability.pyが表示されます。問題が発生した場合は、同じ手順をもう一度実行して、どこが間違っていたかを判断できるかどうかを確認してください。

仕様

  • readability.pyというファイルに、まずユーザにテキストの入力を求め、次にColeman-Liauの式に従ってテキストのグレードレベルを出力するプログラムを記述します。Problem set 2と同じですが、今回のプログラムはPythonで記述する必要があります。
    • Coleman-Liau指数は0.0588 * L - 0.296 * S - 15.8として計算されます。ここで、Lは100語あたりの平均字数、Sは100語あたりの平均文数です。
  • CS50ライブラリからget_stringを使用してユーザの入力を取得し、printを使用して回答を出力します。
  • プログラムは、テキスト内の文字、単語、および文の数をカウントする必要があります。文字は、aからzまでの任意の小文字またはAからZまでの任意の大文字であり、スペースで区切られた任意の文字列は単語としてカウントされ、ピリオド、感嘆符、または疑問符が出現すると文の終わりを示します。
  • プログラムは、出力"Grade X"として出力する必要があります。ここで、XはColeman-Liau式によって計算され、最も近い整数に丸められたグレードです。
  • 作成されたインデックス番号が16以上 (大学4年生以上) の場合は"Grade 16+"と出力され、正確なインデックス番号は出力されません。インデックス番号が1より小さい場合、プログラムは "Before Grade 1" を出力します。

使い方

プログラムは次の例のように動作するはずです。

$ python readability.py
Text: Congratulations! Today is your day. You're off to Great Places! You're off and away!
Grade 3

テスト

この問題についてはcheck50を使用できますが、次の各項目については、最初に自分でコードをテストすることをお勧めします。

  • プログラムをpython readability.pyとして実行し、入力を求めるプロンプトが表示されるのを待ちます。以下のテキストを入力します。「One fish. Two fish. Red fish. Blue fish.」 Enterキーを押します。プログラムはBefore Grade 1を出力するはずです。
  • プログラムをpython readability.pyとして実行し、入力を求めるプロンプトが表示されるのを待ちます。以下のテキストを入力します。「Would you like them here or there? I would not like them here or there. I would not like them anywhere.」 Enterキーを押します。プログラムはGrade 2を出力するはずです。
  • プログラムをpython readability.pyとして実行し、入力を求めるプロンプトが表示されるのを待ちます。以下のテキストを入力します。「Congratulations! Today is your day. You're off to Great Places! You're off and away!」 と入力します。Enterキーを押します。プログラムはGrade 3を出力するはずです。
  • プログラムをpython readability.pyとして実行し、入力を求めるプロンプトが表示されるのを待ちます。以下のテキストを入力します。「Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard.」 Enterキーを押します。プログラムはGrade 5を出力するはずです。
  • プログラムをpython readability.pyとして実行し、入力を求めるプロンプトが表示されるのを待ちます。以下のテキストを入力します。「In my younger and more vulnerable years my father gave me some advice that I've been turning over in my mind ever since.」 Enterキーを押します。プログラムはGrade 7を出力するはずです。
  • プログラムをpython readability.pyとして実行し、入力を求めるプロンプトが表示されるのを待ちます。以下のテキストを入力します。「Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, "and what is the use of a book," thought Alice "without pictures or conversation?"」 Enterキーを押します。プログラムはGrade 8を出力するはずです。
  • プログラムをpython readability.pyとして実行し、入力を求めるプロンプトが表示されるのを待ちます。以下のテキストを入力します。「When he was nearly thirteen, my brother Jem got his arm badly broken at the elbow. When it healed, and Jem's fears of never being able to play football were assuaged, he was seldom self-conscious about his injury. His left arm was somewhat shorter than his right; when he stood or walked, the back of his hand was at right angles to his body, his thumb parallel to his thigh.」 Enterキーを押します。プログラムはGrade 8を出力するはずです。
  • プログラムをpython readability.pyとして実行し、入力を求めるプロンプトが表示されるのを待ちます。以下のテキストを入力します。「There are more things in Heaven and Earth, Horatio, than are dreamt of in your philosophy.」 Enterキーを押します。プログラムはGrade 9を出力するはずです。
  • プログラムをpython readability.pyとして実行し、入力を求めるプロンプトが表示されるのを待ちます。以下のテキストを入力します。「It was a bright cold day in April, and the clocks were striking thirteen. Winston Smith, his chin nuzzled into his breast in an effort to escape the vile wind, slipped quickly through the glass doors of Victory Mansions, though not quickly enough to prevent a swirl of gritty dust from entering along with him.」 Enterキーを押します。プログラムはGrade 10を出力するはずです。
  • プログラムをpython readability.pyとして実行し、入力を求めるプロンプトが表示されるのを待ちます。以下のテキストを入力します。「A large class of computational problems involve the determination of properties of graphs, digraphs, integers, arrays of integers, finite families of finite sets, boolean formulas and elements of other countable domains.」 Enterキーを押します。プログラムはGrade 16+を出力するはずです。

check50を使用して以下を実行し、コードの正確さを評価してください。ただし、テストは必ず自分で行ってください。

check50 cs50/problems/2022/x/sentimental/readability

以下を実行し、style50を使用してコードのスタイルを評価します。

style50 readability.py

提出方法

ターミナルで、以下のコマンドを実行して提出してください。

submit50 cs50/problems/2022/x/sentimental/readability