-
Output sentences using keywords in java
Hello,
I am trying to write a program in Java that tags sentences from a text file. Each tag has an associated array of keywords. A tag is applied to a sentence if and only if the sentence contains one or more keywords belonging to the tag's array My aim is to output the sentences in a categorical manner, e.g. if a certain keyword is an a category, and that keyword is in the sentence, then that sentence should be able to go to into that category. This is just the basic of what I have done:
For e.g. I have two tags: faults, and adaption. In those tags' arrays, I have words such as Bugs, Fail etc, for faults, and Polish, Clean up for adaption. One of the sentence will be 'BUG found and fixed', which would go into the faults category, hence it will input the sentence after the heading fault as it contains the word 'bug'.
Code :
String[] fault = {
"Misspelled",
"Error",
"Fixed",
"Change",};
And
Code :
String[] adaption = {"Fixed comments",
"Filters", "Polish","Adjust"};
This is the code that I have used to read the text file, and split the words:
Code :
{
try{
FileInputStream fstream = new FileInputStream("readthis.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String newread;
while ((newread = br.readLine()) != null) {
System.out.println( java.util.Arrays.toString( newread.split(" ")));
}
in.close();
}catch
(Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Right now the output that I am getting is:
Quote:
['This, bugs, needs, fixing']
['Cleaned, up, section, one']
['Fixed, typos']
['adjusted, the, code, to, make, it, look, simpler']
as this were the words in my text file, without the split.
My next step is to use the arrays that I have got, and use the sentences above to put them in their category, either in Faults, or Adaption, vie the keywords. However, I have looked at HashMaps, but I don't know how to use the arrays with it.
Any help or suggestions is useful for me.
Thank you :)
-
Re: Output sentences using keywords in java
I'm not totally sure what your question is. What have you tried?
-
Re: Output sentences using keywords in java
At this moment, I have the arrays list, e.g. the keywords that goes with their category. And I have read the text file that I need, and split the words up. However, I'm trying to read the file, and output the sentences into an category, looking at the words that they have, from the array list, and output those sentence in the category. e.g. sentence: 'Fixed typos' would go into the faults category, because of the keyword 'fixed' in the faults array. So the output will be something like Faults: Fixed Typos. That is the part I'm stuck at, putting the sentence in the category.
-
Re: Output sentences using keywords in java
What about it is confusing you?
I suggest writing out what you have to do, in plain English. Pretend you have a really dumb friend, and your job is to show that friend how to categorize a bunch of index cards that have these sentences written on them. Write out a list of steps your friend can follow to accomplish that goal.
When you have that written out, you'll have an algorithm that you can then begin translating into code.
-
Re: Output sentences using keywords in java
I have all the steps, and someone suggested that I should use Hashmap to put the the sentences into their categories using ArrayList. I don't know how to actually do that. So I was asking for any suggestion that would help. I think my question is bit confusing.
-
Re: Output sentences using keywords in java
A Map sounds like a useful tool. The key to the Map would be the category and the value associated with that key could be an ArrayList of all the sentences that matched.
-
Re: Output sentences using keywords in java
The Map tutorial contains code for breaking up an array of words into the frequency of each. Might be useful: The Map Interface (The Java™ Tutorials > Collections > Interfaces)
-
Re: Output sentences using keywords in java
Can I use HashTable as well?
-
Re: Output sentences using keywords in java
Quote:
Originally Posted by
GameGirl91
Can I use HashTable as well?
What happened when you tried? ;)
Notice that Hashtable *is a* Map.
-
Re: Output sentences using keywords in java
Quote:
Originally Posted by
KevinWorkman
What happened when you tried? ;)
Notice that Hashtable *is a* Map.
Ah, didn't know that. Do you know how I can get started? Because I don't really have clue. There are tutorials out there that helps you with just using hashmap by itself, but not with the arrays.
-
Re: Output sentences using keywords in java
How do you want to use arrays with Maps?
What is in the arrays?
What in the Map?
-
Re: Output sentences using keywords in java
The array is going to be
Code :
String[] faults = {
"Misspelled",
"Error",
"Fixed",
"Change",}
And
Code :
String[] adaption = {"Fixed comments",
"Filters", "Polish","Adjust"};
For the map, It's going to look for words from a text file, which is like this:
Quote:
['This, bugs, needs, fixing']
['Cleaned, up, section, one']
['Fixed, typos']
['adjusted, the, code, to, make, it, look, simpler']
I would love some help on how to get started.
-
Re: Output sentences using keywords in java
Do some more research on how to use a Map. Your posting doesn't look like how a Map would be used.
How are the two arrays: faults and adaption going to be used?
-
Re: Output sentences using keywords in java
Quote:
Originally Posted by
Norm
Do some more research on how to use a Map. Your posting doesn't look like how a Map would be used.
How are the two arrays: faults and adaption going to be used?
Each of the two array's has keywords. The system will read those keywords from the text file and output those sentence's which will have the matching array keyword and print them, in the either one of the the two group, e.g. the sentence ' bug 1457454: new interface' will come in the faults, as the word 'bug' matches with the keyword in faults.
-
Re: Output sentences using keywords in java
If you have arrays of words to search for in lines read from a file, as each line is read from the file loop through the arrays and use the array elements in a String class's method to see if the line contains one of the Strings from the arrays.
-
Re: Output sentences using keywords in java
Quote:
Originally Posted by
Norm
If you have arrays of words to search for in lines read from a file, as each line is read from the file loop through the arrays and use the array elements in a String class's method to see if the line contains one of the Strings from the arrays.
Can I than print out the results, e.g. all the sentences and print it out in another text file? If I can get any help in how to start the loop through the array, I would be great full.
Thank you
-
Re: Output sentences using keywords in java
See the tutorial about arrays:
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
Or look at the many code samples here on the forum.
-
Re: Output sentences using keywords in java
Thank you, I'll do that.
I just wanted to know to make an For loop, I need a stop condition for the loop stop, and since I don't know how many sentences are there in the text file, how would I stop it? Really confused on this.
-
Re: Output sentences using keywords in java
Quote:
how would I stop it
Use a while loop and exit the loop when there are no more lines to be read from the file.
The code in post#1 does that already.
-
Re: Output sentences using keywords in java
Is there there anyway in which I can read from the array?
-
Re: Output sentences using keywords in java
You can access elements of an array by using an index:
Code :
theArray[theIndex] //<<< access element in theArray at theIndex
Can you explain what you mean by "read from the array"?
-
Re: Output sentences using keywords in java
Because I already have two arrays I wondering how can I read the keywords from the array, and match them with the words in the sentences that I have in the text file.
-
Re: Output sentences using keywords in java
Quote:
how can I read the keywords from the array,
See my post#21 for how to get/read elements in an array.
Quote:
match them with the words in the sentences
Use the elements from the array in String class methods.
-
Re: Output sentences using keywords in java
I have a standard array right now, but I guess in order to read the elements from I need to convert the array into an arrayList. I have used this to do it:
Code :
new ArrayList<String>(Arrays.asList(faults));
ArrayList newarray = new ArrayList(100);
newarray.addAll(Arrays.asList(faults));
I was wondering if this was right?
-
Re: Output sentences using keywords in java
Quote:
if this was right?
What happens to the object returned by the first line?
Compile it and execute it to see. That's what I'd have to do.