Search in sources :

Example 61 with BufferedReader

use of java.io.BufferedReader in project CoreNLP by stanfordnlp.

the class Dictionaries method loadCorefDict.

private static void loadCorefDict(String[] file, ArrayList<Counter<Pair<String, String>>> dict) {
    for (int i = 0; i < 4; i++) {
        dict.add(new ClassicCounter<>());
        BufferedReader reader = null;
        try {
            reader = IOUtils.readerFromString(file[i]);
            // Skip the first line (header)
            reader.readLine();
            while (reader.ready()) {
                String[] split = reader.readLine().split("\t");
                dict.get(i).setCount(new Pair<>(split[0], split[1]), Double.parseDouble(split[2]));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeIgnoringExceptions(reader);
        }
    }
}
Also used : BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException)

Example 62 with BufferedReader

use of java.io.BufferedReader in project CoreNLP by stanfordnlp.

the class Dictionaries method loadCorefDictPMI.

private static void loadCorefDictPMI(String file, Counter<Pair<String, String>> dict) {
    BufferedReader reader = null;
    try {
        reader = IOUtils.readerFromString(file);
        // Skip the first line (header)
        reader.readLine();
        while (reader.ready()) {
            String[] split = reader.readLine().split("\t");
            dict.setCount(new Pair<>(split[0], split[1]), Double.parseDouble(split[3]));
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeIgnoringExceptions(reader);
    }
}
Also used : BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException)

Example 63 with BufferedReader

use of java.io.BufferedReader in project CoreNLP by stanfordnlp.

the class Dictionaries method loadCountriesLists.

private void loadCountriesLists(String file) {
    try {
        BufferedReader reader = IOUtils.readerFromString(file);
        for (String line; (line = reader.readLine()) != null; ) {
            countries.add(line.split("\t")[1].toLowerCase());
        }
        reader.close();
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    }
}
Also used : RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException)

Example 64 with BufferedReader

use of java.io.BufferedReader in project CoreNLP by stanfordnlp.

the class Dictionaries method loadDemonymLists.

/** The format of the demonyms file is
   *     countryCityOrState ( TAB demonym )*
   *  Lines starting with # are ignored
   *  The file is cased but stored in in-memory data structures uncased.
   *  The results are:
   *  demonyms is a hash from each country (etc.) to a set of demonymic Strings;
   *  adjectiveNation is a set of demonymic Strings;
   *  demonymSet has all country (etc.) names and all demonymic Strings.
   */
private void loadDemonymLists(String demonymFile) {
    BufferedReader reader = null;
    try {
        reader = IOUtils.readerFromString(demonymFile);
        for (String line; (line = reader.readLine()) != null; ) {
            line = line.toLowerCase(Locale.ENGLISH);
            String[] tokens = line.split("\t");
            if (tokens[0].startsWith("#"))
                continue;
            Set<String> set = Generics.newHashSet();
            for (String s : tokens) {
                set.add(s);
                demonymSet.add(s);
            }
            demonyms.put(tokens[0], set);
        }
        adjectiveNation.addAll(demonymSet);
        adjectiveNation.removeAll(demonyms.keySet());
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    } finally {
        IOUtils.closeIgnoringExceptions(reader);
    }
}
Also used : RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException)

Example 65 with BufferedReader

use of java.io.BufferedReader in project CoreNLP by stanfordnlp.

the class ConvertGenderFile method main.

public static void main(String[] args) throws IOException {
    String input = null;
    String output = null;
    for (int argIndex = 0; argIndex < args.length; ) {
        if (args[argIndex].equalsIgnoreCase("-input")) {
            input = args[argIndex + 1];
            argIndex += 2;
        } else if (args[argIndex].equalsIgnoreCase("-output")) {
            output = args[argIndex + 1];
            argIndex += 2;
        } else {
            throw new IllegalArgumentException("Unknown argument " + args[argIndex]);
        }
    }
    if (input == null) {
        throw new IllegalArgumentException("Must specify input with -input");
    }
    if (output == null) {
        throw new IllegalArgumentException("Must specify output with -output");
    }
    Map<List<String>, Gender> genderNumber = Generics.newHashMap();
    BufferedReader reader = IOUtils.readerFromString(input);
    for (String line; (line = reader.readLine()) != null; ) {
        String[] split = line.split("\t");
        String[] countStr = split[1].split(" ");
        int male = Integer.parseInt(countStr[0]);
        int female = Integer.parseInt(countStr[1]);
        int neutral = Integer.parseInt(countStr[2]);
        Gender gender = Gender.UNKNOWN;
        if (male * 0.5 > female + neutral && male > 2) {
            gender = Gender.MALE;
        } else if (female * 0.5 > male + neutral && female > 2) {
            gender = Gender.FEMALE;
        } else if (neutral * 0.5 > male + female && neutral > 2) {
            gender = Gender.NEUTRAL;
        }
        if (gender == Gender.UNKNOWN) {
            continue;
        }
        String[] words = split[0].split(" ");
        List<String> tokens = Arrays.asList(words);
        genderNumber.put(tokens, gender);
    }
    IOUtils.writeObjectToFile(genderNumber, output);
}
Also used : BufferedReader(java.io.BufferedReader) List(java.util.List) Gender(edu.stanford.nlp.dcoref.Dictionaries.Gender)

Aggregations

BufferedReader (java.io.BufferedReader)5548 InputStreamReader (java.io.InputStreamReader)3430 IOException (java.io.IOException)2601 FileReader (java.io.FileReader)1283 File (java.io.File)942 InputStream (java.io.InputStream)845 ArrayList (java.util.ArrayList)766 FileInputStream (java.io.FileInputStream)694 URL (java.net.URL)526 Test (org.junit.Test)447 FileNotFoundException (java.io.FileNotFoundException)380 StringReader (java.io.StringReader)340 BufferedWriter (java.io.BufferedWriter)242 HashMap (java.util.HashMap)232 HttpURLConnection (java.net.HttpURLConnection)231 Matcher (java.util.regex.Matcher)223 OutputStreamWriter (java.io.OutputStreamWriter)212 PrintWriter (java.io.PrintWriter)208 URLConnection (java.net.URLConnection)208 Reader (java.io.Reader)201