1

所以我的标题可能无法很好地解释事情,所以我会更好地解释,我正在开发这个 chrome 扩展,它从我在 MIT 的某个人那里找到的 dat 文件中获取同义词。我的大部分想法都是用 Java(我的母语)编写的,在这里你可以看到我想要做什么:

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();
    }
}

现在,凭借我对语言的“Codecademy”知识,我不知道 Chrome 的 JavaScript API 中包含的所有库等。我们应该开始寻找完成这项任务吗?哦,我还需要弄清楚如何在 JavaScript 中制作数组,就像我上面写的集合一样。

4

1 回答 1

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. 
于 2012-12-17T07:30:37.703 回答