|
Image Recognition
|
bet | 49/182 | Sana | 19.05.2024 | Hajmi | 5,69 Mb. | | #244351 |
Bog'liq Python sun\'iy intellekt texnologiyasi Dasrlik 2024Ma’lumotlarni o‘qish
Bu chatbot.txt faylida o‘qiymiz va butun korpusni keyingi oldindan qayta ishlash uchun jumlalar ro‘yxati va so‘zlar ro‘yxatiga aylantiramiz.
f=open(‘chatbot.txt’,’r’,errors = ‘ignore’)
raw=f.read()
raw = raw.lower()# converts to lowercase
sent_tokens = nltk.sent_tokenize(raw)# converts to list of sentences
word_tokens = nltk.word_tokenize(raw)# converts to list of words
Xom matnni oldindan qayta ishlash
Endi biz tokenlarni kiritish va normallashtirilgan tokenlarni qaytaradigan LemTokens funksiyasini aniqlaymiz.
lemmer = nltk.stem.WordNetLemmatizer()
#WordNet is a semantically-oriented dictionary of English included in NLTK.
def LemTokens(tokens):
return [lemmer.lemmatize(token) for token in tokens]
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))
Kalit so‘zni moslashtirish
Biz bot tomonidan salomlashish funksiyasini belgilaymiz, ya’ni agar foydalanuvchining kiritishi salomlashish bo‘lsa, bot javob qaytaradi. ELIZA salomlashish uchun oddiy kalit so‘zdan foydalanadi. Bu erda biz xuddi shu kontseptsiyadan foydalanamiz.
GREETING_INPUTS = (“hello”, “hi”, “greetings”, “sup”, “what’s up”,”hey”,”yo”)
GREETING_RESPONSES = [“hi”, “hey”, “*nods*”, “hi there”, “hello”, “I am glad! You are talking to me”]
def greeting(sentence):
for word in sentence.split():
if word.lower() in GREETING_INPUTS:
return random.choice(GREETING_RESPONSES)
Javob yaratish
Bu funksiyani belgilaymiz javob U bir yoki bir nechta dasturlashtirilgan kalit so‘z uchun foydalanuvchi kiritganini qidiradi va bir nechta mumkin bo‘lgan javoblardan birini qaytaradi. Agar kalit so‘zlardan birortasiga mos keladigan kiritma topilmasa, u javob qaytaradi:
def response(user_response):
spar_response=’’
sent_tokens.append(user_response)
TfidfVec = TfidfVectorizer(tokenizer=LemNormalize, stop_words=’english’)
tfidf = TfidfVec.fit_transform(sent_tokens)
vals = cosine_similarity(tfidf[-1], tfidf)
idx=vals.argsort()[0][-2]
flat = vals.flatten()
flat.sort()
req_tfidf = flat[-2]
if(req_tfidf==0):
spar_response=spar_response+”I don’t understand you”
return spar_response
else:
spar_response = spar_response+sent_tokens[idx]
return spar_response
Endi biz suhbatni boshlash va yakunlashda foydalanuvchi kiritishi bo‘yicha Bot aytishini xohlagan iboralarni buyuramiz.
flag=True
print(“Spar: My name is Spar. I will answer your queries about Chatbots. If you want to exit, type Bye!”)
while(flag==True):
user_response = input()
user_response=user_response.lower()
if(user_response!=’bye’):
if(user_response==’thanks’ or user_response==’thank you’ ):
flag=False
print(“Spar: You are welcome..”)
else:
if(greeting(user_response)!=None):
print(“Spar: “+greeting(user_response))
else:
print(“Spar: “,end=””)
print(response(user_response))
sent_tokens.remove(user_response)
else:
flag=False
Ekranning ko‘rinishi quyidagicha:
5.1.2-rasm. Chatbotda dastur kodi
|
| |