Advertisement
Ladies_Man

#TF_YA Lab2 (XML) Text parsing

Nov 8th, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1. //найти конструкцию вида:
  2. //<слово><знак_препинания><знак_табуляции>
  3. //предварительно разбив текст на предложения и отделив междометия: ([А!] [У!] [А?] [О!] [А. С. Пушкин])
  4.  
  5. import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
  6. import java.io.File;
  7. import java.io.FileOutputStream;
  8. import java.io.PrintStream;
  9. import java.util.Scanner;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13. public class check {
  14.  
  15.     public static void form_xml(String entire_text) {
  16.  
  17.         String xml_header = "<?xml version=\"1.1\" encoding=\"UTF-8\" ?>\n<lab2_Belyaev>\n";
  18.         String xml_footer = "</lab2_AB>";
  19.         int n = 0, found_in_string;
  20.  
  21.         Pattern p0 = Pattern.compile("(\\w{2,}[\\.|\\!|\\?])|(\\w[\\!|\\?])", Pattern.CASE_INSENSITIVE);
  22.         Matcher m0 = p0.matcher(entire_text);
  23.  
  24.         StringBuilder newstr = new StringBuilder(entire_text);
  25.         String preedited_str;
  26.  
  27.         //find all non-abbreviation words in the end of sentences and replace end-of-string charachters with '!'
  28.         while (m0.find()) {
  29.             String tmp = m0.group();
  30.  
  31.             //tmp = tmp.replaceAll("\\.|\\!|\\?", "!");
  32.             //System.out.println("Abbrev[" + m0.start() + "]:" + '"' + tmp + '"');
  33.  
  34.             newstr.setCharAt(m0.start() + tmp.length() - 1, '!');
  35.         }
  36.  
  37.         preedited_str = newstr.toString().replace("\n", "");
  38.  
  39.         String[] sentences = preedited_str.split("\\!");
  40.  
  41.         for (String entry : sentences) {
  42.             System.out.println("S:" + '"' + entry + '"');
  43.         }
  44.  
  45.  
  46.  
  47.         StringBuffer buffer = new StringBuffer();
  48.         buffer.append(xml_header);
  49.  
  50.  
  51.         StringBuffer tmp_entry = new StringBuffer();
  52.         StringBuffer tmp_word = new StringBuffer();
  53.         StringBuffer tmp_sym = new StringBuffer();
  54.         StringBuffer tmp_tab = new StringBuffer();
  55.  
  56.         for (String entry : sentences) {
  57.  
  58.             String regex = "(\\w{1,}\\W\\t+)";
  59.             String inner_regex;
  60.             Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
  61.             Matcher m = p.matcher(entry);
  62.  
  63.             found_in_string = 0;
  64.  
  65.             while (m.find()) {
  66.                 tmp_entry.append(m.group());
  67.  
  68.                 if (found_in_string == 0) buffer.append("\t<sentence>" + entry + "</sentence>\n");
  69.  
  70.                 inner_regex = "\\w+";
  71.                 Pattern p_inner = Pattern.compile(inner_regex, Pattern.CASE_INSENSITIVE);
  72.                 Matcher m_inner = p_inner.matcher(tmp_entry);
  73.                 while (m_inner.find()) {
  74.                     tmp_word.append(m_inner.group());
  75.                 }
  76.  
  77.                 inner_regex = "[^\\w^\\s]";
  78.                 p_inner = Pattern.compile(inner_regex, Pattern.CASE_INSENSITIVE);
  79.                 m_inner = p_inner.matcher(tmp_entry);
  80.                 while (m_inner.find()) {
  81.                     tmp_sym.append(m_inner.group());
  82.                 }
  83.  
  84.                 inner_regex = "\\t+";
  85.                 p_inner = Pattern.compile(inner_regex, Pattern.CASE_INSENSITIVE);
  86.                 m_inner = p_inner.matcher(tmp_entry);
  87.                 while (m_inner.find()) {
  88.                     tmp_tab.append(m_inner.group());
  89.                 }
  90.  
  91.                 //System.out.println("X:<w>" + tmp_word + "</w> <c>" + tmp_sym + "</c> <tab>" + tmp_tab + "</tab>");
  92.                 buffer.append("\t\t<w>").append(tmp_word).append("</w>\n").append("\t\t  <c>").append(tmp_sym).append("</c>\n").append("\t\t  <tab>").append(tmp_tab).append("</tab>\n\n");
  93.  
  94.                 tmp_entry.delete(0, tmp_entry.length());
  95.                 tmp_word.delete(0, tmp_word.length());
  96.                 tmp_sym.delete(0, tmp_sym.length());
  97.                 tmp_tab.delete(0, tmp_tab.length());
  98.  
  99.                 n++;
  100.                 found_in_string++;
  101.             }
  102.         }
  103.  
  104.         buffer.append(xml_footer);
  105.  
  106.  
  107.         System.out.println(buffer);
  108.         System.out.print("Matches:" + n);
  109.  
  110.  
  111.  
  112.         try (PrintStream out =
  113.                      new PrintStream(new FileOutputStream("out.xml"))) {
  114.             out.print(buffer);
  115.         }
  116.         catch (java.io.FileNotFoundException e) {
  117.             System.err.println(e.toString());
  118.         }
  119.  
  120.     }
  121.  
  122.     public static void main(String[] args){
  123.  
  124.         try {
  125.             String input_text = new Scanner(new File("src/in.txt")).useDelimiter("\\A").next();
  126.             form_xml(input_text);
  127.         }
  128.         catch (java.io.FileNotFoundException e) {
  129.             System.out.println("E1: file not found");
  130.             System.err.println(e.toString());
  131.         }
  132.  
  133.     }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement