• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

Hilfe bei Chatbox

Status
Für weitere Antworten geschlossen.

Centifolia

Neues Mitglied
Hi --
wäre toll,wenn mir hierzu jemand helfen könnte.Bin Anfängerin in Javascript,muss aber für die Uni eine kleine Aufgabe in Anlehnung an folgende Seite lösen:
Eliza, Computer Therapist

Ich hab nun erst mal ganz einfach angefangen und ein input feld sowie textarea erstellt. Wenn ich den Code richtig verstehe, wird doch bei onSubmit die Funktion dialog aufgerufen. Ich hab mich da an dem Quelltext orientiert und so sieht nun erst mal mein Code aus:

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<SCRIPT language="JavaScript">

// Put together an array for the dialog
    
	chatmax = 5;						// number of lines / 2
	chatpoint = 0;
	chatter = new Array(chatmax);
	
	function dialog(){
		var Input = document.Eliza.input.value;	  // capture input and copy to log
		document.Eliza.input.value = "";        
		chatter[chatpoint] = " \n* " + Input;
		elizaresponse = listen(Input);
		setTimeout("think()", 1000 + Math.random()*3000);
		chatpoint ++ ; 
		if( chatpoint >= chatmax ){ chatpoint = 0; }
		return write();
	}


	
	</script>
</head>

<body>
<table cellspacing="0" cellpadding="10" border="0" align="center">
	<tr><th bgcolor=#ffffff align="left">
		
	</td></tr>
  	<tr><td bgcolor=#660000>
    	<form name="Eliza" onSubmit="return dialog();">
      	<center><textarea rows=14 cols=50 name="log"></textarea><br><br>
      	
		  <INPUT TYPE="text" size = 50 NAME="input" VALUE=""></center> 

    	</form>
  	</td></tr>
</table>


</body>
</html>

Leider funktioniert das Übergeben nicht. Was mache ich falsch?
 
Hast Du schonmal in Deine Fehlerkonsole geguckt? Da werden etliche Fehler drin stehen, die Du erst beheben musst.

z.B. wird er sagen, dass er die Funktionen listen() und think() nicht kennt.
Außerdem ist mir nicht klar, was "return write()" machen soll. write() als Methode von document schreibt etwas irgendwo ins Dokument. Du hast nichts übergeben, was geschrieben werden soll, und der Returnwert ist bei so einer Funktion auch eher uninteressant.
 
Hallo Efchen --
vielen Dank für Deine Antwort. Ich sehe ein, dass da genug Fehler zu beheben sind. Ich habe den Code nun verändert und hoffentlich lauffähiger gemacht. Allerdings habe ich noch das Problem, dass die Eingabe noch nicht in das Textfeld geschrieben wird. Stehe da irgendwie total auf dem Schlauch.

Habe es so versucht:
document.getElementById('eingabefeld').value = document.getElementById('textfeld').value

Fehlt da aber nicht so was wie document.write ?

Hier noch mal der neue Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<SCRIPT type="text/javascript"language="JavaScript">


function dialog(){

//Eingabe wird in variable b gespeichert, die dann die einzelnen Wörter in ein Array speichert
var b = document.getElementById('eingabefeld').value;
var temp = new Array();
temp = b.split(' ');


// hier wird das
for(int i=0; i < temp.length; i++) {

for(int n=0; n < conj1.length; n++) {
if(temp == conj1[n])
temp = conj2[n];
}

}

for(int i=0; i < temp.length; i++) {

for(int n=0; n < keyword.length; n++) {
if(temp == keyword[n])
temp = conj2[n];
}

}


for(int i=0; i < temp.length; i++) {

for(int n=0; n < conj3.length; n++) {
if(temp == conj3[n])
temp = conj4[n];
}

}

document.getElementById('textfeld').innerHTML = document.getElementById('textfeld').innerHTML + ausgabe;

}







// don't redraw the ELIZA's form!

// build our data base here
// eingaben werden entsprechend bei der ausgaben angepasst,
// d.h. I am --> you are
// conj1[x] wird durch durch conj2[x] ersetzt
conj1[0] = "are"; conj2[0] = "am";
conj1[1] = "am"; conj2[1] = "are";
conj1[2] = "were"; conj2[2] = "was";
conj1[3] = "was"; conj2[3] = "were";


// array to post process correct our tenses of pronouns such as "I/me"
// zum Korrigieren von Sprachfehlern - gleiches Prinzip wie oben
// conj3[x] wird durch conj4[x] ersetzt

