Advertisement
PetrovIgor

Яндекс.Склонятор.Тест

Nov 21st, 2011
1,987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.39 KB | None | 0 0
  1. //File Inflector.java
  2. package od.igor.petrov.lang.util;
  3.  
  4. import java.io.BufferedInputStream;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.net.MalformedURLException;
  10. import java.net.URL;
  11.  
  12. public class Inflector {
  13.  
  14.     public Inflector(String name) {
  15.         try {
  16.             URL url = new URL(inputUrl + name);
  17.             in = url.openStream();
  18.             in = new BufferedInputStream(in);
  19.  
  20.             tempFile = new File(".\\" + name + ".xml");
  21.             fout = new FileOutputStream(tempFile);
  22.             int symbol = 0;
  23.             while ((symbol = in.read()) != -1) {
  24.                 fout.write(symbol);
  25.             }
  26.             InflectParser pars = new InflectParser(tempFile);
  27.             String[] str = pars.getInflections();
  28.             for (int i = 0; i < str.length; i++) {
  29.                 System.out.println(str[i]);
  30.             }
  31.         } catch (MalformedURLException e) {
  32.             e.printStackTrace();
  33.         } catch (IOException e) {
  34.             e.printStackTrace();
  35.         } finally {
  36.             if (tempFile.exists()) {
  37.                 tempFile.delete();
  38.             }
  39.         }
  40.     }
  41.  
  42.     private static final String inputUrl = "http://export.yandex.ru/inflect.xml?name=";
  43.     private InputStream in = null;
  44.     private File tempFile = null;
  45.     private FileOutputStream fout = null;
  46. }
  47. //File InflectParser.java
  48. package od.igor.petrov.lang.util;
  49.  
  50. import java.io.File;
  51. import java.io.IOException;
  52.  
  53. import javax.xml.parsers.DocumentBuilder;
  54. import javax.xml.parsers.DocumentBuilderFactory;
  55. import javax.xml.parsers.ParserConfigurationException;
  56.  
  57. import org.w3c.dom.Document;
  58. import org.w3c.dom.Element;
  59. import org.w3c.dom.Node;
  60. import org.w3c.dom.NodeList;
  61. import org.xml.sax.SAXException;
  62.  
  63. public class InflectParser {
  64.  
  65.     public InflectParser(File xmlFile) {
  66.         try {
  67.  
  68.             factory = DocumentBuilderFactory.newInstance();
  69.             factory.setIgnoringComments(true);
  70.             factory.setCoalescing(true);
  71.             factory.setNamespaceAware(false);
  72.             factory.setValidating(false);
  73.  
  74.             parser = factory.newDocumentBuilder();
  75.  
  76.             document = parser.parse(xmlFile);
  77.  
  78.             sections = document.getElementsByTagName("inflection");
  79.             int numberOfSections = sections.getLength();
  80.             for (int i = 0; i < numberOfSections; i++) {
  81.                 section = (Element) sections.item(i);
  82.                 node = section.getFirstChild();
  83.                 inflections[i] = node.getNodeValue();
  84.             }
  85.         } catch (ParserConfigurationException e) {
  86.             e.printStackTrace();
  87.         } catch (SAXException e) {
  88.             e.printStackTrace();
  89.         } catch (IOException e) {
  90.             e.printStackTrace();
  91.         }
  92.     }
  93.  
  94.     public String[] getInflections() {
  95.         return inflections;
  96.     }
  97.  
  98.     private DocumentBuilderFactory factory = null;
  99.     private DocumentBuilder parser = null;
  100.     private Document document = null;
  101.     private NodeList sections = null;
  102.     private Element section = null;
  103.     private Node node = null;
  104.     private String[] inflections = new String[6];
  105. }
  106.  
  107. //File Starter.java
  108. package od.igor.petrov.lang.util;
  109.  
  110. public class Starter implements Runnable{
  111.  
  112.     public Starter(String[] input){
  113.         this.input = input;
  114.     }
  115.    
  116.     @Override
  117.     public void run() {
  118.         for(int i = 0; i < input.length; i++){
  119.             new Inflector(input[i]);
  120.             try {
  121.                 Thread.sleep(500);
  122.             } catch (InterruptedException e) {
  123.                 e.printStackTrace();
  124.             }
  125.         }
  126.     }
  127.  
  128.     private String[] input = null;
  129.    
  130. }
  131.  
  132. //File MainTest.java
  133. package od.igor.petrov.lang.util;
  134. public class MainTest {
  135.     public static void main(String[] args) {
  136.         new Thread(new Starter(new String[]{"утюг", "хрен", "поршень"})).start();
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement