Author Topic: Ah! Help!  (Read 6325 times)

0 Members and 1 Guest are viewing this topic.

Offline dinosteven

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 194
  • Rating: +10/-1
    • View Profile
Ah! Help!
« on: January 16, 2013, 10:08:50 pm »
Our teacher has been giving us a program to do each day since the start of the year, and I can't quite finish them before the class period is done. I'm falling behind, each day finishing the previous day's program before starting the next. ARRGH - I'm behind 2 programs already after only 8 days! So I'm really sorry for asking for you to do my homework (not entirely, I know it's not right, I want to do it myself), but I need help fast tonight. I don't have a compiler on my computer - well, it's not working - so I can't test.
So the 2 problems are:
Spoiler For Problem1:
Read a text file of Lincoln's Gettysburg Address. Just output the total number of words in it, the total number of unique words ("the" 8 times counts as 1 word), and the top 30 words that occur most frequently.
Special cases are "sixty-three" is 1 word. "joyous - sparkling" as 2 words, " 'tis " as 1 word, "can't" as 1 word, and "The" and "the" are the same, capitals negligible.
So I just need to split the string on spaces, don't add the word if it's a hyphen, convert it to all lowercase, check if it's already in the String Arraylist, and then add it to the arraylist if it meets all the criteria. I can figure out the total number of words by taking the .size() of the Arraylist, and figure out the total number of words by taking the .size() of the array made by the split.
I dunno how to figure out the 30 most frequent, though. Output should be like:
103   the
97    of
59   to
etc.
# words used: 525
# of words: 1577

Spoiler For Problem2:
This one is even easier. I finished this one, but it wasn't working and I couldn't figure out why. Unfortunately, I don't have my code with me.
In a while loop until the user enters in a "?", determine if an input is a valid identifier or not. Criteria for a valid input:
Less than 256 characters; first character is a letter or an underscore, and then followed by numbers, underscores, and digits; no more than one underscore in a row.

This is Java, by the way. My main issue is syntax - I don't know what command does what or how to spell the commands. So I use trial/error until it doesn't give me compile errors. That's what is slowing me down.
Please - I need this soon...

Offline ruler501

  • Meep
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2475
  • Rating: +66/-9
  • Crazy Programmer
    • View Profile
Re: Ah! Help!
« Reply #1 on: January 16, 2013, 10:31:48 pm »
Remember to not only remove spaces, but also punctuation. I'm not sure how to implement it in java, but I know it has a dictionary object so I would loop over each word and assign the word as a key and the value as how many of them there are. Then list the keys by size of the value.
I'm not sure on the second one.
I currently don't do much, but I am a developer for a game you should totally try out called AssaultCube Reloaded download here https://assaultcuber.codeplex.com/
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCM/CS/M/S d- s++: a---- C++ UL++ P+ L++ E---- W++ N o? K- w-- o? !M V?
PS+ PE+ Y+ PGP++ t 5? X R tv-- b+++ DI+ D+ G++ e- h! !r y

Offline DrDnar

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 546
  • Rating: +97/-1
    • View Profile
Re: Ah! Help!
« Reply #2 on: January 17, 2013, 12:13:57 am »
How advanced is the class? What sort of data structures are you supposed to be working with? There are plenty of good guides to basic Java syntax, and Google is your friend for finding out about the Java API.

A dictionary is an abstract data structure in which each unique key maps to one output. For the first problem, you would want a dictionary in which the key is a String (do .toLowerCase() before adding) and the output is an Integer. For each word in the list, check if it is in the dictionary and set the associated Integer object to 1 if not; otherwise, increment the Integer by 1. When you're done, collect the word-number pairs into an array (e.g. by making a new WordPair class which implements Comparable<WordPair> and filling it by iterating over the HashMap<String, Integer> or whatever) and sort it (java.util.Arrays.Sort()). Remember that Comparable<T> is a useful interface.

For the second problem, use a regex. Remember that underscores are a special case; specifically, the regex needs to include the clause that after each underscore, there needs to be one of the allowed characters or nothing at all (that is, an underscore can be at the end of an identifier). Or just iterate over the characters using a for(...){...} loop, checking if the char is valid using a switch{...}.
« Last Edit: January 17, 2013, 12:22:23 am by DrDnar »
"No tools will make a man a skilled workman, or master of defense, nor be of any use to him who has not learned how to handle them, and has never bestowed any attention upon them. . . . Yes, [] the tools which would teach men their own use would be beyond price."—Plato's The Republic, circa 380 BC

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Ah! Help!
« Reply #3 on: January 17, 2013, 09:47:45 am »
Some things that can help you complete each question on time, depending upon the IDE you are using.

Many Java IDE's have code completion and code insertion. So things often used like getters/setters constructors and toString classes can be quickly auto generated. Also, in my course I always used the same programming structure and reused code from previous projects.

If you are not using these things already, I would suggest doing so. It'll help speed up your development speed greatly.

What IDE are you using? Some IDE's can help you work faster than others.

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline pimathbrainiac

  • Occasionally I make projects
  • Members
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1731
  • Rating: +136/-23
  • dagaem
    • View Profile
Re: Ah! Help!
« Reply #4 on: January 17, 2013, 10:01:11 am »
I can't help you on this one... Me no work with string methods.

The conditional statement:

stringName.equalsIgnoreCase(string)

might help, though
I am Bach.

Offline dinosteven

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 194
  • Rating: +10/-1
    • View Profile
Re: Ah! Help!
« Reply #5 on: January 17, 2013, 05:35:36 pm »
I'm using JCreator. The teacher lets us use either JCreator or Eclipse, and I've never learned how to use Eclipse.
JCreator autocompletes parenthesis and curly brackets for me, but that's all I know that it does.
I couldn't get Dictionary to work; my teacher says the's never used it either. I ended up using a String ArrayList with words, and an integer array index of how many times the word occurred. Anyone know how to remove punctuation? Ex "war," becomes "war". I need help with that.
Sorting will be difficult with 2 arrays, so I'll just find maximums 1 at a time.
I'll deal with my second problem later...
Thanks for all the help, guys!

Offline Mighty Moose

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 141
  • Rating: +4/-0
    • View Profile
