нейросеть получается нехилая
Graphics 640,480,24,2
ClsColor(64,64,255)
font=LoadFont("system")
SetFont(font)
Global WORD_NUM=0
Global MAX_LINK=100
Dim knowledge$(100000)
Dim link(10000,MAX_LINK)
Dim weights#(10000)
Global weight#=0.5
Global LastError#=1
Global Smoothing#=0.5
Function Ns_train(v1#,v2#)
i=0
While((LastError > Smoothing Or LastError < -Smoothing) And i<=25)
i=i+1
Train(v1,v2)
;AddToChat "iteration "+i+" error "+LastError
If LastError<1 Return
Wend
If(weights(v1)<=0.1 Or weights(v1)>=0.9)weights(v1)=0.5
End Function
Function ProcessInputData#(input_#)
Return input_*weight;
End Function
Function RestoreInputData#(output#)
Return output/weight;
End Function
Function Train(input_#, exResult#)
aRes#=input_*weight
LastError=exResult - aRes
correction#=(lasterror/aRes)*Smoothing
weight=weight+correction
End Function
;------------------------------------
Function String_To_Vec(s$)
For i=1 To WORD_NUM
If knowledge(i)=s$ Return i
Next
Return 0
End Function
Function Vec_To_String$(v)
If v>=1 And v<=WORD_NUM Return knowledge(v);
Return ""
End Function
;------------------------------------
f=FileOpen("link.txt")
For j=1 To 10000
For i=1 To MAX_LINK
If Eof(f) Then Exit
link(j,i)=ReadInt(f)
Next
If Eof(f) Then Exit
Next
CloseFile(f)
f=FileOpen("weights.txt")
For i=1 To 10000
weights(i)=ReadFloat(f)
Next
CloseFile(f)
Function Add_Link(v1,v2)
For i=1 To MAX_LINK
If link(v1,i)=v2 Then Return
Next
For i=1 To MAX_LINK
If link(v1,i)=0 Then link(v1,i)=v2: Return
Next
End Function
Function Link_Get_Random(v)
For i=1 To MAX_LINK
If link(v,i)=0 Exit
Next
i=i-1
Return link(v,Rand(1,i))
End Function
Include "chat.bb"
Cls()
DrawChat()
Include "text.bb"
Global prev_word$=" "
While(1)
weight#=0.5
LastError#=1
Smoothing#=0.5
ReadKnowledge(0)
SeedRnd(MilliSecs())
Locate(0,0)
inp$=Input("> ")
TextBegin(inp)
g_text_word=prev_word
w1$=g_text_word
While(g_text_word<>"")
AddToKnowledgeWord(w1)
TextReadWord()
w2$=g_text_word
AddToKnowledgeWord(w2)
Add_Link(String_To_Vec(w1),String_To_Vec(w2))
w1=w2
weight=weights(String_To_Vec(w1))
Ns_train(String_To_Vec(w1),Link_Get_Random(String_To_Vec(w1)))
weights(String_To_Vec(w1))=weight
Wend
f=FileOpen("link.txt")
For j=1 To 10000
For i=1 To MAX_LINK
WriteInt(f,link(j,i))
Next
Next
CloseFile(f)
f=FileOpen("weights.txt")
For i=1 To 10000
WriteFloat(f,weights(i))
Next
CloseFile(f)
AddToChat("> "+inp)
txt$=""
TextBegin(inp)
TextReadWord()
r=Rand(1,7)
For i=1 To r
If(g_text_word<>"")prev_word=g_text_word
Ns_train(String_To_Vec(prev_word),Link_Get_Random(String_To_Vec(g_text_word)))
weight=weights(String_To_Vec(prev_word))
g_text_word=Vec_To_String(ProcessInputData(String_To_Vec(prev_word)))
If(g_text_word="") g_text_word=Vec_To_String(Link_Get_Random(String_To_Vec(prev_word)))
txt$=txt+g_text_word+" "
TextReadWord()
Next
f=FileOpen("weights.txt")
For i=1 To 10000
WriteFloat(f,weights(i))
Next
CloseFile(f)
AddToChat(txt)
Cls()
DrawChat()
Wend
WaitKey()
;--------------------------------------------------------------------------------
Function AddToKnowledgeWord(s$)
If Word_Qu(s)>0 Then
;AddToChat("word already exist")
Return
EndIf
If(s = "") Return
f=FileOpen("knowledge.txt")
SeekFile(f, FileSize("knowledge.txt"));
WriteLine(f, s)
WORD_NUM=WORD_NUM+1
CloseFile(f);
ReadKnowledge(g_theme)
;AddToChat("word added")
End Function
Function ReadKnowledge(theme)
WORD_NUM=0
g_theme=theme
file1=FileOpen("knowledge.txt");
While(Eof(file1)<>1)
WORD_NUM=WORD_NUM+1
knowledge(WORD_NUM) = ReadLine(file1);
Wend
CloseFile(file1)
End Function
Function Word_Qu(w$)
q=0
ReadKnowledge(0)
For i=1 To WORD_NUM
If w=knowledge(i) q=q+1
Next
Return q
End Function
Function FileOpen(fn$)
file1 = OpenFile(fn$);
If(file1=0)file1=WriteFile(fn$);
Return file1;
End Function
Global g_text_pos = 0;
Global g_text$;
Global g_text_word$;
Function TextBegin(text_$)
g_text=text_
g_text_pos = 1
g_text_word=""
End Function
Function TextReadWord()
g_text_word =""
If (g_text_pos > Len(g_text))Return 0;
While (Mid(g_text,g_text_pos,1) = " ") : g_text_pos=g_text_pos+1 : Wend
While (Mid(g_text,g_text_pos,1) <> " " And g_text_pos<=Len(g_text))
g_text_word =g_text_word+ Mid(g_text,g_text_pos,1);
g_text_pos=g_text_pos+1
Wend
g_text_pos=g_text_pos+1;
Return 1;
End Function