Skip to content

Instantly share code, notes, and snippets.

@apua
Forked from fkztw/korean_fish.py
Last active June 10, 2021 11:05
Show Gist options
  • Save apua/2f108640a182c01b08ccb9f89d6e6696 to your computer and use it in GitHub Desktop.
Save apua/2f108640a182c01b08ccb9f89d6e6696 to your computer and use it in GitHub Desktop.
Yet another chatbot
# Julia 1.0.3
buzzwords = (
"自經區", "自貿區",
"摩天輪", "愛情摩天輪", "愛情產業鏈",
"發大財", "愛河的水甘甘", "選總統",
"迪士尼",
"F1", "F1賽車場", "F1 賽車場",
"賭馬", "賽馬",
"九二共識", "一中各表", "一國兩制", "兩岸統一",
)
question_patterns(keyword) = Iterators.flatten((
Iterators.product(["請問什麼是", "什麼是", ""], [keyword], ["的具體內容", "的內容", ""], ["", "?", "?"]),
Iterators.product(["請問", ""], [keyword], ["的具體內容", "的內容", ""], ["是什麼", "是", ""], ["", "?", "?"]),
))
known = Dict((patt, word) for word in buzzwords for patt in map(join, question_patterns(word)))
function answer(question)
buzzword = get(known, question, nothing)
if buzzword !== nothing
"總目標是高雄要發大財,這個$(buzzword)只是其中一部份,好不好?謝謝。"
else
"你說的不是重點,重點是高雄要發大財。"
end
end
function conversation()
while true
question = begin
line = (print("議員請發問:"); readline(keep=true))
isempty(line) ? #= sit down =# break : chomp(line)
end
!isempty(question) && print(answer(question), "\n\n")
end
end
conversation()
# Python 3.6+
import itertools
buzzwords = (
'自經區', '自貿區',
'摩天輪', '愛情摩天輪', '愛情產業鏈',
'發大財', '愛河的水甘甘', '選總統',
'迪士尼',
'F1', 'F1賽車場', 'F1 賽車場',
'賭馬', '賽馬',
'九二共識', '一中各表', '一國兩制', '兩岸統一',
)
question_patterns = lambda keyword: itertools.chain(
itertools.product(['請問什麼是', '什麼是', ''], [keyword], ['的具體內容', '的內容', ''], ['', '?', '?']),
itertools.product(['請問', ''], [keyword], ['的具體內容', '的內容', ''], ['是什麼', '是', ''], ['', '?', '?']),
)
known = {patt: word for word in buzzwords for patt in map(''.join, question_patterns(word))}
def answer(question):
buzzword = known.get(question)
if buzzword is not None:
return f'總目標是高雄要發大財,這個{buzzword}只是其中一部份,好不好?謝謝。'
else:
return '你說的不是重點,重點是高雄要發大財。'
def conversation():
while True:
try:
question = input('議員請發問:')
except EOFError: # sit down
break
if question:
print(answer(question), end='\n\n')
if __name__ == '__main__':
conversation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment