4

I´m currently working on a little tool which analyses the usage of a group-chat in Whatsapp.

i´m trying to realize it with the whatsapp logfile. I managed it to format the raw .txt to the following format to work with the formated text:

29. Jan. 12:01 - Random Name: message text
29. Jan. 12:22 - Random Name: message text
29. Jan. 12:24 - Random Name: message text
29. Jan. 12:38 - Random Name: message text
29. Jan. 12:52 - Random Name: message text

so far, so good. The Problem is that there are a few floppy lines like:

29. Jan. 08:42 - Random Name2: message text 1
                 additional text of the message 1
29. Jan. 08:43 - Random Name2: message text 2

or even worse:

15. Jan. 14:00 - Random Name: First part of the message
                 second part
                 third part
                 forth part
                 fifth part    
29. Jan. 08:43 - Random Name2: message text 2

I guess I need a kind of algorythm to solve this problem, but i´m pretty new in programming and can´t create such a complex algorithm.

The same problem in Python: parse a whatsApp conversation log

[EDIT]

This is my code which doesn´t work. (I know it´s pretty bad)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FormatList {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        FileReader fr = new FileReader("Whatsapp_formated.txt");
        BufferedReader br = new BufferedReader(fr);

        FileWriter fw = new FileWriter("Whatsapp_formated2.txt");
        BufferedWriter ausgabe = new BufferedWriter(fw);

        String line="";
        String buffer="";

        while((line = br.readLine())!=null)
        {
            System.out.println("\n"+line);

            if(line.isEmpty())
            {

            }
            else{
                if(line.charAt(0)=='0'||line.charAt(0)=='1'||line.charAt(0)=='2'||line.charAt(0)=='3'||line.charAt(0)=='4'||line.charAt(0)=='5'||line.charAt(0)=='6'||line.charAt(0)=='7'||line.charAt(0)=='8'||line.charAt(0)=='9')
                {
                    buffer = line;

                }
                else
                {
                    buffer += line;
                }

                 ausgabe.write(buffer);
                 ausgabe.newLine();
                System.out.println(buffer);
            }

            ausgabe.close();

        }




    }

}

[EDIT 2]

In the end i want to read out the file and analyse each line:

29. Jan. 12:01 - Random Name: message text

I can tell when it was sent, who sent it and what/how much he wrote

If i now get the following line:

additional text of the message 1

I neither can tell when it was written nor who sent it

Community
  • 1
  • 1
  • What is the final goal? So what do you want to do? What output do you expect and what did you already write in java to analyze the text? – Nitram Jun 26 '15 at 14:05
  • 1
    @Eric Pro You can send the log via Whatsapp default settings. – MegaCleptomaniac Jun 26 '15 at 14:06
  • @Nitram I want an '.txt' file where all lines are formated properly to read each line and analyse which person wrote how much text. I managed it to reach above listed form with a few "bad" parts like i described – MegaCleptomaniac Jun 26 '15 at 14:08
  • You checked on Escape-Chars (e.g. \n)? – Eric Jun 26 '15 at 14:10
  • 1
    You could open the output file with a hex editor and look for "0A" (and "0D 0A"). EDIT: for your code that means, before you are checking if the line is empty use line=line.replace("\r","").replace("\n",""); – Eric Jun 26 '15 at 14:17
  • 2
    Your input already looks formatted. If you give an idea on how you want your output to look like may be it will help – Johnson Abraham Jun 26 '15 at 14:26
  • @EricPro It worked fine for the empty spaces but the problem still is that there are lines without timestamps and names. could i solve this problem in any other way ? – MegaCleptomaniac Jun 26 '15 at 14:29
  • @JohnsonAbraham The output should look like the first piece of code given: every line in the formated '.txt' should start with the timestamp followed by the name and the message. No line should remain with just message text bus. I need to somehow either add the timestamp and name to the floppy lines or add the floppy lines to the complete line before. – MegaCleptomaniac Jun 26 '15 at 14:34

2 Answers2

2

well, I came up with a solution for your problem, I believe, according to what I understood.

Given a file with this format:

29. Jan. 12:01 - Random Name: message text
29. Jan. 12:22 - Random Name: message text
29. Jan. 12:24 - Random Name: message text
29. Jan. 12:38 - Random Name: message text
29. Jan. 12:52 - Random Name: message text
29. Jan. 08:42 - Random Name2: message text 1
                 additional text of the message 1
29. Jan. 08:43 - Random Name2: message text 2
15. Jan. 14:00 - Random Name: First part of the message
                 second part
                 third part
                 forth part
                 fifth part    
29. Jan. 08:43 - Random Name2: message text 2

(This is a file called "wsp.log" in my "data" folder. So the path to access to it is "data/wsp.log")

I expect something like this:

29. Jan. 12:01 - Random Name: message text
29. Jan. 12:22 - Random Name: message text
29. Jan. 12:24 - Random Name: message text
29. Jan. 12:38 - Random Name: message text
29. Jan. 12:52 - Random Name: message text
29. Jan. 08:42 - Random Name2: message text 1 additional text of the message 1
29. Jan. 08:43 - Random Name2: message text 2
15. Jan. 14:00 - Random Name: First part of the message second part third part forth part fifth part
29. Jan. 08:43 - Random Name2: message text 2

According to that, I implemented the following class:

public class LogReader {

    public void processWspLogFile() throws IOException {
        //a. I would reference to my file
        File wspLogFile = new File("data/wsp.log");
        //b. I would use the mechanism to read the file using BufferedReader
        BufferedReader bufferedReader = new BufferedReader(new FileReader(wspLogFile));

        String currLine = null;//This is the current line (like my cursor)

        //This will hold the data of the file in String format
        StringBuilder stringFormatter = new StringBuilder();
        boolean firstIterationDone = false;//The first line will always contains the format, so I will always append it, from the second I will start making the checkings...

        // Now I can use some regex (I'm not really good at this stuff, I just used a Web Page: http://txt2re.com/)
        /* This regex will match the lines that contains the date in this format "29. Jan. 12:22", when I take a look at your file
          I can see that the "additional text of the message" does not contains any date, so I can use that as my point of separation*/
        String regex = "(\\d)(\\d)(\\.)(\\s+)([a-z])([a-z])([a-z])(\\.)(\\s+)(\\d)(\\d)(:)(\\d)(\\d)";
        //As part of using regex, I would like to create a Pattern to make the lines on the list match this expression      
        Pattern wspLogDatePattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

        //Use of the line separator of the O.S
        String lineSeparator = System.getProperty("line.separator");

        while ((currLine = bufferedReader.readLine()) != null) {

            if (!firstIterationDone) {
                stringFormatter.append(currLine);
                firstIterationDone = true;
            } else {
                Matcher wspLogDateMatcher = wspLogDatePattern.matcher(currLine);    

                //The first time we will check if the second line has the pattern, if it does, we append a line separator
                if (wspLogDateMatcher.find()) {
                    //It is a "normal" line
                    stringFormatter.append(lineSeparator).append(currLine);             
                } else {
                    //But if it doesn't, we append it on the same line
                    stringFormatter.append(" ").append(currLine.trim());
                }
            }
        }
        System.out.println(stringFormatter.toString());
    }
}

Which I will invoke this way:

public static void main(String[] args) throws IOException {
    new LogReader().processWspLogFile();
}

Hope this can give you some idea or can be useful for your purposes. I know some improvements are needed, refactor is always needed for code :), but by now it can achieve the format expected. Happy coding :).

Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51
0

Try this code. And see if it does whats intended.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class WhatsappFormatted {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        char preString = '-';
        char searchString = ':';
        FileReader fr = new FileReader("Whatsapp_formated.txt");
        BufferedReader br = new BufferedReader(fr);

        FileWriter fw = new FileWriter("Whatsapp_formated2.txt");
        BufferedWriter ausgabe = new BufferedWriter(fw);

        String line = "";
        String buffer = "";
        String lastMember = null;
        while ((line = br.readLine()) != null) {
            System.out.println("\n" + line);

            if (!line.isEmpty())

                if (Character.isDigit(line.charAt(0)) && Character.isDigit(line.charAt(1))) {
                    lastMember = line.substring(0, line.indexOf(searchString, line.indexOf(preString)) + 1);
                    buffer = line.trim();
                } else {
                    buffer += "\n" + lastMember + line.trim();
                }

            ausgabe.write(buffer);
            ausgabe.newLine();
            System.out.println(buffer);
        }

        ausgabe.close();

    }

}
Johnson Abraham
  • 771
  • 4
  • 12
  • with this code the size of the new document is like 20 times bigger than my old file due to a String which gets bigger with every line in the unformated version. – MegaCleptomaniac Jun 29 '15 at 06:23
  • That will be the case with formatted and non-formatted output since in the formatted output we will be putting in our stuff, in this case we will be adding this '15. Jan. 14:00 - Random Name:' to each line. But is this code serving your purpose. – Johnson Abraham Jun 29 '15 at 07:18