0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

参考文献

はじめての機械学習
著者 小高 知宏
発売日 2011/04/22

準備

自分のパソコンの環境で実行します。

[Python 3.10.0]を使用します。

プログラム

n_gram1.py
import sys

N = 32

def set_last_ch(n, ch, last_ch):
    for i in range(n-2, -1, -1):
        last_ch[i+1] = last_ch[i]
    last_ch[0] = ch

def print_ngram(n, last_ch):
    for i in range(n-1, -1, -1):
        print(last_ch[i], end='')
    print()

def main():
    if len(sys.argv) != 2:
        print("使い方: python ngram.py (Nの値) < (入力ファイル名)> (出力ファイル名)", file=sys.stderr)
        sys.exit(1)

    n = int(sys.argv[1])
    if n < 1 or n >= N:
        print("Nの値が不適切です", file=sys.stderr)
        sys.exit(1)

    last_ch = [' '] * N
    while True:
        ch = sys.stdin.read(1)
        if not ch:
            break
        if ch != '\n':
            set_last_ch(n, ch, last_ch)
            print_ngram(n, last_ch)

if __name__ == "__main__":
    main()

解析対象文献

「Alice’s Adventures in Wonderland」
「by Lewis Carroll」
この著書をテキストファイル化したものを解析対象としました。
また、テキストファイル名を「Alice’s_Adventures_in_Wonderland.txt」としました。
解析は5-gramとし、出力後のファイル名を「alice5gram.txt」としました。

実行結果

console
> $ python n_gram1.py 5 <Alice’s_Adventures_in_Wonderland.txt> alice5gram.txt 
> $ cat alice5gram.txt
    *
   **
  ***
 *** 
*** S
** ST
* STA
 STAR
START
TART 
ART O
RT OF
T OF 
 OF T
OF TH
F THE
 THE 
THE P
HE PR
E PRO
 PROJ
PROJE
ROJEC
OJECT
JECT 
ECT G
CT GU
T GUT
 GUTE
GUTEN
UTENB
TENBE
ENBER
NBERG
BERG 
ERG E
RG EB
G EBO
 EBOO
EBOOK
BOOK 
OOK A
OK AL
K ALI
 ALIC
ALICE
LICE'
ICE'S
CE'S 
E'S A
'S AD
S ADV
 ADVE
ADVEN
DVENT
VENTU
ENTUR
NTURE
TURES
URES 
RES I
ES IN
S IN 
 IN W
IN WO
N WON
 WOND
WONDE
ONDER
NDERL
DERLA
ERLAN
RLAND
LAND 
AND *
ND **
D ***
 ***c
***co
**cov
*cove
cover
overA
verAl
erAli
rAlic
Alice
lice’
ice’s
ce’s 
e’s A
’s Ad
s Adv
 Adve
Adven
dvent
ventu
entur
nture
tures
ures 
res i
es in
s in 
 in W
in Wo
n Won
 Wond
Wonde
onder
nderl
derla
erlan
rland
landb
andby
ndby 
dby L
by Le
y Lew
 Lewi
Lewis
ewis 
wis C
is Ca
s Car
 Carr
Carro
arrol
rroll
rollT
ollTH
llTHE
lTHE 
THE M
HE MI
E MIL
 MILL
MILLE
ILLEN
LLENN
LENNI
ENNIU
NNIUM
NIUM 
IUM F
UM FU
M FUL
 FULC
FULCR
ULCRU
LCRUM
CRUM 
RUM E
UM ED
M EDI
 EDIT
EDITI
DITIO
ITION
TION 
ION 3
ON 3.
N 3.0
 3.0C
3.0Co
.0Con
0Cont
Conte
onten
ntent
tents
entsC
ntsCH
tsCHA
sCHAP
CHAPT
HAPTE
APTER
PTER 
TER I
ER I.
R I.	
 I.	D
I.	Do
.	Dow
	Down
Down 
own t
wn th
n the
 the 
the R
he Ra
e Rab
 Rabb
Rabbi
abbit
bbit-
bit-H
it-Ho
t-Hol
-Hole
HoleC
oleCH
leCHA
eCHAP
CHAPT
HAPTE
APTER
PTER 
TER I
ER II
R II.
 II.	
II.	T
I.	Th
.	The
	The 
The P
he Po
e Poo
 Pool
Pool 
ool o
ol of
l of 
 of T
of Te
f Tea
 Tear
Tears
earsC
arsCH
rsCHA
sCHAP
CHAPT
HAPTE
APTER
PTER 
TER I
ER II
R III
 III.
III.	
II.	A
I.	A 
.	A C
	A Ca
A Cau
 Cauc
Caucu
aucus
ucus-
cus-R
...................................
...................................
...................................
...................................
...................................
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?