2017-10-15 12 views
-1

J'ai un énorme fichier texte. Je voudrais supprimer tous les retours à la ligne et je souhaite que les sauts de paragraphes soient également supprimés et ajoutés au paragraphe précédent. Comment dois-je le faire en utilisant Java? J'ai utilisé replaceALL() en java mais je suis coincé avec l'ajout du paragraphe au précédent.Comment supprimer tous les sauts de ligne et sauts de paragraphe en utilisant java pour un fichier texte donné?

Please view this image for the file screenshot

public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException{ 
      StringBuilder sb = new StringBuilder(); 
      System.out.println(value.toString().replaceAll("[\\t\\n]+", "")); 
      StringTokenizer itr = new StringTokenizer(value.toString().replaceAll("[\\t\\n]+", ""));   
      String[] tokens = new String[itr.countTokens()*2]; 

      for(int l = 0 ; l<tokens.length;l++){ 
       if(itr.hasMoreTokens()){ 
        tokens[l] = itr.nextToken(); 

       } 
      } 
        for(int i = 0; i < tokens.length; i++){ 
        if(tokens[i] != null && tokens[i] != " "){ 
         sb.append(tokens[i]); 
          for(int j = i+1;j<i+5;j++){ 
           if(tokens[j] != null) 
           { 
           sb.append(" "); 
           sb.append(tokens[j]); 
           } 

          } 
        } 
         word.set(sb.toString()); 
         context.write(word, one); 
         //System.out.println(sb.toString()); 
         sb.setLength(0); 

        } 
     } 

Entrée:

The Project Gutenberg EBook of The Complete Works of William Shakespeare, by 
William Shakespeare 
sn 
This eBook is for the use of anyone anywhere at no cost and with 
almost no restrictions whatsoever. You may copy it, give it away or 
re-use it under the terms of the Project Gutenberg License included 
with this eBook or online at www.gutenberg.org 

** This is a COPYRIGHTED Project Gutenberg eBook, Details Below ** 
**  Please follow the copyright guidelines in this file.  ** 

Title: The Complete Works of William Shakespeare 

Author: William Shakespeare 

Posting Date: September 1, 2011 [EBook #100] 
Release Date: January, 1994 

Language: English 


*** START OF THIS PROJECT GUTENBERG EBOOK COMPLETE WORKS--WILLIAM SHAKESPEARE *** 




Produced by World Library, Inc., from their Library of the Future 

This is the 100th Etext file presented by Project Gutenberg, and 
is presented in cooperation with World Library, Inc., from their 
Library of the Future and Shakespeare CDROMS. Project Gutenberg 
often releases Etexts that are NOT placed in the Public Domain!! 

Shakespeare 

*This Etext has certain copyright implications you should read!* 

Résultats escomptés:

The Project Gutenberg EBook of The Complete Works of William Shakespeare, by 
William Shakespeare sn This eBook is for the use of anyone anywhere at no cost and with almost no restrictions whatsoever. You may copy it, give it away or re-use it under the terms of the Project Gutenberg License included 
with this eBook or online at www.gutenberg.org ** This is a COPYRIGHTED Project Gutenberg eBook, Details Below Please follow the copyright guidelines in this file.Title: The Complete Works of William Shakespeare Author: William Shakespeare Posting Date: September 1, 2011 [EBook #100] 
Release Date: January, 1994 Language: English START OF THIS PROJECT GUTENBERG EBOOK COMPLETE WORKS--WILLIAM SHAKESPEARE Produced by World Library, Inc., from their Library of the Future This is the 100th Etext file presented by Project Gutenberg, and is presented in cooperation with World Library, Inc., from their Library of the Future and Shakespeare CDROMS. Project Gutenberg often releases Etexts that are NOT placed in the Public Domain!! Shakespeare *This Etext has certain copyright implications you should read!* 
+2

post exemple d'entrée et de sortie prévu. N'envoyez pas non plus de texte/code comme image/lien ([plus d'infos] (https://meta.stackoverflow.com/a/285557)). Utilisez l'option [Modifier] pour corriger votre message. – Pshemo

+0

@Pshemo J'ai besoin de tous les retours à la ligne, de la ponctuation à supprimer et du paragraphe à ajouter aux paragraphes précédents. c'est un tout comme un seul paragraphe –

+0

Ce n'est toujours pas très clair. Vous réclamez "tous les sauts de ligne" mais cela voudrait dire que nous aurions une seule ligne, ce qui n'est pas le cas ici puisque votre sortie attendue a quatre lignes. Comment avez-vous reconnu quels séparateurs de ligne devaient rester? Vous avez également écrit que tous les signes de ponctuation doivent être supprimés, mais nous pouvons voir ',' avant '' par [linebreak] William Shakespeare', sans parler de 'Release Date: January, 1994'. – Pshemo

Répondre

0

Si vous voulez seulement les mots, vous pouvez rechercher des mots avec \ w et les concaténer.

public static void main(String args[]) { 
    final String input = "hello, how are you today how was school today, what did you have for food? this star needs to be removed ****"; 
    final String regex = "\\w+"; 
    final Matcher m = Pattern.compile(regex).matcher(input); 

    String output = ""; 
    while (m.find()) { 
     output += m.group(0)+" "; 
    } 
    System.out.println(output); 
} 

Résultat:

hello how are you today how was school today what did you have for food this star needs to be removed 
0

Utiliser la chaîne échappe littérales, pour l'onglet réel, saut de ligne. Et ne pas oublier le retour chariot (sur Windows).

String text = value.toString() 
    .replaceAll("(\r?\n){2}", "§") // Two line breaks will become a real line break. 
    .replaceAll("[\t\r\n]+", " ") // White space will become a real space. 
    .replace("§", "\n"); // The real line breaks. 

Au lieu de § on peut utiliser un caractère ésotérique uFEFF.

Devient

Good Morning, 

How are you? 
I am fine. 

Dans

Good Morning, 
How are you? I am fine.