Results 1 to 12 of 12
Thread: Help removing pronouns from text
- 11-11-2010, 04:16 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 29
- Rep Power
- 0
Help removing pronouns from text
hi,
im trying to remove the pronouns for a string. i have a list of pronouns in a text file. i get those into a tring array and then check my string against the elements in the array..here is my code.
im getting a null pointer exception in the line,Java Code:String a; BufferedReader pronounlist = new BufferedReader(new FileReader("E:/pronounslist.txt")); a = new String[100]; String pronoun = pronounlist.readLine(); int k = 0; while (pronoun != null) { a[k] = pronoun; pronoun = pronounlist.readLine(); System.out.println(a[k]); k++; } for(int iFilt = 0; iFilt<a.length; iFilt++) { int nWordLength = a[iFilt].length(); int nFoundPos = new1.indexOf(a[iFilt]); while (nFoundPos != -1){ new1 = new1.substring(0,nFoundPos)+""+new1.substring(nFoundPos+nWordLength); nFoundPos = new1.indexOf(a[iFilt]); } }
int nWordLength = a[iFilt].length();
can somebody help me with this?
jessieLast edited by Eranga; 11-12-2010 at 10:51 AM. Reason: code tags added
- 11-11-2010, 06:24 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
When you post code, put [code] at the start and [/code] at the end of the code so that it appears nicely formatted:
(I have done this so I can read it ;)Java Code:String a; BufferedReader pronounlist = new BufferedReader(new FileReader("E:/pronounslist.txt")); a = new String[100]; String pronoun = pronounlist.readLine(); int k = 0; while (pronoun != null) { a[k] = pronoun; pronoun = pronounlist.readLine(); System.out.println(a[k]); k++; } for(int iFilt = 0; iFilt<a.length; iFilt++) { int nWordLength = a[iFilt].length(); int nFoundPos = new1.indexOf(a[iFilt]); while (nFoundPos != -1){ new1 = new1.substring(0,nFoundPos)+""+new1.substring(nFou ndPos+nWordLength); nFoundPos = new1.indexOf(a[iFilt]); } }
- 11-11-2010, 06:37 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
im getting a null pointer exception in the line,
int nWordLength = a[iFilt].length();
"NullPointerException" means that a value was null when it should not have been. In this case the two candidates are the array a and the array element a[iFilt]. Neither of these are allowed to be null because you can't access an element if the array is null, and you can't call a method if something is null.
Since your code says quite straightforwardly
Java Code:a = new String[100];
we can rule out a being null. That leaves a[iFilt] being null. And you have to wonder why that would happen.
a is initialised as above and, like all Java arrays, it is never empty! Instead it has one hundred locations and each one of them has a null value. a.length is, and will always be, 100. So when you work along the for loop from index 0 to index 100 you will, almost certainly, hit one of these null values in the array. (the front part of the array was filled with nonnull pronouns, but you made the array "big enough" so there are bound to be nulls at the end of it).
Anyway you get to one of these nulls and then a[iFilt] has the value null and a[iFilt].length() causes the NPE because you can't call a method on a null thing.
------------------------
Instead of the "argument" to show a[iFilt] is null (rather than a), you can use System.out.println().
Java Code:or(int iFilt = 0; iFilt<a.length; iFilt++) { System.out.println("a=" + a); System.out.println("a[iFilt]=" + a[iFilt]); int nWordLength = a[iFilt].length(); // ...
Such code will tell you exactly what it is that is null.Last edited by pbrockway2; 11-11-2010 at 06:40 PM.
- 11-12-2010, 10:44 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 29
- Rep Power
- 0
hi,
thnks a lot. now i dont get the null pointer exception after i set the array size to the exact size.. but now, this code does not find the pronouns in the list and remove that from the string. why is that? i cant find anything wrong here. i tried the following code as well. but still i can see the pronouns in the string..
Java Code:for(int iFilt = 0; iFilt<a.length; iFilt++) { while ( new1.indexOf(a[iFilt]) != -1){ new1 = new1.replace(a[iFilt], ""); } }
- 11-12-2010, 10:52 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Did you check what you've in the array a. You keep all the stuff in an array and removing them. If it's not found in the array then you cannot see any changes to the original.
- 11-12-2010, 12:31 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 29
- Rep Power
- 0
the array contains the pronouns... i tested it..but still its not checking it in my string....
- 11-12-2010, 01:00 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 29
- Rep Power
- 0
Im iterating the list of words like this and then searching for pronouns in the list using my array this time...still it does not work....
Java Code:for(Iterator f = words1.iterator();f.hasNext();){ String anew = f.next().toString(); //System.out.println(anew); for(int iFilt = 0; iFilt<a.length; iFilt++) { if(anew.indexOf(a[iFilt]) != -1){ anew = anew.replaceAll(a[iFilt], ""); } } System.out.println(anew); }
- 11-12-2010, 09:35 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Again you can use System.out.println() to see what you are checking for:
Java Code:for(Iterator f = words1.iterator();f.hasNext();){ String anew = f.next().toString(); System.out.println("looking at " + anew); for(int iFilt = 0; iFilt<a.length; iFilt++) { System.out.println("checking for " + a[iFilt]); if(anew.indexOf(a[iFilt]) != -1){ System.out.println("found!") anew = anew.replaceAll(a[iFilt], ""); } } System.out.println(anew); }
- 11-14-2010, 01:09 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-15-2010, 10:50 AM #10
Member
- Join Date
- Nov 2010
- Posts
- 29
- Rep Power
- 0
i think there is a mistake in my searching code.. because the list words1 contains a list of words like this for example.
it
Belgium
's IMEC
a threeyear collaboration agreement
all
advanced metallization process
i want to remove pronouns like, it, all and so on from this list.. but my search code replaces all in the metallization word itself. how can i remove only the pronouns that are in the list as one word/element...
- 11-16-2010, 12:03 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
It's hard to know exactly what you're doing here.
Originally you had one string and were searching for pronouns within it. In that case you could use replaceAll() but will have to use regular expressions that corresponds to each of these words. (A word is not just a sequence of letters: it is a sequence within a certain context that marks it out as a word.)
The problem you pose now is quite different.
how can i remove only the pronouns that are in the list as one word/element
In that case you would take the list item and work through all the pronouns. If the item is equal to any of the pronouns, remove it from the list.
- 11-19-2010, 05:45 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Basically you want to remove the whole word (pronoun) if found, not the patterns of them. Isn't it?
Similar Threads
-
Removing Items In a GUI
By 67726e in forum New To JavaReplies: 3Last Post: 08-01-2010, 05:46 PM -
Removing Duplicates.
By dashwall in forum New To JavaReplies: 9Last Post: 12-29-2009, 01:03 PM -
removing reference
By ajith_raj in forum Advanced JavaReplies: 4Last Post: 02-12-2009, 11:46 AM -
image removing
By Triss in forum New To JavaReplies: 3Last Post: 01-20-2008, 08:27 PM -
Removing all the instances of a given text form a String
By Java Tip in forum Java TipReplies: 1Last Post: 01-11-2008, 10:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks