Advertisement
Rahulpck

Untitled

May 21st, 2024
740
0
1 day
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 9.73 KB | Source Code | 0 0
  1. Aim: Write a program to implement Water Jug Problem.
  2. Source code
  3. Domains
  4. A,B=integer
  5. Predicates
  6. go
  7. water(A,B)
  8. clauses
  9. go:-
  10. write("enter capacity of jug A"),
  11. readint(A),nl,
  12. A<=4,
  13. write("enter capacity of jug B"),
  14. readint(B),nl,
  15. B<=3,
  16. write(A," ",B),nl,
  17. water(A,B).
  18. water(2,0):-
  19. write("goal achieved"),nl.
  20. water(2,_):-
  21. write("2_0"),nl,
  22. water(2,0).
  23. water(0,2):-
  24. write("2_0"),nl,
  25. water(2,0).
  26. water(_,2):-
  27. write("0_2"),nl,
  28. water(0,2).
  29. water(4,2):-
  30. write("0_2"),nl,
  31.  
  32. water(0,2).
  33. water(3,3):-
  34. write("4_2"),nl,
  35. water(4,2).
  36. water(3,0):-
  37. write("3_3"),nl,
  38. water(3,3).
  39. water(0,3):-
  40. write("3_0"),nl,
  41. water(3,0).
  42. water(4,3):-
  43. write("0_3"),nl,
  44. water(0,3).
  45.  
  46. Output
  47.                                
  48.  
  49. Program 3
  50. Aim: Write a program to find a disease from given symptoms.
  51. Source Code
  52.  
  53. domians
  54. A,B,C=string
  55. predicates
  56. go
  57. disease(A,B,C)
  58. clauses
  59. go:-
  60. write("Enter the first symptoms: "),
  61. readln(A),nl,
  62. write("Enter the second symptoms: "),
  63. readln(B),nl,
  64. write("Enter the third symptoms: "),
  65. readln(C),
  66. disease(A,B,C).
  67. disease("cough","runny nose","fever"):-
  68. write("common cold"),nl.
  69. disease("sore throat","cold","congestion"):-
  70. write("Seasonal Allergy"),nl.
  71. disease("dizzy","dehydration","hyperthermia"):-
  72. write("sun stroke"),nl.
  73. disease("cold","hypothermia","fever"):-
  74. write("frostbite"),nl.
  75.  
  76.  
  77.  
  78. Program 4
  79. Aim: Write a program to implement Monkey Banana Problem.
  80.  
  81. Source Code
  82. domains
  83. Height,Jump,Stick,Table=integer
  84. predicates
  85. go
  86. attempt(integer,integer,integer,integer,integer)
  87. task(integer)
  88. clauses
  89. go:-
  90. write("1.monkey jump"),nl,
  91. readint(Jump),
  92. write("2.Stick Length"),nl,
  93. readint(Stick),
  94. write("3.Height of ceiling"),nl,
  95. readint(Height),
  96. write("4.Height of table"),nl,
  97. readint(Table),
  98. attempt(_,Height,Jump,Stick,Table),
  99. readint(_).
  100. attempt(0,Height,Jump,,):-
  101. write("monkey jumped to catch banana"),nl,
  102. Height<=Jump,
  103. task(1).
  104. attempt(1,Height,Jump,Stick,_):-
  105. write("monkey jumped with stick to catch banana"),nl,
  106. Height<=Jump+Stick,
  107. task(1).
  108. attempt(2,Height,Jump,Stick,Table):-
  109. write("monkey jumped with table having stick to catch banana"),nl,
  110. Height<=Jump+Stick+Table,
  111. task(1).
  112. attempt(3,Height,Jump,_,Table):-
  113. write("monkey jumped with table to catch banana"),nl,
  114. Height<=Jump+Table,
  115. task(1).
  116. attempt(4,Height,Jump,Stick,Table):-
  117. write("monkey jumped with table having stick to catch banana"),nl,
  118. Height>=Jump+Table+Stick,
  119. write("unsuccessful").
  120. task(1):-
  121. write("successful").
  122.  
  123. Output
  124.  
  125.  
  126.  
  127.  
  128. Program 5
  129. Aim: Write a program to implement Travelling Salesman Problem.
  130. Source Code
  131. domains
  132. A,B,C,D=symbol
  133. predicates
  134. go
  135. route(symbol,symbol,integer)
  136. direct(symbol,symbol,integer)
  137. indirect(symbol,symbol,integer)
  138. clauses
  139. route(a,a,0).
  140. route(a,b,5).
  141. route(a,c,6).
  142. route(b,b,0).
  143. route(c,c,0).
  144. route(a,d,7).
  145. route(d,d,0).
  146. route(b,d,2).
  147. route(c,d,1).
  148. go:-
  149. write("enter first city"),nl,
  150. readln(A),
  151. write("enter destination city"),nl,
  152. readln(B),
  153. direct(A,B,X),
  154. indirect(A,B,Y),
  155. X>Y,
  156. write("choose indirect path"),nl,
  157. Y>X,
  158. write("choose direct path"),nl.
  159. direct(A,B,X):-
  160. route(A,B,X),
  161. write("successfull"),nl.
  162. indirect(A,B,Y):-
  163. route(A,R,T),
  164. route(R,B,Z),
  165. write("successfull"),
  166. Y=T+Z.
  167.  
  168.  
  169.  
  170.  
  171. Output
  172. Program 6
  173.  
  174. Aim: Write a program to implement Towers of Hanoi Problem.
  175.  
  176. Source Code
  177. domains
  178. N,J=integer
  179. FROM,TEMP,TO=char
  180. predicates
  181. go
  182. transfer(integer,char,char,char)
  183. clauses
  184. go:-
  185. write("enter no of disks"),nl,
  186. readint(N),
  187. transfer(N,'A','B','C').
  188. transfer(N,FROM,TO,TEMP):-
  189. N>0,
  190. J=N-1,
  191. transfer(J,FROM,TEMP,TO),
  192. write("add disk",J,"from",FROM,"to",TO),nl,
  193. readchar(_),
  194. transfer(J,TEMP,TO,FROM),
  195. true.
  196. transfer(N,FROM,TEMP,TO):-
  197. true.
  198.  
  199. Output
  200.  
  201. Program 7
  202.  
  203. Aim:  Write a program to implement Depth First Search.
  204.  
  205. Source Code
  206. domains
  207. A,B,X,Y=symbol
  208. L=symbol*
  209. predicates
  210. go
  211. childnode(X,Y)
  212. child(X,Y,L)
  213. path(X,Y,L)
  214. clauses
  215. go:-
  216. write("Enter point: "),
  217. readln(A),nl,
  218. write("Exit point: "),
  219. readln(B),nl,
  220. childnode(a,b).
  221. childnode(a,c).
  222. childnode(c,d).
  223. childnode(c,e).
  224. path(A,B,[A|L]):-
  225. child(A,B,L).
  226. child(A,B,[B|[]]):-
  227. childnode(A,B),!.
  228. child(A,B,[X|L]):-
  229. childnode(A,X),
  230. child(X,B,L).
  231.  
  232.  
  233. Output
  234. AIM: Write a python program to implement hangman game.
  235.  
  236. SOURCE CODE
  237. #importing the time module
  238. import time
  239. import random
  240.  
  241. #welcoming the user
  242. name = input("What is your name? ")
  243.  
  244. print("Hello, " + name, "Time to play hangman!")
  245.  
  246. #wait for 1 second
  247. time.sleep(1)
  248.  
  249. print("Start guessing...")
  250. time.sleep(0.5)
  251.  
  252. # List of words to choose from
  253. words = ["python", "hangman", "programming", "computer", "science", "algorithm"]
  254.  
  255. # Randomly select a word from the list
  256. word = random.choice(words)
  257.  
  258. #creates an variable with an empty value
  259. guesses = ''
  260.  
  261. #determine the number of turns
  262. turns = 10
  263.  
  264. # Create a while loop
  265.  
  266. #check if the turns are more than zero
  267. while turns > 0:
  268.  
  269.     # make a counter that starts with zero
  270.     failed = 0
  271.  
  272.     # for every character in secret_word    
  273.     for char in word:
  274.  
  275.         # see if the character is in the players guess
  276.         if char in guesses:    
  277.  
  278.             # print then out the character
  279.             print(char, end=" ")
  280.  
  281.         else:
  282.  
  283.             # if not found, print a dash
  284.             print("_", end=" ")
  285.  
  286.             # and increase the failed counter with one
  287.             failed += 1    
  288.  
  289.     # if failed is equal to zero
  290.  
  291.     # print You Won
  292.     if failed == 0:        
  293.         print("\nYou won")
  294.         break  # exit the script
  295.  
  296.     # ask the user to guess a character
  297.     guess = input("\nguess a character: ").lower()
  298.  
  299.     # set the players guess to guesses
  300.     guesses += guess
  301.  
  302.     # if the guess is not found in the secret word
  303.     if guess not in word:  
  304.  
  305.         # turns counter decreases with 1 (now 9)
  306.         turns -= 1        
  307.  
  308.         # print wrong
  309.         print("Wrong")  
  310.  
  311.         # how many turns are left
  312.         print("You have", turns, 'more guesses' )
  313.  
  314.         # if the turns are equal to zero
  315.         if turns == 0:          
  316.  
  317.             # print "You Lose"
  318.             print("You Lose")
  319.  
  320.  
  321.  
  322.  
  323.  
  324. Write a program for Face Detection using machine learning.
  325.  
  326. SOURCE CODE
  327. import cv2
  328. import mediapipe as mp
  329. mp_face_detection = mp.solutions.face_detection
  330. mp_drawing = mp.solutions.drawing_utils
  331.  
  332. # For static images:
  333. IMAGE_FILES = []
  334. with mp_face_detection.FaceDetection(
  335.     model_selection=1, min_detection_confidence=0.5) as face_detection:
  336.   for idx, file in enumerate(IMAGE_FILES):
  337.     image = cv2.imread(file)
  338.     # Convert the BGR image to RGB and process it with MediaPipe Face Detection.
  339.     results = face_detection.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
  340.  
  341.     # Draw face detections of each face.
  342.     if not results.detections:
  343.       continue
  344.     annotated_image = image.copy()
  345.     for detection in results.detections:
  346.       print('Nose tip:')
  347.       print(mp_face_detection.get_key_point(
  348.           detection, mp_face_detection.FaceKeyPoint.NOSE_TIP))
  349.      
  350. mp_drawing.draw_detection(annotated_image, detection)
  351.     cv2.imwrite('/tmp/annotated_image' + str(idx) + '.png', annotated_image)
  352.  
  353. # For webcam input:
  354. cap = cv2.VideoCapture(0)
  355. with mp_face_detection.FaceDetection(
  356.     model_selection=0, min_detection_confidence=0.5) as face_detection:
  357.   while cap.isOpened():
  358.     success, image = cap.read()
  359.     if not success:
  360.       print("Ignoring empty camera frame.")
  361.      
  362.       continue
  363.  
  364.     # To improve performance, optionally mark the image as not writeable to
  365.     # pass by reference.
  366.     image.flags.writeable = False
  367.     image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  368.     results = face_detection.process(image)
  369.  
  370.     # Draw the face detection annotations on the image.
  371.     image.flags.writeable = True
  372.     image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
  373.     if results.detections:
  374.       for detection in results.detections:
  375.        
  376.  mp_drawing.draw_detection(image, detection)
  377.    
  378. # Flip the image horizontally for a selfie-view display.
  379.     cv2.imshow('MediaPipe Face Detection', cv2.flip(image, 1))
  380.     if cv2.waitKey(5) & 0xFF == 27:
  381.       break
  382. cap.release()
  383.  
  384.  
  385.  
  386. PROGRAM NO – 10
  387. AIM: Write a program for Text Classification for the given sentence using NTLK Library.
  388.  
  389. SOURCE CODE
  390. import nltk
  391. from nltk.corpus import stopwords
  392. from nltk.tokenize import word_tokenize
  393. from nltk.stem import WordNetLemmatizer
  394. from nltk import NaiveBayesClassifier
  395. from nltk.classify import accuracy
  396.  
  397. # Sample training data
  398. training_data = [
  399.     ("I love this sandwich.", "positive"),
  400.     ("This is an amazing place!", "positive"),
  401.     ("I feel very good about these beers.", "positive"),
  402.     ("This is my best work.", "positive"),
  403.     ("What an awesome view", "positive"),
  404.     ("I do not like this restaurant", "negative"),
  405.     ("I am tired of this stuff.", "negative"),
  406.     ("I can't deal with this", "negative"),
  407.    ("He is my sworn enemy!", "negative"),
  408.    ("My boss is horrible.", "negative")
  409. ]
  410. # Preprocessing function
  411.  
  412.  
  413. def preprocess(text):
  414.    lemmatizer = WordNetLemmatizer()
  415.    stop_words = set(stopwords.words('english'))
  416.    words = word_tokenize(text.lower())
  417.    filtered_words = [lemmatizer.lemmatize(w) for w in words if w.isalnum() and w not in stop_words]
  418.    return dict([(word, True) for word in filtered_words])
  419.  
  420. # Preprocess and label the training data
  421. processed_training_data = [(preprocess(text), label) for text, label in training_data]
  422.  
  423. # Train the Naive Bayes classifier
  424. classifier = NaiveBayesClassifier.train(processed_training_data)
  425.  
  426. # Test sentence
  427. test_sentence = "This sandwich is not good."
  428.  
  429. # Preprocess the test sentence
  430. processed_test_sentence = preprocess(test_sentence)
  431.  
  432. # Classify the test sentence
  433. classification = classifier.classify(processed_test_sentence)
  434.  
  435. print("Test Sentence:", test_sentence)
  436. print("Classification:", classification)
  437.  
  438.  
  439.  
Tags: ML
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement