2012-12-16 2 views
1

내 제목이 모든 것을 잘 설명하지 못할 수도 있으므로 더 잘 설명하겠습니다. 웹에서 찾은 dat 파일의 동의어를 가져 오는 Chrome 확장 기능에 대해 연구 중입니다. MIT의 누군가로부터. 언어의 내 'Codecademy에서'지식,JavaScript 및 Google 크롬, 입력 스트림 및 배열

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.util.ArrayList; 

public class Grabber { 

/** 
* @param args 
*/ 
public static void main(String[] args) throws Exception { 

    URL mit = new URL(
      "http://mit.edu/~mkgray/jik/sipbsrc/src/thesaurus/old-thesaurus/lib/thesaurus.dat"); 
    BufferedReader in = new BufferedReader(new InputStreamReader(
      mit.openStream())); 

    String inputLine; 
    while ((inputLine = in.readLine()) != null) { 
     if (inputLine.startsWith("spoken")) { 
      ArrayList<String> list = new ArrayList<String>(); 
      String[] synonyms = inputLine.substring("spoken".length()) 
        .split(" "); 
      for (String toPrint : synonyms) { 
       if (toPrint.length() > 0) { 
        list.add(toPrint.trim()); 
       } 
      } 
      for (String toPrint : list) { 
       System.out.println(toPrint); 
      } 
     } 
    } 
    in.close(); 
    } 
} 

지금 : 나는 자바 (제 모국어)로 작성 내 생각의 대부분을받은 것, 여기가 내가 할 노력하고있어 볼 수 있도록이다 , 나는 모든 도서관 및 그와 같은 것들이 크롬의 자바 스크립트 API에 포함되어 있다는 것을 모른다. 이 작업을 완료하기 위해 조사를 시작해야합니까? 오, 나는 또한 자바 스크립트에서 배열을 만드는 방법을 알아야 할 필요가있다.

+0

코드 카데미는 조금 잡을 필요가 있습니다, 배열을 만드는 방법을 가르쳐 않았다. 몇 가지 힌트 : 1. 'XHR'파일을 grub; 2.'String.split()'; 3.'Object'를 사전으로 사용하십시오. – xiaoyi

답변

1

다음은 예입니다 :

var xhr = new XMLHttpRequest(); // Use XMLHttpRequest to fetch resources from the Web 
xhr.open("GET", // HTTP GET method 
    "http://mit.edu/~mkgray/jik/sipbsrc/src/thesaurus/old-thesaurus/lib/thesaurus.dat", 
    true // asynchronous 
); 
xhr.onreadystatechange = (function() 
{ 
    if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) // success 
    { 
     // data fetched in xhr.responseText, now parse it 
     var inputLines = xhr.responseText.split(/\r|\n|\r\n/); //Split them into lines 
     /* A quick and brief alternative to 
     while ((inputLine = in.readLine()) != null) { 
       if (inputLine.startsWith("spoken")) { 
       ... 
      } 
     } */ 
     inputLines.filter(function(inputLine) 
     { 
      // You can also use 
      // return inputLine.substr(0, 6) == "spoken"; 
      // if you are not familiar with regular expressions. 
      return inputLine.match(/^spoken/); 
     }).forEach(inputLine) 
     { 
      var list = []; 
      var synonyms = inputLine.substring("spoken".length).split(" "); 
      synonyms.fonEach(function(toPrint) 
      { 
       if(toPrint.length > 0) 
        list.push(toPrint.replace(/^\s+|\s+$/g, '')); 
        //toPrint.replace(/^\s+|\s+$/g, '') is similar to toPrint.trim() in Java 
        //list.push(...) is used to add a new element in the array list. 
      }); 
      list.forEach(function(toPrint) 
      { 
       // Where do you want to put your output? 
      }); 
     }); 
    } 
}); 
xhr.send(); // Send the request and fetch the data. 
관련 문제