Switch to unified view

a b/scripts/ctmetamaptest.java
1
import java.util.*;
2
import java.io.*;
3
import java.net.*;
4
import gov.nih.nlm.nls.skr.*;
5
public class ctmetamaptest {
6
  static GenericObject gj = new GenericObject();
7
  public static void main(String[] args) throws Exception {
8
    Scanner in = new Scanner(new File("example_ct.csv"));
9
    PrintWriter out = new PrintWriter(new File("sample_output_full.csv"));
10
    String useless = in.nextLine();
11
    while(in.hasNext()) {
12
      String[] terms = in.nextLine().split(",");
13
      String source = getURLSource(terms[2]);
14
      int index = source.indexOf("display_order");
15
      if (index > 0) {
16
        ArrayList<Integer> indices = new ArrayList<Integer>();
17
        ArrayList<Integer> startinds = new ArrayList<Integer>();
18
        ArrayList<Integer> endinds = new ArrayList<Integer>();
19
        while (index > 0) {
20
          indices.add(index);
21
          startinds.add(source.indexOf("description", index));
22
          endinds.add(source.indexOf("}", index));
23
          index = source.indexOf("display_order", index+1);
24
        }
25
        ArrayList<String> strings = new ArrayList<String>();
26
        for (int i = 0; i < startinds.size(); i++) strings.add(source.substring(startinds.get(i) + 14, endinds.get(i)-1));
27
        out.print(terms[0] + "," + terms[1] + ",\"[");
28
        for (int i = 0; i < strings.size(); i++) {
29
          PrintWriter tmp = new PrintWriter(new File("input.txt"));
30
          tmp.print(strings.get(i));
31
          tmp.close();
32
          out.print("\'" + getMetamap() + "\'");
33
          if (i < strings.size() - 1) out.print(",");
34
        }
35
        out.println();
36
      }
37
    }
38
    in.close();
39
    out.close();
40
  }
41
  static String getMetamap() {
42
    gj.setField("Email_Address", "neilmalur@hotmail.com");
43
    gj.setFileField("UpLoad_File", "./input.txt");
44
    //gj.setField("APIText", input);
45
    //gj.setField("Batch_Command", "MTI -opt1L_DCMS -E");
46
    gj.setField("Batch_Command", "metamap -pcI");
47
    gj.setField("BatchNotes", "SKR Web API test");
48
    gj.setField("SilentEmail", true);
49
    return gj.handleSubmission();
50
  }
51
  public static String getURLSource(String url) throws IOException
52
    {
53
        URL urlObject = new URL(url);
54
        URLConnection urlConnection = urlObject.openConnection();
55
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
56
        try {
57
        return toString(urlConnection.getInputStream());}catch(Exception e) {return "";}
58
    }
59
  private static String toString(InputStream inputStream) throws IOException
60
    {
61
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")))
62
        {
63
            String inputLine;
64
            StringBuilder stringBuilder = new StringBuilder();
65
            while ((inputLine = bufferedReader.readLine()) != null)
66
            {
67
                stringBuilder.append(inputLine);
68
            }
69
70
            return stringBuilder.toString();
71
        }
72
    }
73
}