Hi everybody. I'm very new in programming languages and I have this project that from an input text file writes the frequency of the lemmas in the output file. I need to modify this and to make it work for an directory with text files as an input. I think I have to make a loop or something like that...but I don't know how :( what I've got so far is this:

public static void main(String[] args) {

		HashMap<String, Integer> hash = new HashMap();
		ArrayList<String> stopwords = new ArrayList<String>();
		try {

			FileReader in = new FileReader("text_lema_pos.txt");
			BufferedReader br = new BufferedReader(in);

			FileWriter fstream = new FileWriter("text_frecv.txt");
			BufferedWriter out = new BufferedWriter(fstream);

			FileReader stop = new FileReader("stopwords.inc");
			BufferedReader words = new BufferedReader(stop);

			String s = new String();

			while ((s = words.readLine()) != null) {

				stopwords.add(s);

			}

			while ((s = br.readLine()) != null) {
				String tokens[] = s.split(" ");

				for (int i = 0; i < tokens.length; i++) {

					String lema[] = tokens[i].split("\\|");
					int c = 0;
					for (int w = 0; w < stopwords.size() && c == 0; w++) {
						if (lema[1].equalsIgnoreCase(stopwords.get(w)))
							{c = 1;
					//		System.out.println("lema[1]: "+lema[1]+ " stopwords.get(w): "+stopwords.get(w));
							}
					}
					if (c == 0) {

						String word = lema[1] + " " + lema[2];

						if (hash.containsKey(word)) {
							// get number of occurrences for this word
							// increment it
							// and put back again
							hash.put(word, hash.get(word) + 1);
						} else {
							// this is first time we see this word, set value
							// '1'
							hash.put(word, 1);
						}
					}

				}
			}

			// First we're getting values array
			ArrayList<Integer> values = new ArrayList<Integer>();
			values.addAll(hash.values());
			// and sorting it (in reverse order)
			Collections.sort(values, Collections.reverseOrder());

			int last_i = -1;
			// Now, for each value
			for (Integer i : values) {

				if (last_i == i) // without dublicates
					continue;
				last_i = i;

				for (String str : hash.keySet()) {
					if (hash.get(str) == i) // which have this value
					{
						String s1[] = str.split(" ");
						//System.out.println("<word lemma=\"" + s1[0] + "\""
						//				+ " pos=\"" + s1[1] + "\" frecv=\"" + i
						//				+ "\">");
						out.write("<word lemma=\"" + s1[0] + "\"" + " pos=\""
								+ s1[1] + "\" frecv=\"" + i + "\">\n");
					}
				}
			}

			out.close();
			in.close();
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//System.out.println("gata");

	}

Ugh...
do not have time to rewrite your code but a wrote a loop that you need for all files in folder

import java.io.*; // class File belongs here
...
...
...
		File folder=new File("C:\\"); //Open folder
		String [] flist=folder.list(); //Declare array and fill it with list of files
			for (int i=0;i<flist.length;i++) //Now go throught file list and...
			{
				System.out.println(i + ": " + flist[i]); // ... print their names. Here in format "FileNo: FileName". Change it as you need
			}

now join this code in yours and change file name for reading in flist and that should be it.

If this solved your problem, please mark thread SOLVED, if not i'll be glad to answer any of your question abaout my solution

Edited 5 Years Ago by monarchmk: n/a

@monarchmk - Much better this way. Now you just need to break it down to "here's where you find the information you need" and you'll be doing great.

@Dee1004 - you'll find the information you need in the documentation for the File class and in the file i/o tutorial on the Sun web site.
This will give you the background to understand why it is that monarchmk's snippet iterates over the files in a directory.

@jon.kiparsky As i stated in previous post... if you say that before or sent an pm it would not be a problem

@mKorbel thanks for pointing out... FileReader accepts "File" types..
Here is a loop with correct properties...

File folder=new File("C:\\"); //Open folder
	String[] flist=folder.list(); //Declare array and fill it with list of files
		for (int i=0;i<flist.length;i++) //Now go throught file list and...
		{
			System.out.println(i + ": " + flist[i]); // ... print their names. Here in format "FileNo: FileName". Change it as you need
			File f=new File("C:\\" + flist[i]); //!!!Be aware of folder path!!!
			if (!f.isDirectory())
			{
			FileReader ft=new FileReader(f);
			....
			....
			....
			}
		}

Ok, now line 9 should be place to continue with code...

Edited 5 Years Ago by monarchmk: n/a

not File is platform indepedent... :-), this example isn't good, but don't worry about that, if you knew answer anytime post that, be sure that you'll learn more than..., allways exist 2,3 or more correct way, but best is with performace for endUser, .... bla bla bla .... :-)

and there are few good men around, those who always say how they feel ... :-)

This article has been dead for over six months. Start a new discussion instead.