Analyser le fichier journal Whatsapp en Java
Je travaille actuellement sur un petit outil qui analyse l'utilisation d'un chat de groupe dans Whatsapp.
J'essaie de le réaliser avec le fichier journal WhatsApp. Je l'ai réussi à formater le .txt
brut au format suivant pour travailler avec le texte 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
Jusqu'à présent, si bon. Le problème est qu'il y a quelques lignes de disquette comme:
29. Jan. 08:42 - Random Name2: message text 1
additional text of the message 1
29. Jan. 08:43 - Random Name2: message text 2
, Ou pire encore:
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
Je suppose que j'ai besoin d'une sorte d'algorithme pour résoudre ce problème, mais je suis assez nouveau dans la programmation et impossible de créer un algorithme aussi complexe.
Le même problème en Python: analyser un journal de conversation WhatsApp
[MODIFIER]
C'est mon code qui ne fonctionne pas. (Je sais que c'est assez mauvais)
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();
}
}
}
[MODIFIER 2]
À la fin, je veux lire le fichier et d'analyser chaque ligne:
29. Jan. 12:01 - Random Name: message text
Je peux dire quand il a été envoyé, qui l'a envoyé et quelle il a écrit
Si j'obtiens maintenant la ligne suivante:
additional text of the message 1
Je ne peux pas dire quand c'était écrit ni qui l'a envoyé
2 answers
Eh bien, j'ai trouvé une solution à votre problème, je crois, selon ce que j'ai compris.
Étant donné un fichier avec ce 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
(Il s'agit d'un fichier appelé "wsp.journal" dans mon dossier "data". Donc, le chemin d'accès est "données/wsp.journal")
Je m'attends à quelque chose comme ça:
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
Selon cela, j'ai implémenté la classe suivante:
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());
}
}
Que je vais invoquer de cette façon:
public static void main(String[] args) throws IOException {
new LogReader().processWspLogFile();
}
J'Espère que cela peut vous donner une idée ou peut être utile pour votre but. Je sais que certaines améliorations sont nécessaires, refactor est toujours nécessaire pour le code:), mais maintenant il peut atteindre le format attendu. Amusez-vous bien :).
Essayez ce code. Et voir si elle n'quoi de prévu.
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();
}
}