Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:02:39+00:00 2026-06-15T07:02:39+00:00

Given a decently long text, I need to find how many times a certain

  • 0

Given a decently long text, I need to find how many times a certain word appears into it. Like the sherlock novels, if I type in Sherlock, to give me 200 times or something similar.

So far I know how to read a list with this function I implemented, posted below. I appreciate all the help, don’t know what to do next or how.

read_list(L) :-
read(N), N \= end_of_file
-> L = [N|Ns], !, read_list(Ns)
; L = []
.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:02:40+00:00Added an answer on June 15, 2026 at 7:02 am

    read/1 fetch a term followed by . but for sake of discussion let’s ignore this fact.

    If you are just interested in word frequency, why building a list? just count the words and the matches, and at end of file compute the frequency:

    word_freq(W, Freq) :-
      word_count(W, 0, Total, 0, Match),
      Total > 0 -> Freq is Match / Total.
    
    word_count(W, TotSoFar, Tot, MatchSoFar, Match) :-
      (  read(N),
         N \= end_of_file
      -> T1 is TotSoFar + 1,
         (  N == W
         -> M1 is MatchSoFar+1
         ;  M1 is MatchSoFar
         ),
         word_count(W, T1, Tot, M1, Match)
       ; TotSoFar = Tot,
         MatchSoFar = Match
       ).
    

    test:

    ?- word_freq(a,F).
    |: a.
    |: b.
    |: c.
    |: a.
    |: F = 0.5.
    

    edit Instead of read/1, let’s define a read_word(W), where a word is simply a sequence of alphanumerics

    read_word(SoFar, W) :-
        get_code(C),
        (   C == -1
        ->  ( SoFar == [] -> W = end_of_file ; reverse(SoFar, W) )
        ;   code_type(C, alnum)
        ->  read_word([C|SoFar], W)
        ;   reverse(SoFar, W)
        ).
    

    equipped with such ugly code, and replaced read/1 with read_word/1, we get

    ?- word_freq("ab",F).
    |: a ab abc
    |: F = 0.3333333333333333.
    

    Note that now I’m passing a string, not an atom.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given that the web application doesn't have su privileges, I'd like to execute a
Given a certain date, I want to set the value of a cell with
Given a MySQL table of real estate data, I would like to generate a
Given a document collection in Text::DocumentCollection in Perl, I want to calculate the cosine
Given the following text: |Description=This is the start of the ... ... <strong>description</strong> of
Given a Rails 3 App with a menu like: <ul> <li>Home</li> <li>Books</li> <li>Pages</li> </ul>
Given a generic array T[] , where T extends java.lang.Number , I would like
Given a .so file and function name, is there any simple way to find
Given the following Core Data Model: -> : to-one releationship ->>: to-many relationship Entity
Given the above dynamically generated text (meaning that I can't just use an image),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.