Question: Why is the function ` wordsnopunc / 1 ` ( replacing all punctuation with spaces before breaking into words ) failing? How can I edit

Why is the function `wordsnopunc/1`(replacing all punctuation with spaces before breaking into words) failing? How can I edit my code to create a secondary function that returns the first n words of the text?
Now replace all punctuation with spaces before breaking into words. The function should be called wordsnopunc/1.
8%CONSULT
sYour code here
wordsnopunc(X) :-
open('alice.txt', read, Str),% Step 1: Open the file for reading
read_string(Str,_, Input),% Step 2: Read the entire content of the file into the Text variable
string_chars(Input, CharList),% Convert input string to list of characters
replace_punctuation(CharList, CleanList),% Replace punctuation with spaces
string_chars(CleanString, CleanList),% Convert back to string
split_string(CleanString,"","", Words).% Split the string into words
close(Str).% Step 5: Close the file stream
Prolog: Warning: /tmp/tmp4lvgjrwj.pl:2:
Prolog: Warning: Singleton variables: [X,Words]
Prolog: Warning: /tmp/tmp4lvgjrwj.pl:9:
Prolog: Warning: Singleton variables: [Str]
Prolog: ERROR: /tmp/tmp4lvgjrwj.pl:9:
Prolog: ERROR: No permission to modify static procedure `close/1'
true
wordsnopunc(X), X >30500, X 30600.
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/swi_prolog_kernel/kernel.py", line 140, in do_execute
self._execute(code)
File "/usr/local/lib/python3.8/dist-packages/swi_prolog_kernel/kernel.py", line 100, in _execute
result = self._thread.query(code)
File "/usr/local/lib/python3.8/dist-packages/swiplserver/prologmqi.py", line 682, in query
return self._return_prolog_response()
File "/usr/local/lib/python3.8/dist-packages/swiplserver/prologmqi.py", line 829, in _return_prolog_response
raise PrologError(jsonResult)
swiplserver.prologmqi.PrologError: existence_error(procedure,'/'(replace_punctuation, 2))
Question #5
Now rewrite wordsnopunc above as wordsn/2 to return the first n words of the text. It should take two arguments: n and the returned text.
%%CONSULT
%Your code here
wordsn(
5,
["The", "Project", "Gutenberg", "eBook", "of"]
).
Why is the function ` wordsnopunc / 1 ` (

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!