Permalink
Browse files

Version 1.2

- Code cleanup: removed the unnecessary "s:" flags for most variables that just need to be local to their functions. No other functional difference from the previous version.
  • Loading branch information...
1 parent 433c98e commit 877409f3d7d3dad8d179ed55bfdaca48070a55de Michael Kamensky committed with Oct 12, 2012
Showing with 92 additions and 91 deletions.
  1. +2 −2 README.orig
  2. +90 −89 plugin/VimChatBot.vim
View
4 README.orig
@@ -48,8 +48,8 @@ Alternatively, you can start Vim ChatBot by typing the following command:
Macros
======
-You can use the following macros when typing answers, the bot will also utilize them
-when learning to use the phrase from you:
+You can use the following macros when typing answers, the bot will also utilize
+them when learning to use the phrase from you:
$TIME$ - refers to the current time.
$TIME12$ - (experimental) refers to the current time in 12-hour format.
View
179 plugin/VimChatBot.vim
@@ -4,109 +4,109 @@
let s:ChatIteration = 1
let s:MagicalContexts = 2
-let s:BotVersion = "1.1"
+let s:BotVersion = "1.2"
function! Random(min, max)
if has("python")
python from random import randint
python from vim import command, eval
python command("return %d" % randint(int(eval("a:min")), int(eval("a:max"))))
else
- let s:random_val = localtime() % 65536
- let s:random_val = (s:random_val * 31421 + 6927) % 65536
- let s:constrained_val = (s:random_val * a:max) / 65536 + a:min
- return s:constrained_val
+ let random_val = localtime() % 65536
+ let random_val = (random_val * 31421 + 6927) % 65536
+ let constrained_val = (random_val * a:max) / 65536 + a:min
+ return constrained_val
endif
endfunction
function! Macroexpand(phrase)
- let s:expanded_phrase = substitute(a:phrase, "\\$TIME\\$", strftime("%H:%M"), "g")
- let s:expanded_phrase = substitute(s:expanded_phrase, "\\$TIME12\\$", strftime("%I:%M %p"), "g")
- let s:expanded_phrase = substitute(s:expanded_phrase, "\\$WEEKDAY\\$", strftime("%A"), "g")
- let s:expanded_phrase = substitute(s:expanded_phrase, "\\$DAY\\$", strftime("%d"), "g")
- let s:expanded_phrase = substitute(s:expanded_phrase, "\\$MONTH\\$", strftime("%B"), "g")
- let s:expanded_phrase = substitute(s:expanded_phrase, "\\$YEAR\\$", strftime("%Y"), "g")
- return s:expanded_phrase
+ let expanded_phrase = substitute(a:phrase, "\\$TIME\\$", strftime("%H:%M"), "g")
+ let expanded_phrase = substitute(expanded_phrase, "\\$TIME12\\$", strftime("%I:%M %p"), "g")
+ let expanded_phrase = substitute(expanded_phrase, "\\$WEEKDAY\\$", strftime("%A"), "g")
+ let expanded_phrase = substitute(expanded_phrase, "\\$DAY\\$", strftime("%d"), "g")
+ let expanded_phrase = substitute(expanded_phrase, "\\$MONTH\\$", strftime("%B"), "g")
+ let expanded_phrase = substitute(expanded_phrase, "\\$YEAR\\$", strftime("%Y"), "g")
+ return expanded_phrase
endfunction
function! GetLineMatchingPattern(pattern)
- let s:num_line = 1
- let s:max_line = line("$")
- while s:num_line != s:max_line
- if getline(s:num_line) =~ a:pattern
- return s:num_line
+ let num_line = 1
+ let max_line = line("$")
+ while num_line != max_line
+ if getline(num_line) =~ a:pattern
+ return num_line
endif
- let s:num_line += 1
+ let num_line += 1
endwhile
return -1
endfunction
function! CountResponses(start_line)
- let s:num_responses = 0
- let s:cur_line = a:start_line + 1
- let s:max_line = line("$")
+ let num_responses = 0
+ let cur_line = a:start_line + 1
+ let max_line = line("$")
if a:start_line <= 0
return -1
endif
- while s:cur_line < s:max_line
- if getline(s:cur_line) =~ "^\\s*$"
+ while cur_line < max_line
+ if getline(cur_line) =~ "^\\s*$"
break
endif
- let s:cur_line += 1
- let s:num_responses += 1
+ let cur_line += 1
+ let num_responses += 1
endwhile
- return s:num_responses
+ return num_responses
endfunction
function! HasResponse(start_line, resp)
- let s:has_response = 0
- let s:cur_line = a:start_line + 1
- let s:max_line = line("$")
+ let has_response = 0
+ let cur_line = a:start_line + 1
+ let max_line = line("$")
if a:start_line <= 0
return -1
endif
- while s:cur_line < s:max_line
- if getline(s:cur_line) =~ a:resp
- let s:has_response = 1
+ while cur_line < max_line
+ if getline(cur_line) =~ a:resp
+ let has_response = 1
break
- elseif getline(s:cur_line) =~ "^\\s*$"
+ elseif getline(cur_line) =~ "^\\s*$"
break
endif
- let s:cur_line += 1
+ let cur_line += 1
endwhile
- return s:has_response
+ return has_response
endfunction
function! AI_AddResponse(pattern, new_resp, iteration)
- let s:signature = ":::"
+ let signature = ":::"
if a:iteration <= s:MagicalContexts
- let s:signature = s:signature . a:iteration
+ let signature = signature . a:iteration
endif
- let s:resp_block = GetLineMatchingPattern(a:pattern . s:signature)
- if s:resp_block == -1
+ let resp_block = GetLineMatchingPattern(a:pattern . signature)
+ if resp_block == -1
call AI_Store_New_Response(a:pattern, a:new_resp, a:iteration)
return 0
endif
- let s:has_resp = HasResponse(s:resp_block, a:new_resp)
- if s:has_resp == 1
+ let has_resp = HasResponse(resp_block, a:new_resp)
+ if has_resp == 1
return -1
endif
- execute ("normal " . s:resp_block . "gg")
+ execute ("normal " . resp_block . "gg")
execute ("normal o" . a:new_resp)
return 0
endfunction
function! AI_Decide_Response(pattern)
- let s:resp_block = GetLineMatchingPattern(a:pattern)
- if s:resp_block == -1
+ let resp_block = GetLineMatchingPattern(a:pattern)
+ if resp_block == -1
return -1
endif
- let s:has_resp = HasResponse(s:resp_block, a:pattern)
- if s:has_resp == -1
+ let has_resp = HasResponse(resp_block, a:pattern)
+ if has_resp == -1
return -1
endif
- let s:num_responses = CountResponses(s:resp_block)
- return s:resp_block + Random(1, s:num_responses)
+ let num_responses = CountResponses(resp_block)
+ return resp_block + Random(1, num_responses)
endfunction
function! AI_Store_New_Response(pattern, response, iteration)
@@ -122,63 +122,63 @@ endfunction
function! AI_Respond(pattern, iteration)
if a:iteration <= s:MagicalContexts
- let s:chosen_response = AI_Decide_Response("^" . a:pattern . ":::" . a:iteration . "$")
- let s:resp_offset = 5
- let s:request_group = 'v:val =~ ".*:::' . a:iteration . '$"'
- let s:request_signature = ":::" . a:iteration
+ let chosen_response = AI_Decide_Response("^" . a:pattern . ":::" . a:iteration . "$")
+ let resp_offset = 5
+ let request_group = 'v:val =~ ".*:::' . a:iteration . '$"'
+ let request_signature = ":::" . a:iteration
if a:iteration == s:MagicalContexts
- let s:next_request = 'v:val =~ ".*:::$"'
- let s:resp_offset = 4
- let s:request_group = 'v:val =~ ".*:::' . a:iteration . '$"'
- let s:request_signature = ":::" . a:iteration
+ let next_request = 'v:val =~ ".*:::$"'
+ let resp_offset = 4
+ let request_group = 'v:val =~ ".*:::' . a:iteration . '$"'
+ let request_signature = ":::" . a:iteration
else
- let s:next_request = 'v:val =~ ".*:::' . (a:iteration + 1) . '$"'
+ let next_request = 'v:val =~ ".*:::' . (a:iteration + 1) . '$"'
endif
else
- let s:chosen_response = AI_Decide_Response("^" . a:pattern . ":::$")
- let s:resp_offset = 4
- let s:request_group = 'v:val =~ ".*:::$"'
- let s:next_request = 'v:val =~ ".*:::$"'
- let s:request_signature = ":::"
+ let chosen_response = AI_Decide_Response("^" . a:pattern . ":::$")
+ let resp_offset = 4
+ let request_group = 'v:val =~ ".*:::$"'
+ let next_request = 'v:val =~ ".*:::$"'
+ let request_signature = ":::"
endif
- if s:chosen_response != -1
- echo "ChatBot: " . Macroexpand(getline(s:chosen_response)) . "\n"
+ if chosen_response != -1
+ echo "ChatBot: " . Macroexpand(getline(chosen_response)) . "\n"
else
echohl Comment
echo "ChatBot: I don't understand that. Can you please teach me what to say?\n"
echohl None
- let s:suggested_response = input("You say: ")
- if s:suggested_response == "/Q" || s:suggested_response =~ "^\\s*$"
+ let suggested_response = input("You say: ")
+ if suggested_response == "/Q" || suggested_response =~ "^\\s*$"
echohl Comment
echo "ChatBot: Fine, don't teach me if you don't want to!\n"
echohl N
return
endif
- echo "Human: " . Macroexpand(s:suggested_response) . "\n"
- call AI_Store_New_Response(a:pattern, s:suggested_response, a:iteration)
+ echo "Human: " . Macroexpand(suggested_response) . "\n"
+ call AI_Store_New_Response(a:pattern, suggested_response, a:iteration)
echohl Comment
echo "ChatBot: Thanks, I'll remember that!\n"
echohl None
endif
- let s:decide_to_ask_back = Random(0, 1)
- if s:decide_to_ask_back
- let s:lines = getline(1, line("$"))
- let s:requests = filter(s:lines, s:next_request)
- if len(s:requests) != 0
- let s:decision = Random(0, len(s:requests) - 1)
- echo "ChatBot: " . Macroexpand(s:requests[s:decision][:-s:resp_offset]) . "\n"
- let s:taught_response = input("You say: ")
- if s:taught_response == "/Q" || s:taught_response =~ "^\\s*$"
+ let decide_to_ask_back = Random(0, 1)
+ if decide_to_ask_back
+ let lines = getline(1, line("$"))
+ let requests = filter(lines, next_request)
+ if len(requests) != 0
+ let decision = Random(0, len(requests) - 1)
+ echo "ChatBot: " . Macroexpand(requests[decision][:-resp_offset]) . "\n"
+ let taught_response = input("You say: ")
+ if taught_response == "/Q" || taught_response =~ "^\\s*$"
echohl Comment
echo "ChatBot: Fine, don't want to answer me - don't answer.\n"
echohl None
return
endif
- echo "Human: " . Macroexpand(s:taught_response) . "\n"
- let s:response_group_loc = GetLineMatchingPattern(s:requests[s:decision][:-s:resp_offset] . s:request_signature)
- let s:already_has_resp = HasResponse(s:response_group_loc, s:taught_response)
- if s:already_has_resp != 1
- call AI_AddResponse(s:requests[s:decision][:-s:resp_offset], s:taught_response, a:iteration)
+ echo "Human: " . Macroexpand(taught_response) . "\n"
+ let response_group_loc = GetLineMatchingPattern(requests[decision][:-resp_offset] . request_signature)
+ let already_has_resp = HasResponse(response_group_loc, taught_response)
+ if already_has_resp != 1
+ call AI_AddResponse(requests[decision][:-resp_offset], taught_response, a:iteration)
echohl Comment
echo "ChatBot: Oh, thanks, that's a good way to respond to that! I'll remember it!\n"
echohl None
@@ -189,26 +189,26 @@ endfunction
function! MainChatLoop()
let s:ChatIteration = 1
- let s:more_status = &more
+ let more_status = &more
setlocal nomore
echohl Comment
echo "Welcome to Agetian's ChatBot for Vim v" . s:BotVersion . "! Enjoy and have fun!\n"
echo "Type /Q to choose not to answer the bot's question or to end the conversation.\n"
echohl None
while 1
- let s:HumanResponse = input("You say: ")
- if s:HumanResponse =~ "^\\s*$"
+ let HumanResponse = input("You say: ")
+ if HumanResponse =~ "^\\s*$"
continue
endif
- if s:HumanResponse == "/Q"
+ if HumanResponse == "/Q"
echo "ChatBot: Bye-bye!\n"
break
endif
- echo "Human: " . Macroexpand(s:HumanResponse) . "\n"
- call AI_Respond(s:HumanResponse, s:ChatIteration)
+ echo "Human: " . Macroexpand(HumanResponse) . "\n"
+ call AI_Respond(HumanResponse, s:ChatIteration)
let s:ChatIteration += 1
endwhile
- let &more = s:more_status
+ let &more = more_status
endfunction
nnoremap <silent> \Cb :call MainChatLoop()<CR>
@@ -228,6 +228,7 @@ Not a whole lot!
Not much!
How's it going?:::1
+Good!
Doing good, thanks!
Good, thanks!

0 comments on commit 877409f

Please sign in to comment.