2013-04-04 5 views
0

현재 진술을 반영 할 수있는 기본 'AI'를 작성하려고합니다. 예를 들어 사용자가 "내 고양이는 갈색입니다."라고 입력하면 인공 지능은 "당신의 고양이는 갈색입니까?"라고 대답 할 것입니다.파이썬에서 문자열의 단어를 대체하는 방법은 무엇입니까?

내가 번역을 위해 사용하려는 변수 데이터의 테이블을 작성했습니다 :

reflections = \ 
[["I",   "you"], 
["i",   "you"], 
["We",   "you"], 
["we",   "you"], 
["We're",  "you're"], 
["we're",  "you're"], 
["I'm",  "you're"], 
["i'm",  "you're"], 
["im",   "you're"], 
["this",  "that"], 
["This",  "that"], 
["am",   "are"], 
["Am",   "are"], 
["My",   "your"], 
["my",   "your"], 
["you",  "I"], # Grammar: Sometimes "me" is better 
["You",  "I"], 
["u",   "me"], 
["I'd",  "you'd"], 
["I'll",  "you'll"], 
["We'd",  "you'd"], 
["we'd",  "you'd"], 
["We'll",  "you'll"], 
["we'll",  "you'll"], 
["You're",  "I'm"], 
["you're",  "I'm"], 
["ur",   "I'm"], 
["c",   "see"], 
["I've",  "you've"], 
["We've",  "you've"], 
["we've",  "you've"], 
["Our",  "your"], 
["our",  "your"], 
["was",  "were"], 
["Was",  "were"], 
["were",  "was"], 
["Were",  "was"], 
["me",   "you"], 
["your",  "my"], 
["Your",  "my"]] 

는 그러나, 나는 데이터를 구현하는 몇 가지 문제가 있어요. 문자열 반성

내 현재의 정의는 다음과 같습니다

from string import maketrans 

intab = ".!" 
outtab = "??" 
translate_message = maketrans(intab, outtab) #used to replace punctuation 

def reflect_statement(message): 
    if ' ' not in message: 
     if len(message) == 0: 
      return elicitations[0] 
     if len(message) == 1: 
      return elicitations[1] 
     if len(message) == 2: 
      return elicitations[2] 
     if len(message) == 3: 
      return elicitations[3] 
     if len(message) == 4: 
      return elicitations[4] 
     if len(message) == 5: 
      return elicitations[5] 
     if len(message) == 6: 
      return elicitations[6] 
     if len(message) == 7: 
      return elicitations[7] 
     if len(message) == 8: 
      return elicitations[8] 
     if len(message) == 9: 
      return elicitations[9] 
     if len(message) == 10: 
      return elicitations[10] 
     if len(message) > 10: 
      return elicitations[11] 
    if ' ' in message: 
     message = message.translate(translate_message) 
     return message 

가 elicitations 참조를 무시, 그게 내가 완료 한 프로그램의 별도의 일부입니다.

누군가가 저에게 제공 할 수있는 도움에 정말 감사드립니다.

건배!

+1

: 반환 elicitations은 [I] – zod

답변

1
if ' ' not in message: 
    if len(message) < 11: 
     return elicitations[len(message)] 
    else: 
     return elicitations[11] 
else: 
    for pair in reflections: 
     message = message.replace(*pair) 

노트를 좋아하는 경향이

  1. 이것은 단어의 일부를 대체합니다. 적절한 교체를 위해서는 정중 한 표현을 신중하게 작성해야합니다. 어쩌면 r'\b{}\b'.format(oldword) 같은 것이 작동합니다,하지만 난 잘 모르겠어요 :

    import re 
    for old, new in reflections: 
        message = re.sub(r'\b{}\b'.format(old), new, message) 
    
  2. 중첩 된 목록은 가장 논리적 인 데이터 구조 아니라, 사전을 사용하는 것이 좋습니다. LEN (메시지) == 내가 경우

+0

는 "나는 선물이"'으로 영업 이익이 원하는 꽤 어떻게 작동하지 않을 수 있지만,'' –

+0

"당신은 gyouft이"'된다 @JoranBeasley 당신 말이 맞아요, 나는 그것을 타이핑하고있었습니다. –

+0

나는 그가 영어 문장 구조를 인식하기 위해 문법을 필요로한다고 생각한다 ... –

1

은 당신이 정말로 원하는 것은 당신이 어떤 문법을 만들 필요가 없습니다 이런 일에 대해 당신이 대명사를 선택하고 단지 대명사를 대체 할 수 있도록 문법 .... 기본적으로

을 정의하다 무슨 일이 ... 나 AI가

Neds List Of PyParsers에서 살펴

많이 아무것도에 액수하지 않습니다 ... 나는 플라이

관련 문제