In a multi-line text file, determine the number of words in each line and enter information about this in a separate line of the new file in the form: line # contains words.The file contains only words, for example "Marry Christmas" is 2 words.
Asked
Active
Viewed 134 times
-1
-
This can be very easy (see the comment under the answer by @Deffa) or maybe not. How do you define a word? Do you need to count how many times each word appears in the file? Also, please show some attempt at solving it. Downvoting until this question is possible to answer. (You can edit your question with more details and code.) – TA_intern Nov 29 '21 at 12:22
-
To be honest, I have no idea how to do this yet. According to the assignment, you need to count the number of words in a line of the file and display this number – Марьяна Nov 30 '21 at 09:07
-
If you want useful help you need to do two things. 1. Understand the problem statement and transmit it faithfully. 2. At least attempt something on your own. And beware, the only answer you've got so far has bugs in it, see my comments. – TA_intern Nov 30 '21 at 09:16
-
Thank you, I will write you a full assignment, maybe you can help me. In a multi-line text file, determine the number of words in each line and enter information about this in a separate line of the new file in the form: line #
contains – Марьяна Nov 30 '21 at 10:04words -
So you need to create a new file, with exactly as many lines as the original? How about the definition of "word". How many words we have here: `a b:c`? How about here: `a+b = c`? And please update your question, do not add details in the comments – TA_intern Nov 30 '21 at 10:22
-
You need to be able to define what makes a word. Are they just separated by white space? – TA_intern Nov 30 '21 at 13:35
-
Yes, the words are just separated by a space. That is, for example, in our file there are several lines, on each line there is a set of words. You need to output information about each line to a new file, for example: line 1 contains 5 words\n line 2 contains 1 word – Марьяна Nov 30 '21 at 15:28
-
By "a space" or by any number of spaces? – TA_intern Dec 01 '21 at 06:56
-
Normal space, it's only one between words – Марьяна Dec 01 '21 at 08:51
-
The file looks like this: line 1: It snowed today (4 words), line 2: I woke up at 12 o'clock (6 words) – Марьяна Dec 01 '21 at 10:30
-
so if you have two consecutive white spaces you will have a 0-length word between the two spaces that you need to count? – TA_intern Dec 02 '21 at 10:53
-
No, it is assumed that there cannot be more than 1 consecutive space. – Марьяна Dec 02 '21 at 11:18
-
so should the program break if t here more than one space? Or should it just take multiple spaces as one? – TA_intern Dec 02 '21 at 12:50
-
Use multiple spaces as one or you can just display a warning that there are many spaces and exit the program, it seems to me that it will be easier to write code – Марьяна Dec 02 '21 at 15:10
-
How are you going to read the file? Line by line, writing to the other file? – TA_intern Dec 03 '21 at 07:14
-
To be honest, I tried to read from a file, but it did not work for me, I have never programmed in functional languages, can you show the code how to at least approximately complete my task? – Марьяна Dec 03 '21 at 10:29
-
Did you see the answer you got already? There is some link there. But it also depends on what predicates you are supposed to be using for this, it depends on your learning materials. – TA_intern Dec 03 '21 at 13:29
1 Answers
-1
You can see this answer on how to read a file: Post Once the lines are read, counting is pretty straight forward. Iterate over all lines and split them with split_string/4:
lines(["word1 word2", "word3 word4"]).
word_count(C):- lines(LS), %read file here instead
count(LS, C).
count([], 0).
count([L|LS], C) :- split_string(L, " ", "", W),
length(W, N1), count(LS, N2), C is N1 + N2.

Deffa
- 194
- 6
-
At that point you could just read the whole file to a string, split it, then use length. Why the recursion? Like this: `read_string(Input, _, S), split_string(S, "\s\t\n", "\s\t\n", W), length(W, N).` – TA_intern Nov 29 '21 at 12:17
-
What will happen if you have several white spaces after each other? Try: `split_string("a b", " ", "", W).` – TA_intern Nov 29 '21 at 12:20