|
| 1 | +packagetest; |
| 2 | + |
| 3 | +importjava.io.BufferedInputStream; |
| 4 | +importjava.io.BufferedReader; |
| 5 | +importjava.io.DataOutputStream; |
| 6 | +importjava.io.File; |
| 7 | +importjava.io.FileOutputStream; |
| 8 | +importjava.io.FileReader; |
| 9 | +importjava.io.IOException; |
| 10 | +importjava.io.InputStream; |
| 11 | +importjava.io.InputStreamReader; |
| 12 | +importjava.io.OutputStreamWriter; |
| 13 | +importjava.net.HttpURLConnection; |
| 14 | +importjava.net.MalformedURLException; |
| 15 | +importjava.net.ProtocolException; |
| 16 | +importjava.net.URI; |
| 17 | +importjava.net.URL; |
| 18 | +importjava.net.URLConnection; |
| 19 | +importjava.net.URLEncoder; |
| 20 | +importjava.nio.charset.StandardCharsets; |
| 21 | +importjava.nio.file.Files; |
| 22 | +importjava.nio.file.Path; |
| 23 | +importjava.nio.file.Paths; |
| 24 | +importjava.util.HashMap; |
| 25 | +importjava.util.List; |
| 26 | +importjava.util.Map; |
| 27 | +importjava.util.Map.Entry; |
| 28 | + |
| 29 | +importorg.json.simple.JSONObject; |
| 30 | +importorg.json.simple.parser.JSONParser; |
| 31 | +importorg.json.simple.parser.ParseException; |
| 32 | + |
| 33 | +importjavajs.util.AjaxURLConnection; |
| 34 | +importjavajs.util.Rdr; |
| 35 | +importsun.misc.IOUtils; |
| 36 | +importswingjs.JSUtil; |
| 37 | +importtest.osp.OSPDocument; |
| 38 | + |
| 39 | +publicclassTest_GoogleextendsTest_ { |
| 40 | + |
| 41 | +@SuppressWarnings("unused") |
| 42 | +publicstaticvoidmain(String[]args) { |
| 43 | +testGoogle(); |
| 44 | +} |
| 45 | + |
| 46 | +privatestaticvoidtestGoogle() { |
| 47 | +intmssleep =5000; |
| 48 | +URLurl =Test_URL.class.getResource("words.txt"); |
| 49 | +byte[]ret; |
| 50 | +try { |
| 51 | +ret = (byte[])Rdr.getStreamAsBytes(newBufferedInputStream(url.openStream()),null); |
| 52 | +String[]words =newString(ret).split("\r\n"); |
| 53 | +intn =words.length; |
| 54 | +double[]freq =newdouble[n]; |
| 55 | +StringBuffersb =newStringBuffer(); |
| 56 | +for (inti =0;i <n;i++) { |
| 57 | +try { |
| 58 | +processWord(i,words,freq); |
| 59 | +sb.append(i).append('\t').append(words[i]).append('\t').append(freq[i]).append('\n'); |
| 60 | +}catch (IOExceptione) { |
| 61 | +System.out.println(e); |
| 62 | +// TODO Auto-generated catch block |
| 63 | +i--; |
| 64 | +try { |
| 65 | +Thread.sleep(mssleep); |
| 66 | +}catch (InterruptedExceptione1) { |
| 67 | +// TODO Auto-generated catch block |
| 68 | +e1.printStackTrace(); |
| 69 | +} |
| 70 | +} |
| 71 | +} |
| 72 | +FileOutputStreamfos =newFileOutputStream("c:/temp/words.out"); |
| 73 | +fos.write(sb.toString().getBytes()); |
| 74 | +fos.close(); |
| 75 | +}catch (IOExceptione2) { |
| 76 | +// TODO Auto-generated catch block |
| 77 | +e2.printStackTrace(); |
| 78 | +} |
| 79 | + |
| 80 | +// TODO Auto-generated method stub |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +privatestaticvoidprocessWord(inti,String[]words,double[]freq)throwsIOException { |
| 85 | +Stringword =words[i]; |
| 86 | +URLurl =newURL("https://books.google.com/ngrams/json?content=+"+word +"&year_start=1981&year_end=2020&corpus=en&smoothing=3");byte[]bytes = (byte[])Rdr.getStreamAsBytes(newBufferedInputStream(url.openStream()),null); |
| 87 | +Stringjson =newString(bytes); |
| 88 | +String[]values =json.split("\\[")[2].split("\\]")[0].split(","); |
| 89 | +doublev =0; |
| 90 | +for (intj =values.length; --j >=0;) { |
| 91 | +v +=Double.valueOf(values[j]); |
| 92 | +} |
| 93 | +System.out.println(i +"\t" +word +"\t" +v); |
| 94 | +freq[i] =v; |
| 95 | +} |
| 96 | + |
| 97 | +} |