How can I produce a count on the number of times each word has occurred in the following

Edward Crockett

Using the code below in Jupyter notebook, I can only produce a count of each character found. But I am looking to get a count on the number of times each word occurs. Thank you!

from bs4 import BeautifulSoup as Soup, Tag
import re
import requests
from collections import Counter

url = "http://en.wikipedia.org/wiki/October_27"
DayBorn = [] # create a list to save the soup contents 
response = requests.get(url)
soup = Soup(response.content)


births_span = soup.find("span", {"id": "Births"}) # find where the first instance of span with ID of births appears
births_ul = births_span.parent.find_next_sibling() # find the parents next sibling which is ul (unordered list)

for item in births_ul.findAll('li'): # find all the occurrences of li within births_ul
    if isinstance(item, Tag): 
        #print(item.text) # if the next item found is a 'li' then print the value of its text
        DayBorn.append(item.text)

This next section gives me a list of each word as it occurs.

text_iterated = str(DayBorn) 
[x for x,y in re.findall(r'((\w+[^,.()]))', text_iterated)]

I have tried both these methods so far

Counter(str(text_iterated))

and

occurrences = Counter()
for word in str(DayBorn):
    occurrences[word] += 1
occurrences  

They result in the same thing, a count of each number/letter e.g.

counter({'[': 4,
         "'": 449,
         '8': 104,
         '9': 277,
         '2': 109,
         ' ': 2237,
         '–': 225,
         'E': 50,
Prune

You very specifically told your program to iterate through the characters of the list you created:

for word in str(DayBorn):

You converted the list to its string-output form, and then iterated through the characters of that string. Instead,

for word in DayBorn:

Better yet, simply use the provided Python facility for counting:

from collections import Counter
...
occurrences = Counter(DayBorn)

EDIT per USER COMMENT

DayBorn needs to be a list of words. Again, we need your MVE. Perhaps this will help as you ingest lines: instead of adding the entire line to your list

    DayBorn.append(item.text)

... add the words individually

    DayBorn.extend(item.text.split())

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is it possible to count the number of times each key has occurred in a column of JSON?

How can I count the number of relationships each node has in Cypher?

How can I print out the number of times each unique word appears in a inputted string?

Count Number of Times a Mouse Wheel Scroll Event has Occurred

How can I count the number of times one item has been grouped together with another in R?

Count number of times a char has appeared in Tables that has occurred less than a number of times

Javascript regular expression to match words and count the number of times each word has occured

How to count how many times a result has occurred?

How can I count the number of times I call a loss function?

How can i get the number of times a word is in an array?

Assuming each letter in a word has an assigned number, like 'a' = 3, 'b' = 2, how can I print the sum of all letters in a word?

Can I use a Slack bot to count how many times each user has posted a message in multiple channels without joining those channels?

How to count the number of times a word is in the text file

How can I count the number of times a byte sequence occurs in a file?

How can i print number of times each alphabet occurs in the string?

Is there a way to count the number of times a word has repeated in a string?

Using StringBuilder and BufferedReader, how can I add commas after each word in a text file and produce a string?

How to count the number of appearances of a word in each line

How can I count the number of time that was executed retry operator wether the exception of an error occurred

How can I count the number of comments and likes that a post has in MySQL?

How can I count the number of columns a value has after a Where?

How can I count the number of messages in one channel of each user?

SQL How can I count the number of meetings for each client?

How Can I count the number of Capital Cities within each Continent?

how I can count the number of positive value in each column?

Pandas count the number of times an event has occurred in last n days by group

R: how do I count the number of times a value has already appeared in a vector (or the number of times it appears to the left of that value)?

How do I find the number of times a word has been repeated in a tweepy stream?

Count number of times a word appears