conj3[0] = "me am"; conj4[0] = "I am";
conj3[1] = "am me"; conj4[1] = "am I";
conj3[2] = "me can"; conj4[2] = "I can";
conj3[3] = "can me"; conj4[3] = "can I";
conj3[4] = "me have"; conj4[4] = "I have";
conj3[5] = "me will"; conj4[5] = "I will";
conj3[6] = "will me"; conj4[6] = "will I";


// Keywords

keyword[ 0]=new key( "CAN YOU", 1, 3);
keyword[ 1]=new key( "CAN I", 4, 5);
keyword[ 2]=new key( "YOU ARE", 6, 9);
keyword[ 3]=new key( "YOU'RE", 6, 9);
keyword[ 4]=new key( "I DON'T", 10, 13);
keyword[ 5]=new key( "I FEEL", 14, 16);


response[ 0]="ELIZA - Javascript Version by George Dunlop ( [email protected] )"
response[ 1]="Don't you believe that I can<*";
response[ 2]="Perhaps you would like to be able to<*";
response[ 3]="You want me to be able to<*";
response[ 4]="Perhaps you don't want to<*";
response[ 5]="Do you want to be able to<*";
response[ 6]="What makes you think I am<*";

</script>
</head>

<body><table cellspacing="0" cellpadding="10" border="0" align="center">
<tr><th bgcolor=#ffffff align="left">

</td></tr>
<tr><td bgcolor=#660000>
<form name="Eliza">
<center><textarea rows=14 cols=50 name="log" id="textfeld"></textarea><br></center>
<br>
<INPUT TYPE="text" id="eingabefeld" size = 50 NAME="input" VALUE="">

<input type="button" value="GO" onClick="dialog();" />
</form>
</td></tr>
</table>
</body>
</html>
 
document.getElementById('eingabefeld').value = document.getElementById('textfeld').value
Fehlt da aber nicht so was wie document.write ?
Nein. Du willst ja nichts Neues ausgeben. Du willst ja den Inhalt eines bestehenden Feldes ändern.

Wenn es nicht geht, stellt sich mir die Frage: Was sagt die Fehlerkonsole?
 
hallo Efchen --
ich hab' mir die fehlermeldungen noch mal genau angesehen und nun auch beheben können. Die Eingabe wird nun auch in das Textfeld geschrieben. Darüber bin ich schon sehr glücklich.
Allerdings sollte es so sein, dass eine Antwort automatisch erscheint, je nachdem was der user eingibt. Ein Keyword sollte erkannt werden woraufhin die entsprechende Antwort geliefert wird.
Hast Du eine Idee, wo da noch der Fehler ist?

Hier der Code:
Code:
<html><head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Untitled Document</title>
<script type="text/javascript" language="JavaScript">

var conj1= new Array();
var conj2= new Array();
var conj3= new Array();
var conj4= new Array();


// Keys

   maxKey = 36;
   keyword = new Array(maxKey);

	function key(key,idx,end){
   	this.key = key;               			// phrase to match
    	this.idx = idx;               			// first response to use
    	this.end = end;               			// last response to use
    	this.last = end;								// response used last time
  	}
	maxrespnses =116;
   response = new Array(maxrespnses);




	function dialog(){
		
		//Eingabe wird in variable b gespeichert, die dann die einzelnen Wörter in ein Array 		speichert
		var b = document.getElementById('eingabefeld').value;
	document.getElementById('textfeld').value = b;
		var temp = new Array();
		temp = b.split(' ');  
		
		
		// hier wird das 
		for (i=0; i < temp.length; i++) {

		 for (n=0; n < conj1.length; n++) {
		   if(temp[i] == conj1[n]) {
			temp[i] = conj2[n]; 
		 }
		 }

		}
		
		for (i=0; i < temp.length; i++) {

		 for (n=0; n < keyword.length; n++) {
		   if(temp[i] == keyword[n]) {
			temp[i] = conj2[n];
		 }
 }
		}
		
		
		for (i=0; i < temp.length; i++) {

		 for (n=0; n < conj3.length; n++) {
		   if(temp[i] == conj3[n]) {
			temp[i] = conj4[n];  }
		 }

		}
		
	document.getElementById('textfeld').innerHTML = document.getElementById('textfeld').innerHTML + ausgabe;
		
	}
 
das ist die fortsetzung (etwas gekürzt)
Code:
// don't redraw the ELIZA's form!
		
