I am having a problem. There is an input file [Input_sentences.txt file] consisting of sentences line by line. I pick up each sentence and generate sub-sentences of length 2 to 6 words. There are two more files [Combined_Man_phrase.txt and Combined_Eng_phrase.txt] which store the sub-sentence patterns aligned in parallel fashion. I want to write the corresponding pattern from the Combined_Eng_phrase.txt file for each sub-sentences match found in Combined_Man_phrase.txt file. If I find any sub-sentence match found from the input sentence starting with two words upto six words , then I remove that sub-sentence and go ahead with the remaining part of the input sentence for next subsentence match ( starting with 2 words upto 6 again ) . I have written the program in Java as given below but I am unable to get the matches for all sub-sentence [Assuming that all the entries of the sub-sentences are there]. Even if there are multiple match, I want to pick up the first match only and avoid writing multiple matched patterns to the output file. Any help is appreciated.
Code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class search_Match {
/** Creates a new instance of search_Match */
public search_Match() {
}
public void replace() throws FileNotFoundException, IOException {
//Create output file writter
FileWriter fr=new FileWriter("replaced_output.txt");
//Read Input sentences to be replaced
BufferedReader br=new BufferedReader(new FileReader("Input_sentences.txt"));
String sentence=null;
boolean found=false;
while((sentence=br.readLine())!=null){
String[] choppedUpString = sentence.trim().split(" ");
// Sub-sentence files
BufferedReader br4 = new BufferedReader(new FileReader("Combined_Ma_phrase.txt"));
BufferedReader br5 = new BufferedReader(new FileReader("Combined_En_phrase.txt"));
String ManPhrase=null;
String EngPhrase=null;
String tempString="";
int start=0;
int index=-1;
for (int i=0; i< choppedUpString.length ; i++)
{
for(int j=2; j<6; j++)
{
for (int k=start; k<(start+j) && k < choppedUpString.length ; k++)
tempString= tempString+" "+choppedUpString[k];
tempString = tempString.replaceFirst("\\s", "");
tempString= tempString.replaceAll("\\s+$","");
while((ManPhrase=br4.readLine())!=null)
{
EngPhrase=br5.readLine();
if(ManPhrase.matches(tempString))
{
fr.write(EngPhrase);
fr.write(" ");
start=0;
found=true;
index = sentence.indexOf(tempString);
if (index!=-1) sentence = sentence.substring(index+tempString.length(), sentence.length());
choppedUpString = sentence.trim().split(" ");
}
}
tempString="";
}
start++;
}
sentence=sentence.replaceFirst("\\s", "");
choppedUpString = sentence.split(" ");
fr.write("\r\n");
br4.close();
br5.close();
}
fr.close();
}
public static void main(String[] args) throws IOException{
search_Match m=new search_Match();
m.replace();
}
}
This program works for the first sub-subsentence. However, for the following sub-sentences of the remaining sentence after removing the first sub-sentence, it does not work. [ which is my main problem]
The sample files are attached. The first four words [i.e., This classic best seller ] of the Input_sentences.txt is matching with the fourth sub-sentences of Combined_Ma_phrase.txt, so the fourth sub-sentence of the corresponding order of Combined_En_phrase.txt is picked up and written to replaced_output.txt [i.e., thisthis classicclassic bestbest sellerseller ] . Again, from the remaining words of the input sentence, pick up sub-sentence [i.e., has been ] because the sub-sentence length is 2 to 6. So, the first match is picked up and the corresponding sub-sentence from Combined_En_phrase.txt [i.e., hashas beenbeen] is written to replaced_output.txt. Similarly, this process continues till the end of the sentence.
Last edited by TDX; October 8th, 2009 at 01:40 PM.
Reason: Indentation
Secondly please provide some examples showing several lines of each of the files you are reading in and show what you are expecting to match with what and what output you want.
Not really. (I'll never understand why indentation is so difficult for people).
Originally Posted by TDX
However, for the following sub-sentences of the remaining sentence after removing the first sub-sentence, it does not work. which is my main problem
I quickly scanned your code & found no evidence of debugging. I found no System.out.println("GOT HERE"), or System.out.println("variable x=" + x);. Put in effort into debugging your code. Debugging is not something that should wait until you've graduated college--you should start doing it almost after your first 'Hello World' program.
Even though people on programming forums like posting ...helpful...replies like 'your problem is here -> (points to line of code)', the sooner you learn how to debug yourself and find out for yourself that 'my problem is here -> (points to line of code)' the better.
If you've got the RAM, eclipse really helps. Never click on a class variable that can't be resolved though. Eclipse will freeze for a full minute. You have been warned.
If you've got the RAM, eclipse really helps. Never click on a class variable that can't be resolved though. Eclipse will freeze for a full minute. You have been warned.
IDE's with debuggers certainly make debugging code quicker but if the OP can't/doesn't know how to debug code using simple print statements I don't suppose he/she will be able to use a debugger.
I use Eclipse a lot and have have done for a few years now and I've never seen it freeze in the way you describe. Are you using the latest version and is your machine of a sufficient spec.?
TDX: I don't understand the logic within your replace loop it's not clear what you are trying to achieve and why are you reading the man and en files within the inner loop. Each time you start that loop again the streams will continue reading from where they left off and not from the begining of each file - is that really what you want? Why not read thefiles into ArrayLists (of better still a Map type collection class) outside of the loop and then the values will be available for each iteration of the loop.
As Martin O has already suggested you need to add some print statements within the loop so you can see what is actually happening and what values the key variables have.
IDE's with debuggers certainly make debugging code quicker but if the OP can't/doesn't know how to debug code using simple print statements I don't suppose he/she will be able to use a debugger.
I use Eclipse a lot and have have done for a few years now and I've never seen it freeze in the way you describe. Are you using the latest version and is your machine of a sufficient spec.?
I use Eclipse a lot and have have done for a few years now and I've never seen it freeze in the way you describe. Are you using the latest version and is your machine of a sufficient spec.?
Sorry, I actually didn't read this paragraph. Kinda hasty. Ok, I have a Thinkpad x61. WinKey+Pause/Break says I have 3 GB RAM and a Core 2 Duo at 2 GHz. Also, I'm running Galileo, pretty recent.
And although I can't currently reproduce the freeze, it has happened on many occasions.
Eclipse is an IDE for Java, written in Java. I expect no less... but since I use it, I obviously think it's better than the javac Main.java, java Main routine. Not even raw types get by eclipse. And I still recommend it for newbies.
Anyway, my post was irrelevant, since OP has logical errors in his code, not compile errors.
Since the sample files are attached, the logic may me more or less clear with reference to the first post. Also please provide some links on Indentation lesson. [Standardized one, if any]
Since the sample files are attached, the logic may me more or less clear with reference to the first post. Also please provide some links on Indentation lesson. [Standardized one, if any]
Programs must be written for people to read, and only incidentally for machines to execute...
H. Abelson and G. Sussman
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.