LoginSignup
0
0

More than 5 years have passed since last update.

Fortranで何かを作りたい 没

Last updated at Posted at 2018-11-22

日課のネットサーフィンにて~

スタート.png
やるぞ.png

というわけで、Fortran90を使って人工無能の作成を目指します

偉大なるVIP

設計

人工無脳の祖であるElizaを理想とします

必要なものを大まかに書き出すと、

口のプログラム

受け答えを行う工程

脳みそのプログラム

言われた言葉を吟味する工程
返答を考える工程

この三つの工程を実装する必要がありそう・・・ありそうじゃない?

無脳ないのに脳みそのプログラムとはこれいかに?

( ^ω^)・・・正直、無理難題じゃな

Fortranで自然言語処理ってできるん?みたいな状態です

何をどうすればいいのかすら手探りです

それでもやるんですけどネ!

受け答えを行う工程

Elisa.f90
implicit none
integer(8) i,c,n,m
character(140) L
character(140),allocatable :: W(:)
write(*,*) "My name is Elisa!"
write(*,*) "Please tell me various things!"
open(87, file='word.txt', form='formatted', status='old')
n=0
do
  read(87,'(A)',end=100)
  n=n+1
end do
100 close(87)
allocate (W(n))
read(*,'(A)') L
open(87, file='word.txt', form='formatted', status='old')
do i=1,n
   read(87,'(A)',end=200) W(i)
end do
200 close(87)
m=0
do i=1,n
   if (W(i)==L) then
       m=i
   end if
end do
if (0<m .and. m<=n) then
    write(*,*) "Don't make fun of me."
else
    open(87, file='word.txt', form='formatted', access='append')
    write(87,'(A)') L
    close(87)
    write(*,*) "I might try this time."
end if
end

やっていることは酷く単純です

格言を入力→テキスト内から格言を捜索して有無の確認→返答を行う

この人工無脳ちゃんの名前はElizaパイセンからあやかって『Elisa』にしました

とは言え、便宜上の名称なので他に良い名前があれば変えます

次にこのままでは、毎回毎回exeファイルを開かなければなりません

そのため、プログラムを任意のタイミングで終了させるようにします

roop.f90
program Elisa
implicit none
integer(8) i,c,n,m
character(140) L
character(4) S
character(140),allocatable :: W(:)
write(*,*) "My name is Elisa!"
write(*,*) "Please tell me various things!"
S="next"
do while(S=="next")
open(87, file='word.txt', form='formatted', status='old')
n=0
do
  read(87,'(A)',end=100)
  n=n+1
end do
100 close(87)
allocate (W(n))
read(*,'(A)') L
open(87, file='word.txt', form='formatted', status='old')
do i=1,n
   read(87,'(A)',end=200) W(i)
end do
200 close(87)
m=0
do i=1,n
   if (W(i)==L) then
       m=i
   end if
end do
if (0<m .and. m<=n) then
    write(*,*) "Don't make fun of me."
else
    open(87, file='word.txt', form='formatted', access='append')
    write(87,'(A)') L
    close(87)
    write(*,*) "I might try this time."
end if
deallocate(W) !超重要
read(*,*) S
end do
end program

《while do》で挟んで《deallocate(W)》を添えただけです

最初《deallocate(W)》はなかったんですけど、Fortranは配列の上書きができないようで、

この文言が無いと二回目に突入する途中で配列を作成できない、と突っ返されます

これで簡単なオウム返しのプログラムの完成です

次は無限のサル定理やらマルコフ連鎖などを利用して文章を作成する工程を組んでいきたいと思います

以上!

と、意気込んだものの

     *      * 
  *  無理です   +   
     n ∧_∧ n 
 + (ヨ(* ´∀`)E) 
      Y     Y    *

文節をざく切りにする方法をどう足掻いても思いつけなかったのでお蔵入りです

Fortranの不得意分野で戦おうとすればこうなるのは自明の理ですな・・・

大規模演算を利用した何か、が思いつくまでこの企画は没とします

0
0
0

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
0