// build our data base here 
// eingaben werden entsprechend bei der ausgaben angepasst, 
// d.h. I am --> you are 
// conj1[x] wird durch durch conj2[x] ersetzt 
		    conj1[0]  = "are";   		conj2[0]  = "am";
		    conj1[1]  = "am";   		conj2[1]  = "are";
		    conj1[2]  = "were";  		conj2[2]  = "was";
		    conj1[3]  = "was";  		conj2[3]  = "were";
		    conj1[4]  = "I";    			conj2[4]= "you";    
		    conj1[5]  = "me";    		conj2[5]  = "you";    
		    conj1[6]  = "you";   		conj2[6]  = "me";
		    conj1[7]  = "my";    		conj2[7]  = "your";    
		    conj1[8]  = "your";  		conj2[8]  = "my";
		    conj1[9]  = "mine";  		conj2[9]  ="your's";    
		    conj1[10] = "your's"; 	conj2[10] = "mine";    
		    conj1[11] = "I'm";   		conj2[11] ="you're";
		    conj1[12] = "you're";  	conj2[12] = "I'm";    
		    conj1[13] = "I've";  		conj2[13] = "you've";
		    conj1[14] = "you've"; 	conj2[14] = "I've";
		    conj1[15] = "I'll"; 		conj2[15] = "you'll";
		    conj1[16] = "you'll"; 	conj2[16] = "I'll";
		    conj1[17] = "myself"; 	conj2[17] = "yourself";
		    conj1[18] = "yourself"; 	conj2[18] = "myself";		
	
	
	
	// array to post process correct our tenses of pronouns such as "I/me"
	// zum Korrigieren von Sprachfehlern - gleiches Prinzip wie oben
	// conj3[x] wird durch conj4[x] ersetzt

	    conj3[0]  = "me am";   	conj4[0]  = "I am";
	    conj3[1]  = "am me";   	conj4[1]  = "am I";
	    conj3[2]  = "me can";   	conj4[2]  = "I can";
	    conj3[3]  = "can me";   	conj4[3]  = "can I";
	    conj3[4]  = "me have";  	conj4[4]  = "I have";
	    conj3[5]  = "me will";   	conj4[5]  = "I will";
	    conj3[6]  = "will me";   	conj4[6]  = "will I";
	
	
		// Keywords

		    keyword[ 0]=new key( "CAN YOU",  		1,  3);
		    keyword[ 1]=new key( "CAN I",    		4,  5);
		    keyword[ 2]=new key( "YOU ARE",  		6,  9);
		    keyword[ 3]=new key( "YOU'RE",   		6,  9);
		    keyword[ 4]=new key( "I DON'T",  		10, 13);
		    keyword[ 5]=new key( "I FEEL",   		14, 16);
		    keyword[ 6]=new key( "WHY DON'T YOU", 17, 19);
		    keyword[ 7]=new key( "WHY CAN'T I", 	20, 21);
		    keyword[ 8]=new key( "ARE YOU",  		22, 24);
		    keyword[ 9]=new key( "I CAN'T",  		25, 27);
		    keyword[10]=new key( "I AM",     		28, 31);
		    keyword[11]=new key( "I'M",      		28, 31);
		    keyword[12]=new key( "YOU",      		32, 34);
		    keyword[13]=new key( "I WANT",   		35, 39);
		    keyword[14]=new key( "WHAT",     		40, 48);
		    keyword[15]=new key( "HOW",      		40, 48);
		    keyword[16]=new key( "WHO",      		40, 48);
		    keyword[17]=new key( "WHERE",    		40, 48);
		    keyword[18]=new key( "WHEN",     		40, 48);
		    keyword[19]=new key( "WHY",      		40, 48);
		    keyword[20]=new key( "NAME",     		49, 50);
		    keyword[21]=new key( "CAUSE",    		51, 54);
		    keyword[22]=new key( "SORRY",    		55, 58);
		    keyword[23]=new key( "DREAM",    		59, 62);
		    keyword[24]=new key( "HELLO",    		63, 63);
		    keyword[25]=new key( "HI",       		63, 63);
		    keyword[26]=new key( "MAYBE",    		64, 68);
		    keyword[27]=new key( "NO",       		69, 73);
		    keyword[28]=new key( "YOUR",     		74, 75);
		    keyword[29]=new key( "ALWAYS",   		76, 79);
		    keyword[30]=new key( "THINK",    		80, 82);
		    keyword[31]=new key( "ALIKE",    		83, 89);
		    keyword[32]=new key( "YES",      		90, 92);
		    keyword[33]=new key( "FRIEND",   		93, 98);
		    keyword[34]=new key( "COMPUTER", 		99, 105);
		    keyword[35]=new key( "NO KEY FOUND", 	106, 112);
		    keyword[36]=new key( "REPEAT INPUT", 	113, 116);


		    response[  0]="ELIZA - Javascript Version by George Dunlop ( [email protected] )"
		    response[  1]="Don't you believe that I can<*";
		    response[  2]="Perhaps you would like to be able to<*";
		    response[  3]="You want me to be able to<*";
		    response[  4]="Perhaps you don't want to<*";
		    response[  5]="Do you want to be able to<*";
		    response[  6]="What makes you think I am<*";
		    response[  7]="Does it please you to believe I am<*";
		    response[  8]="Perhaps you would like to be<*";
		    response[  9]="Do you sometimes wish you were<*";
		    response[ 10]="Don't you really<*";
		    response[ 11]="Why don't you<*";
		    response[ 12]="Do you wish to be able to<*";
		    response[ 13]="Does that trouble you?";
		    response[ 14]="Tell me more about such feelings.";
		    response[ 15]="Do you often feel<*";
		    response[ 16]="Do you enjoy feeling<*";
		    response[ 17]="Do you really believe I don't<*";
		    response[ 18]="Perhaps in good time I will<@";
		    response[ 19]="Do you want me to<*";
		    response[ 20]="Do you think you should be able to<*";
		    response[ 21]="Why can't you<*";
		    response[ 22]="Why are you interested in whether or not I am<*";
		    response[ 23]="Would you prefer if I were not<*";
		    response[ 24]="Perhaps in your fantasies I am<*";
		    response[ 25]="How do you know you can't<*";
		    response[ 26]="Have you tried?";
		    response[ 27]="Perhaps you can now<*";
		    response[ 28]="Did you come to me because you are<*";
		    response[ 29]="How long have you been<*";
		    response[ 30]="Do you believe it is normal to be<*";
		    response[ 31]="Do you enjoy being<*";
		    response[ 32]="We were discussing you, not me.";
		    response[ 33]="Oh... <*";
		    response[ 34]="You're not really talking about me, are you?";
		    response[ 35]="What would it mean to you if you got<*";
		    response[ 36]="Why do you want<*";
		    response[ 37]="Suppose you got<*";
		    response[ 38]="What if you never got<*";
		    response[ 39]="I sometimes also want<@";
		    response[ 40]="Why do you ask?";
		    response[ 41]="Does that question interest you?";
		    response[ 42]="What answer would please you the most?";
		    response[ 43]="What do you think?";
		    response[ 44]="Are such questions on your mind often?";
		    response[ 45]="What is it that you really want to know?";
		    response[ 46]="Have you asked anyone else?";
		    response[ 47]="Have you asked such questions before?";
		    response[ 48]="What else comes to mind when you ask that?";
		    response[ 49]="Names don't interest me.";
		    response[ 50]="I don't care about names, please go on.";
		    response[ 51]="Is that the real reason?";
		    response[ 52]="Don't any other reasons come to mind?";
		    response[ 53]="Does that reason explain anything else?";
		    response[ 54]="What other reasons might there be?";
		    response[ 55]="Please don't apologise!";
		    response[ 56]="Apologies are not necessary.";
		    response[ 57]="What feelings do you have when you apologise?";
		    response[ 58]="Don't be so defensive!";
		    response[ 59]="What does that dream suggest to you?";
		    response[ 60]="Do you dream often?";
		    response[ 61]="What persons appear in your dreams?";
		    response[ 62]="Are you disturbed by your dreams?";
		    response[ 63]="How are you today.. What would you like to discuss?";
		    response[ 64]="You don't seem quite certain.";
		    response[ 65]="Why the uncertain tone?";
		    response[ 66]="Can't you be more positive?";
		    response[ 67]="You aren't sure?";
		    response[ 68]="Don't you know?";
		    response[ 69]="Are you saying no just to be negative?";
		    response[ 70]="You are being a bit negative.";
		    response[ 71]="Why not?";
		    response[ 72]="Are you sure?";
		    response[ 73]="Why no?";
		    response[ 74]="Why are you concerned about my<*";
		    response[ 75]="What about your own<*";
		    
	
	</script>
</head><body><table align="center" border="0" cellpadding="10" cellspacing="0">
	<tbody><tr><th align="left" bgcolor="#ffffff">
		
	</th></tr>
  	<tr><td bgcolor="#660000">
    	<form name="Eliza">
      	<center><textarea rows="14" cols="50" name="log" id="textfeld"></textarea><br></center>

      	  <br>
      	  <input id="eingabefeld" size="50" name="input" value="" type="text"> 

    	  <input value="GO" onclick="dialog();" type="button">
    	</form>
  	</td></tr>
</tbody></table>
</body></html>
 
Status
Für weitere Antworten geschlossen.
Zurück
Oben