r/learnpython • • 8d ago

Can anyone help me with my homework

I’m taking into to programming right now and I’m very confused on what to do and my professor is no help can anyone help explain to me what to do for these questions. The question are

  1. Write a script that determines on its own whether each text is spam or ham WITHOUT using the first column at all. You should only be reading from the second column of the file as you make this decision. The decision will be a sequence of if statements that you write. Try to determine what generic qualities of a text message determine if it is spam or not. One possible rule would be "contains two or more fully capitalized words." Read through the file and see if you can come up with the best rules.
  2. Once you've come up with your rules, determine how good they are! Compare whether your script got the decision right. Add up the number of correct classifications and divide that by the total lines in the file. This will give you the accuracy of your script. Output this value. If you are not happy with it, edit your rules. When you submit, ensure your accuracy is at least 90%
    link to data set

Any help would be really appreciated.

0 Upvotes

7 comments sorted by

9

u/aqua_regis 8d ago

Start by telling and showing us what you have tried and where exactly you are stuck.

Can you read a line in a file? Can you test for two or more fully capitalized words?

2

u/Cold_Arachnid_2617 8d ago

You lazy student, do your own homework!

1

u/Sure-Passion2224 8d ago

Looks like you need to read a text file line by line, and each line represents 2 or more columns of data, possibly comma or tab delimited text. You are to parse each line and apply some sort of logic to a specific column, then document the results. Be aware of a possible need to either account for or ignore UPPER/lower case.

You should work with the language(s) discussed in class but this is relatively easy as either a bash shell script, or using Perl.

Once you can define the process this is a quickie in ChatGPT but you damned well better be able to describe what each line of generated code is doing. When you eventually (hopefully) land a coding job you will have to be able to discuss your code in peer review.

1

u/MarsupialLeast145 8d ago

You need to parse a CSV and read just the data in the second column.

For every row it reads, you need to validate it against a set of rules (probably each in their own function to be manageable and follow SOLID principles)

These rules allow you to classify the text, so each function might return true or false, e.g. true for SPAM, and if you get a single false, you need to return to the main loop, and identify the text as SPAM.

You have over 200 rows, so you should probably have an additional check against column one to see if the guess was correct at the end and output a percentage. Don't fail anything if you get this wrong, just always output a percentage at the end so you can see it go up.

Start by getting this working with just a dummy check. It will output 0% correct.

As you progress, add a new check, make sure the percentage goes up.

1

u/mjmvideos 8d ago

This seems relatively straightforward.

  1. I need to write a script.
  2. The script should look only at column 2 (So that means I need to parse the data to find columns. Is it CSV? I can use a Python module to do that.
  3. For each line I read I should decide by looking at the data in column 2 how to classify it. The instructions say this should be a series of “if” statements. So I’ll need to create a series of if statements. And I’ll need to look through the data file to see if I can figure out some rules that would work. Look for characteristics that lots of lines have in common. (He gives an example- 2 or more fully capitalized words. What other things do you notice? )
  4. After you have your script running, add some code that uses Column 1 to compare your characterization to the actual. Keep track of how many you got right and wrong. After processing all the lines in the file compute the percentage you got right and wrong and print it out.
  5. If the rules you found and coded produce a correct percentage less than 99%. Look at the ones you got wrong and see if you can find another rule to add that will increase your score.

1

u/patryk_builds 8d ago

The task sounds bigger than it is. break it into three small steps and do them in order.

  1. Just read the file and print the second column of the first ten lines. nothing else. if that works, you've done the scariest part
  2. Write one function, something like is_spamtext, that returns True or False from a single rule. the assignment gives you the first one: count the words that are fully uppercase and return True if there are two or more. text.split() gives you the words, word.isupper tells you if one is all caps
  3. Loop over every line, call your function, and compare the answer with the first column. you only use that column here, for checking, never inside is_spam. count the matches, divide by the total, print it

Then improve the rules by looking at what you got wrong. print the spam messages your function missed and read twenty of them. you'll see patterns fast: words like free, win, call now, long numbers, links, currency signs. each pattern is one more if. One tip before you start: check what accuracy you get by answering ham for everything. in most of these datasets spam is a small share, so that baseline is already high, and it tells you how far your rules really have to carry you to reach 90