Re: Ah! Help!
« Reply #6 on: January 17, 2013, 06:04:51 pm »
Just throwing this out there (I don't know how much you know about java), but is there any reason you can't use a StringTokeniser object (java.util.StringTokeniser, I believe)?  That would fit the bill for the first problem, as long as you keep track of repeated words and strip any leading or following punctuation.
« Last Edit: January 17, 2013, 06:05:57 pm by Mighty Moose »
Cheers!
I beta test, so...yeah.  PM me if you want me to test anything :D.

Almost only counts in horseshoes and handgrenades.

Cogito ergo sum.

Calcs:
TI-84+, OS 2.43, Boot Code 1.02, 128k RAM
TI-84+SE VSC, OS 2.43, Boot Code 1.00, 128k RAM  (I'm spoiled :P)
TI-81, OS 1.6K (only borrowed)
Casio fx-CG10 (Prizm), OS 01.04.0200
TI-Nspire Clickpad, OS 1.4
TI-Nspire Clickpad, OS 3.1.0.392
TI-Nspire CAS Clickpad, OS 1.6.10110 (!?) now OS 3.1.0.392

Offline dinosteven

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 194
  • Rating: +10/-1
    • View Profile
Re: Ah! Help!
« Reply #7 on: January 17, 2013, 06:30:59 pm »
I've never even heard of that class... But as the oracle docs says about it, split does the same thing. I used split.
Quote
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
Do you have any idea how one would go about removing puncutation? Maybe a substring...

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Ah! Help!
« Reply #8 on: January 17, 2013, 08:33:42 pm »
how about just run a String.replaceAll(",\\.\\?\"!:;", "") (Plus any other punctuation you want to get rid of) on the string before you parse it?
Edit:  Note that that is a regex pattern, hence having to escape the . and ?
« Last Edit: January 17, 2013, 09:24:08 pm by cooliojazz »
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline dinosteven

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 194
  • Rating: +10/-1
    • View Profile
Re: Ah! Help!
« Reply #9 on: January 17, 2013, 08:52:14 pm »
Ah, thanks!

Offline dinosteven

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 194
  • Rating: +10/-1
    • View Profile
Re: Ah! Help!
« Reply #10 on: January 18, 2013, 02:48:23 pm »
So here is my code so far:

Code: [Select]
import java.util.Scanner;
import java.io.*;
import java.lang.*;
import java.util.ArrayList;
import java.util.List;



public class CoutWords {

public static void main(String[] args) throws IOException {
Scanner in = new Scanner (new File("C:\\Documents and Settings\\100020930\\Desktop\\Lincoln.txt"));
String line;
ArrayList <String> WordList = new ArrayList <String>();
int [] index = new int[154];
while(in.hasNext()) {
line = in.nextLine();
String[] s = line.split("\\s+");
for(String i : s) {
if (!(i.equals("-"))) {
i = i.replace(",","");
i = i.replace(".","");
if (!(WordList.contains(i.toLowerCase()))) {
WordList.add(i.toLowerCase());
}
index[WordList.indexOf(i.toLowerCase())]++;
}
}
}

int sum = 0;
for(int i : index)
sum += i;
System.out.println(sum);
System.out.println(WordList.size());
}

    public CoutWords() {
    }


}
This is the file:
Spoiler For file:
      The Gettysburg Address

   Fourscore and seven years ago our fathers brought forth on
this continent a new nation, conceived in liberty, and dedicated to
the proposition that all men are created equal.

   Now we are engaged in a great civil war, testing whether that
nation, or any nation so conceived and so dedicated, can long endure.
We are met on a great battlefield of that war. We have come to
dedicate a portion of that field as a final resting-place for those
who here gave their lives that the nation might live. It is altogether
fitting and proper that we should do this. But, in a larger sense, we
cannot dedicate, we cannot consecrate, we cannot hallow, this ground.
The brave men, living and dead, who struggled here have consecrated
it, far above our poor power to add or detract. The world will little
note, nor long remember, what we say here, but it can never forget
what they did here. It is for us the living, rather, to be dedicated
here to the unfinished work which they who fought here have thus far
so nobly advanced. It is rather for us to be here dedicated to the
great task remaining before us - that from these honored dead we take
increased devotion to that cause for which they gave the last full
measure of devotion - that we here highly resolve that these dead
shall not have died in vain - that this nation, under God, shall have
a new birth of freedom and that government of the people, by the
people, for the people, shall not perish from the earth.

Offline cooliojazz

  • Support Staff
  • LV7 Elite (Next: 700)
  • *******
  • Posts: 619
  • Rating: +66/-9
  • I omnoms on your soul
    • View Profile
    • Unreal Phantasies
Re: Ah! Help!
« Reply #11 on: January 18, 2013, 06:33:38 pm »
Are you looking for someone to debug it for you?  Finish it?  Are you just posting it to show off the final code?  What are you looking for here? :P
Spoiler For Random signess:
You can not beat my skills.
Trust me.
So don't even try.
And remember never to trust someone who says, "Trust me."

TI File Editor Progress: Remade in java like a boss. 50% we'll call it? IDK =P
Java Libraries: JIRC - 90% JTIF - 5%
TI Projects: Unreal Notator - -5000%
Nomcraft, a Bukkit mod
Some of the music I write can be found here | The Rest Should Be Here (Bandcamp)

Offline dinosteven

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 194
  • Rating: +10/-1
    • View Profile
Re: Ah! Help!
« Reply #12 on: January 18, 2013, 09:54:03 pm »
Well, it's not finished. Still gotta find the most common words - but that will be easy. My problem is that I got 140 unique words - my peers say it's supposed to be 139. Not having an at-home compiler is really annoying. Could you test/debug it?
How do I create a new ArrayList with the same contents as another? Not ArrayList <String> SortList = WordList;
That doesn't create a new array, just a new pointer. I couldn't figure out the syntax for it.
I had to work on another problem today, which is why I had so little time to test, but thankfully today's problem was easy - it was just to find all primes and emirps up to an input. I just used a loop and to limit/6 using 6k-1/6k+1 and tested with a loop to the sqrt.
I've got all weekend plus ML day to finally get these and then I'm back on track :)
« Last Edit: January 18, 2013, 09:55:14 pm by dinosteven »