2015-02-03 4 views
0

배경 : 초보자가 자바로 익숙해 져서 문자열에 익숙해졌습니다. 나는 SO와 자바 API를 다 둘러 보았다.주어진 문자열의 일부를 가져 와서 가져온 부분을 변수로 반환 하시겠습니까?

내 문제 : 저는 작은 chat-bot 프로젝트를 진행하고 있습니다. 목표는 누군가가 "무언가"이라고 입력하면 봇이 "왜 무엇인가나요?"라고 대답해야합니다. 으로 무엇인가은 "사랑"또는 "나는 당신을 사랑합니다"와 같은 것이고, 로봇은 "왜 나를 사랑하니?"

희망 사항으로 여러 단어로 작업해야합니다. 누군가 "내가 실제로 당신을 싫어합니다"라고 쓰면 "왜 나를 실제로 싫어합니까?"라고 대답 할 것입니다.

이렇게하려면 "I"와 "You"를 잘라서 남은 것을 가져 와서 새 문장으로 정렬하십시오. 나는 문자열을 알고 있다면 어떻게 할 것인지 알기에 충분히 익숙하지만 아무도 아무것도 입력 할 수 없기 때문에 초보자로서 나는 문제가있다. 여기에 내가 가진 무엇 : 참고로

public String getResponse(String statement) 
 
\t { 
 
\t \t String response = ""; 
 
\t \t 
 
\t \t 
 
\t \t if (findKeyword(statement, "I") >= 0 
 
\t \t   || findKeyword(statement, "you") >= 0) 
 
\t \t { 
 
\t \t \t response = transformWhyDoYouStatement(statement); 
 
\t \t } 
 

 

 
\t \t return response; 
 
\t }

private String transformWhyDoYouStatement(String statement) 
 
\t { 
 
\t \t // Remove the final period, if there is one 
 
\t \t statement = statement.trim(); 
 
\t \t String lastChar = statement.substring(statement 
 
\t \t \t \t .length() - 1); 
 
\t \t if (lastChar.equals(".")) 
 
\t \t { 
 
\t \t \t statement = statement.substring(0, statement 
 
\t \t \t \t \t .length() - 1); 
 
\t \t } 
 
\t \t 
 
\t \t //String[] parts = string.split(" "); 
 
\t \t len = statement.length; 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t \t 
 
\t \t String restOfStatement = statement.substring(psnOfYou + 3, psnOfMe).trim(); 
 
\t \t return "Why do you" + restOfStatement + " me?"; 
 
\t }

private int findKeyword(String statement, String goal, int startPos) 
 
\t { 
 
\t \t String phrase = statement.trim(); 
 
\t \t // The only change to incorporate the startPos is in the line below 
 
\t \t int psn = phrase.toLowerCase().indexOf(goal.toLowerCase(), startPos); 
 
\t \t 
 
\t \t // Refinement--make sure the goal isn't part of a word 
 
\t \t while (psn >= 0) 
 
\t \t { 
 
\t \t \t // Find the string of length 1 before and after the word 
 
\t \t \t String before = " ", after = " "; 
 
\t \t \t if (psn > 0) 
 
\t \t \t { 
 
\t \t \t \t before = phrase.substring (psn - 1, psn).toLowerCase(); 
 
\t \t \t } 
 
\t \t \t if (psn + goal.length() < phrase.length()) 
 
\t \t \t { 
 
\t \t \t \t after = phrase.substring(psn + goal.length(), psn + goal.length() + 1).toLowerCase(); 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t // If before and after aren't letters, we've found the word 
 
\t \t \t if (((before.compareTo ("a") < 0) || (before.compareTo("z") > 0)) // before is not a letter 
 
\t \t \t \t \t && ((after.compareTo ("a") < 0) || (after.compareTo("z") > 0))) 
 
\t \t \t { 
 
\t \t \t \t return psn; 
 
\t \t \t } 
 
\t \t \t 
 
\t \t \t // The last position didn't work, so let's find the next, if there is one. 
 
\t \t \t psn = phrase.indexOf(goal.toLowerCase(), psn + 1); 
 
\t \t \t 
 
\t \t } 
 
\t \t 
 
\t \t return -1; 
 
\t }

+0

은'findKeyword' 코드를 제시해주십시오. –

+0

나는 전체를 게시 할 것이다 :) –

+0

거기 가서 @ Jean-FrançoisSavard –

답변

0

, 자바 시간 패키지와 일치하는 꽤 좋은 정규 표현식 : java.util.regex. 이러한 클래스를 사용하면 문자열이 "I you"와 같은 패턴과 일치하는지 테스트 할 수 있습니다. 일치하는 경우 문자열에서 특정 "그룹"을 검색 할 수 있습니다. 예를 들어

:

Pattern pattern = Pattern.compile("I (.*) you"); 
Matcher matcher = pattern.matcher("I really dig you"); 
if (matcher.matches()) { 
    System.out.println("Why do you " + matcher.group() + " me?"); 
} 
관련 문제