Ai NKL
Ai NKL
OF TECHNOLOGY
ARTIFICIAL INTELLIGENCE
LAB FILE
(AI-302P)
THEORY:
1. Introduction to PROLOG:
PROLOG is widely used in fields like knowledge representation, automated reasoning, expert
systems, and natural language understanding.
(ii) Processor:
(iii) Memory:
• For macOS, you may need XQuartz to run the development tools.
• For Linux, you might need to add a PPA or use package
managers like apt, yum, or snap.
You can download the SWI-Prolog compiler from the official SWI-Prolog
download page
(i) Facts
Definition: Facts are universally true statements that do not change based on opinion
or interpretation.
Why are they important? They serve as the f oundation of knowledge and decision-
making.
Example:
(ii) Data
Definition: Data consists of raw numbers, symbols, or observations that do not carry
meaning by themselves.
Why is it important? It is the input that, when processed, turns into meaningful
information.
Example:
• A weather station records daily temperatures: 22°C, 24°C, 19°C, 25°C, 21°C.
• A company records sales numbers: 200, 350, 420, 300, 280.
CODE:
Facts:
Queries:
Answer:
Declarative programming focuses on "what" needs to be done rather than "how"
it should be done. PROLOG is a declarative language, meaning that instead of
writing step-by-step instructions, we define facts and rules, and the system figures
out the solution.
Example:
• PROLOG is case-sensitive.
• Atoms (names of predicates and constants) must start with a lowercase letter.
• Variables must start with an uppercase letter or an underscore (_).
Answer:
• Example:
Answer:
• Atoms: Names that begin with a lowercase letter or are enclosed in single
quotes. (e.g., apple, 'New Delhi')
• Variables: Names that begin with an uppercase letter or an underscore. (e.g.,
X, Person)
• Structures: Complex terms made up of a functor and arguments. (e.g.,
student(john, 22).)
Answer:
Backtracking is PROLOG’s way of finding solutions by exploring all possible
options. If a query fails, it goes back to the previous choice point and tries another
path.
Example:
CODE:
QUERIES:
?- celsius_to_fahrenheit(0, F).
?- below_freezing(-5).
?- below_freezing(10).
OUTPUT:
Answer:
• Example:
Answer:
3. What happens if there are multiple matching rules for a query in PROLOG?
• Example:
Answer:
Unification is the process of matching two terms by making their values identical.
• Example:
Answer:
Yes, PROLOG can perform arithmetic using the is operator.
• Example:
CODE:
import string
def remove_punctuation(input_string):
return input_string.translate(translator)
cleaned_text = remove_punctuation(text)
print(cleaned_text)
OUTPUT:
Answer: The translate() method is used to apply the translation table (created by
str.maketrans()) to the string. It processes the string and removes the characters
specified in the translation table (punctuation marks in this case).
4. What happens if there is no punctuation in the input string?
Answer: If there is no punctuation, the function will return the string as it is, without
any modifications.
5. How would you modify the function to remove digits as well as punctuation?
Answer: You could update the string.punctuation by adding string.digits to it, like so:
CODE:
def arrange_sentences(sentences):
return sorted(sentences)
sentences = [
ordered_sentences = arrange_sentences(sentences)
print(sentence)
OUTPUT:
Answer: Arranging sentences helps organize the text logically, either chronologically,
alphabetically, or based on any criteria that makes the text clearer or more coherent.
Answer: You can use Python's sorted() function to sort a list of sentences
alphabetically. It arranges the sentences in ascending order based on lexicographical
order.
Answer: Python provides methods like sorted(), which returns a new sorted list, and
the list.sort() method, which sorts the list in-place.
4. What happens if the sentences have mixed capital and lowercase letters?
5. What will the output be if you try to sort a list of sentences that are already in
order?
Answer: If the sentences are already in alphabetical order, the output will be the same
as the input, as no changes will be necessary.