gpt4 book ai didi

Java 聊天机器人——代码未按预期工作

转载 作者:行者123 更新时间:2023-12-01 09:32:22 25 4
gpt4 key购买 nike

我正在编写以下用于聊天机器人的代码。然而,它并没有按预期工作。当我运行调试器时,它显示“再见”由于某种奇怪的原因不断被分配给“关键字”。从逻辑上讲,一切看起来都应该按预期进行。但显然不是。任何帮助表示赞赏。我对 Java 编程相当陌生。

public class Chatbot {


public Chatbot( )
{
}

/*
* Generates a variety of responses, based on what the user has stated
*/
public static void respond( String statement )
{
// use the findKeyword method to check for various cases of user statements
if( statement.length() == 0 )
{
System.out.println( "Please say something :)" );
}

else if( findKeyword( statement, "hi" ) > 0 ||
findKeyword( statement, "hello" ) > 0 ||
findKeyword( statement, "hey" ) > 0 ||
findKeyword( statement, "hiya" ) > 0 ||
findKeyword( statement, "heya" ) > 0 )
{
System.out.println( "Hello to you too!" );
}

else if( findKeyword( statement, "how are you" ) > 0 ||
findKeyword( statement, "hows it going" ) > 0 ||
findKeyword( statement, "howre you" ) > 0 ||
findKeyword( statement, "how ya doing" ) > 0 ||
findKeyword( statement, "yo wassup" ) > 0 ||
findKeyword( statement, "hey whats up" ) > 0 ||
findKeyword( statement, "whats up" ) > 0 )
{
System.out.println( "I'm good, how are you?" );
}
}



/*
* findKeyword method, returns either a 0 or a 1
* @ 0 -- keyword not found
* @ 1 -- keyword found
*/
public static int findKeyword( String statement, String keyword )
{

// This is in case the keyword is not in the statement at all
if( !statement.contains( keyword ) )
{
return 0;
}


int position = statement.toLowerCase().indexOf( keyword.toLowerCase() ); // position of the keyword in the statement
statement = " " + statement.toLowerCase().replaceAll( "\'\",.?", "") + " "; // the purpose of this statement is to allow for us to search for specific phrases w/ spaces before and after the keyword

String sub = statement.substring( position, position + keyword.length() + 1 ); // isolates the keyword with 1 character before and after

String charBeforeKeyword = sub.substring( 0, 1 ); // the character before the keyword
String charAfterKeyword = sub.substring( sub.length() - 1, sub.length() ); // the character after the keyword



/*
* Now, we check to see if the characters we isolated before are letters; if they are *
* @ If they are letters...then our keyword is part of a bigger word (e.g. if we searched for "success" and it brought us "successful"
* @ If they are not letters, then we have found our keyword with punctuation and/or spaces before/after it
*/
if( (charBeforeKeyword.compareTo( "a" ) < 0 || charBeforeKeyword.compareTo( "z" ) > 0 )
&& (charAfterKeyword.compareTo( "a" ) < 0 || charAfterKeyword.compareTo( "z" ) > 0 ))
{
return 1;
}

return 0;

}


}



import java.util.Scanner;

public class Execute
{
public static void main( String [] args )
{
// Variables and Objects
Chatbot bot = new Chatbot();
Scanner input = new Scanner( System.in );
String statement = "";

// Prompt and get the user's first input
System.out.println( "Type text to start chatting!" );
statement = input.nextLine();

// While the user doesn't say goodbye or some other form of it, respond to user and then get their next response
while( bot.findKeyword( statement, "bye" ) != 1 &&
bot.findKeyword( statement, "cya" ) != 1 &&
bot.findKeyword( statement, "goodbye" ) != 1 &&
bot.findKeyword( statement, "gtg" ) != 1 )
{
bot.respond( statement );
statement = input.nextLine();
}
System.out.println( "Goodbye!" );




}

}

最佳答案

1,请阅读substring的javadoc。

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1

根据你的逻辑,你必须修改

 String sub = statement.substring( position, position + keyword.length() + 1 );

String sub = statement.substring( position, position + keyword.length() + 2 );

2、由于您已经创建了Chatbot实例,因此无需在Chatbot中使用静态方法。

3、当您不再使用扫描仪时,请记得将其关闭。

查看更新后的代码如下:

public class Chatbot {


public Chatbot( )
{
}

/*
* Generates a variety of responses, based on what the user has stated
*/
public void respond( String statement )
{
// use the findKeyword method to check for various cases of user statements
if( statement.length() == 0 )
{
System.out.println( "Please say something :)" );
}

else if( findKeyword( statement, "hi" ) > 0 ||
findKeyword( statement, "hello" ) > 0 ||
findKeyword( statement, "hey" ) > 0 ||
findKeyword( statement, "hiya" ) > 0 ||
findKeyword( statement, "heya" ) > 0 )
{
System.out.println( "Hello to you too!" );
}

else if( findKeyword( statement, "how are you" ) > 0 ||
findKeyword( statement, "hows it going" ) > 0 ||
findKeyword( statement, "howre you" ) > 0 ||
findKeyword( statement, "how ya doing" ) > 0 ||
findKeyword( statement, "yo wassup" ) > 0 ||
findKeyword( statement, "hey whats up" ) > 0 ||
findKeyword( statement, "whats up" ) > 0 )
{
System.out.println( "I'm good, how are you?" );
}
}



/*
* findKeyword method, returns either a 0 or a 1
* @ 0 -- keyword not found
* @ 1 -- keyword found
*/
public int findKeyword( String statement, String keyword )
{

// This is in case the keyword is not in the statement at all
if( !statement.contains( keyword ) )
{
return 0;
}


int position = statement.toLowerCase().indexOf( keyword.toLowerCase() ); // position of the keyword in the statement
statement = " " + statement.toLowerCase().replaceAll( "\'\",.?", "") + " "; // the purpose of this statement is to allow for us to search for specific phrases w/ spaces before and after the keyword

String sub = statement.substring( position, position + keyword.length() + 2 ); // isolates the keyword with 1 character before and after

String charBeforeKeyword = sub.substring( 0, 1 ); // the character before the keyword
String charAfterKeyword = sub.substring( sub.length() - 1, sub.length()); // the character after the keyword



/*
* Now, we check to see if the characters we isolated before are letters; if they are *
* @ If they are letters...then our keyword is part of a bigger word (e.g. if we searched for "success" and it brought us "successful"
* @ If they are not letters, then we have found our keyword with punctuation and/or spaces before/after it
*/
if( (charBeforeKeyword.compareTo( "a" ) < 0 || charBeforeKeyword.compareTo( "z" ) > 0 )
&& (charAfterKeyword.compareTo( "a" ) < 0 || charAfterKeyword.compareTo( "z" ) > 0 ))
{
return 1;
}

return 0;

}


}

还有执行类。

import java.util.Scanner;

public class Execute
{
public static void main( String [] args )
{
// Variables and Objects
Chatbot bot = new Chatbot();
Scanner input = new Scanner( System.in );
String statement = "";

// Prompt and get the user's first input
System.out.println( "Type text to start chatting!" );
statement = input.nextLine();

// While the user doesn't say goodbye or some other form of it, respond to user and then get their next response
while( bot.findKeyword( statement, "bye" ) != 1 &&
bot.findKeyword( statement, "cya" ) != 1 &&
bot.findKeyword( statement, "goodbye" ) != 1 &&
bot.findKeyword( statement, "gtg" ) != 1 )
{
bot.respond( statement );
statement = input.nextLine();
}
input.close();
System.out.println( "Goodbye!" );
}
}

关于Java 聊天机器人——代码未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39302446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com