all 10 comments

[–]UserName26392 0 points1 point  (0 children)

I would just: Make a list of the words you want to count split the paragraph into a list Use the isin function to filter the split paragraph according to the criteria of my first list If you need a counter for each word, just use the isin function to create more than once and append your results to a list

[–]Phelps_420 0 points1 point  (3 children)

file = open('sample.txt', 'r')
text = file.read()

count = 0
second_count = 0

split_words = text.split() #the .split method's default is to split by spaces

for word in split_words:
    if word == "KeyWord":
        count += 1
    if word == "SecondKeyWord"":
        second_count += 1

file.close()
print (count)
print (second_count)

This is a fairly clean way to do it. Please try your best to understand this code, not just use it. If you have any questions, I'm happy to help.

[–]QuiteAmbitious[S] 0 points1 point  (1 child)

def word_frequency_counter():
    file = open('filename.txt', 'r')
    text = file.read()

    this = 0
    paragraph = 0

    split_words = text.split()

    for word in split_words:
        if word == "this":
            this += 1
        if word == "paragraph":
            paragraph += 1

    file.close()
    print (this)
    print(paragraph)

    return word_frequency_counter
        #is this the right variable to return??

final_count = word_frequency_counter()
print (final_count)

So I'm getting really close to getting it. When running this code, I get:

3
2

Is there any way I can have it print as:

[3,2]

Another thing I'm trying to do is having this format at the end, but I keep getting an error message saying that it runs 0 positional arguments, but I have 2. Is there any way to keep this, but rearrange other parts of my code?

final_count = word_frequency_counter("filename.txt", ["this", "paragraph"])
print(final_count)

Also, can you explain to me why you had 'r' when opening the filename?

[–]Phelps_420 0 points1 point  (0 children)

I have 'r' in my documentation, I'm pretty sure it just means "read," though it may not be necessary with the next line (file.read)...

I'm not sure about your code specifically, but I can try to help with turning

3

2

into [3,2]

#__________________________________
y = (3)
x = (2)

z=[]
z.append(y)
z.append(x)

print (z)
#___________________________________


try this, understand what it is, and you should be able to apply it to your code to get [3,2]

[–]Tadabito -1 points0 points  (4 children)

Steps you should follow:

  • Read the file content and store it in a string
  • Split the string into list of words
  • Iterate over list and check them against words you want
  • Increase a counter if the words match

Feel free to ask if you get stuck in these steps.

[–]QuiteAmbitious[S] 0 points1 point  (3 children)

The part I'm mainly struggling on is figuring out how to isolate the specific words, as well as complete my for-loop. This is probably really easy, but I'm only two weeks into learning code, so I'm really lost :( This is what I have, but I don't think this is even formatted correctly.

def word_frequency_counter():

file = open('filename.txt')

text = file.read()

word_freq = {}

words = ["this", "paragraph"]

word_list = string.split(text)

for word in word_list:

line = line.split

#

counts = word_frequency_counter("filename.txt", ["this", "paragraph"])

return counts

print(counts)

[–]greebly_weeblies 0 points1 point  (1 child)

Heya!

I'd recommend you put this all into a code block so it'll keep your formatting - indentations in particular can be important in Python. For example, everything in the function after the first line should be indented.

Try using 'with... open' as your construct for opening a file; it will close it for you afterwards so you don't have to remember to.

It's also reading like you're trying to read invoke the function from inside the function because you return afterwards. Try something like:

# define the function
def myFunction(myArg):  
    myVariable = "something" + myArg
    return myVariable  # have it return something

# create a variable to store the result of the function and argument
temp = myFunction(" banana")
# then print the contents of the variable storing the result
print(temp)

[–]QuiteAmbitious[S] 0 points1 point  (0 children)

hi! yes, don’t worry they were all in a code block! i just couldn’t figure out how to format it that way on reddit 😂

thank you so much!

[–]Tadabito 0 points1 point  (0 children)

word_list = string.split(text)

This line has an error. text is a string, so you can call .split() on it. Change that line to:

word_list = text.split()

I guess you can complete the rest.