Python

pythonでアルファベットの出現数をカウントするプログラム例


pythonで英文の中にA~Zのアルファベットがそれぞれ何個ずつあるのかを調べるためのコードを紹介する。

使用する関数などの方針

短い文章ならともかく、長い英文となるとエディタに張り付けるとめんどくさいので、メモ帳に英文を張り付けて、それをpythonで読みだす方法をとる。この際にopen関数を使用する。

例えば 「f = open(“テキストファイル名 “,”r”,)」でfに読み込みモードでテキストファイルのデータを読み込むことができる(テキストファイルをオープンする)。

その後、読みだした文章に対しcount関数を使ってアルファベットを数えていく。count関数は「str.count(カウントしたい文字列)」と書けば、文章の中からカウントしたい文字文字列の数を数えて返してくれる。

実際のpythonコード

counter = [0] * 26   #26個の要素すべてが0の配列を作る

f = open("Sample.txt", "r" ,encoding='utf-8')    #テキストファイルを読み込みモードで開く
lines = f.readlines()      #すべての行を読みだす

for line in lines:    #line にlinesの行を一行ずつ読み出し、すべての行を読み終わるまでループ
    counter[0] += line.count('a') + line.count('A')
    counter[1] += line.count('b') + line.count('B')
    counter[2] += line.count('c') + line.count('C')
    counter[3] += line.count('d') + line.count('D')
    counter[4] += line.count('e') + line.count('E')
    counter[5] += line.count('f') + line.count('F')
    counter[6] += line.count('g') + line.count('G')
    counter[7] += line.count('h') + line.count('H')
    counter[8] += line.count('i') + line.count('I')
    counter[9] += line.count('j') + line.count('J')
    counter[10] += line.count('K') + line.count('K')
    counter[11] += line.count('l') + line.count('L')
    counter[12] += line.count('m') + line.count('M')
    counter[13] += line.count('n') + line.count('N')
    counter[14] += line.count('o') + line.count('O')
    counter[15] += line.count('p') + line.count('P')
    counter[16] += line.count('q') + line.count('Q')
    counter[17] += line.count('r') + line.count('R')
    counter[18] += line.count('s') + line.count('S')
    counter[19] += line.count('t') + line.count('T')
    counter[20] += line.count('u') + line.count('U')
    counter[21] += line.count('v') + line.count('V')
    counter[22] += line.count('w') + line.count('W')
    counter[23] += line.count('x') + line.count('X')
    counter[24] += line.count('y') + line.count('Y')
    counter[25] += line.count('z') + line.count('Z')

f.close()   #ファイルをクローズする

sum = 0

for i in counter:      #合計の計算
    sum += i
    
print(counter)
print()

print("a =",counter[0])
print("b =",counter[1])
print("c =",counter[2])
print("d =",counter[3])
print("e =",counter[4])
print("f =",counter[5])
print("g =",counter[6])
print("h =",counter[7])
print("i =",counter[8])
print("j =",counter[9])
print("k =",counter[10])
print("l =",counter[11])
print("m =",counter[12])
print("n =",counter[13])
print("o =",counter[14])
print("p =",counter[15])
print("q =",counter[16])
print("r =",counter[17])
print("s =",counter[18])
print("t =",counter[19])
print("u =",counter[20])
print("v =",counter[21])
print("w =",counter[22])
print("x =",counter[23])
print("y =",counter[24])
print("z =",counter[25])

print()
print("合計文字数=",sum)

 

3行目の「sample.txt」というのは、英文をコピーしてあるテキストファイルである。コードファイルと同じディレクトリに置いておく。

3行目の「f = open(“Sampe.txt”, “r” ,encoding=’utf-8′)」における「,encoding=’utf-8′」の部分は必須ではないが、入れないと数字が入っている文章などでバグるっぽいので入れておく(ここでは関係ないが漢字が入っていてもバグる)。しっかりエンコーディングしておくほうがよい。

6行目から32行目までは、文章を一行ずつ読み出して、A~Zの数を数え上げている。また、大文字と小文字は別々にカウントして、最終的には合算している。

所々に入っているprint()は、表示結果を見やすくするための改行である。

なお、数字や空白、記号などはノーカウントとなる。

実際にカウントしてみる

上記のコードを使って実際にアルファベットの文字数をカウントしてみよう。

今回使用する英文は、NHKの「世界へ発信!ニュースで英語術」というサイトから、本日2月27日に公開された「中国 全人代延期へ(CHINA LIKELY TO DELAY ANNUAL CONGRESS)」というニュースの英文を使ってみよう。全文は以下の通りである。

We start in Beijing, where an outbreak of a new coronavirus is having a political ripple effect. President Xi Jinping has likely already decided to postpone next month’s National People’s Congress — the annual meeting where Chinese leadership lays out key policies.
The state-run Xinhua News Agency says members of the body’s Standing Committee will discuss postponing the congress on Monday next week. Observers suggest leadership would find it impossible to gather nearly 3,000 delegates from across the country, amid the threat of the spreading coronavirus.
Still, delaying the meeting would be extremely unusual. The situation could also affect President Xi’s plans to visit Japan in early April.
The latest figures show 1,868 people in mainland China have died after contracting the virus. The total number of infections has now surpassed 72,000.

出力結果

[52, 8, 20, 24, 87, 13, 18, 27, 55, 3, 0, 34, 14, 54, 44, 23, 0, 36, 56, 57, 21, 7, 11, 6, 13, 0]

a = 52
b = 8
c = 20
d = 24
e = 87
f = 13
g = 18
h = 27
i = 55
j = 3
k = 0
l = 34
m = 14
n = 54
o = 44
p = 23
q = 0
r = 36
s = 56
t = 57
u = 21
v = 7
w = 11
x = 6
y = 13
z = 0

合計文字数= 683

しっかりと文字がカウントできている。

結果を見ると、アルファベットによって出現数にばらつきがある事がわかる。この性質は換字式暗号を破る際に利用できる。いずれこの辺のことも記事にしたい。

なお、繰り返す「合計文字数」には数字や空白、記号などは含まれない。