Example usage for org.apache.commons.io FileUtils writeLines

List of usage examples for org.apache.commons.io FileUtils writeLines

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils writeLines.

Prototype

public static void writeLines(File file, Collection lines, String lineEnding) throws IOException 

Source Link

Document

Writes the toString() value of each item in a collection to the specified File line by line.

Usage

From source file:com.github.stagirs.lingvo.build.Xml2Plain.java

public static void main(String[] args) throws Exception {
    FileUtils.writeLines(new File("dict.opcorpora.plain"), "utf-8",
            getLemmas(new File("dict.opcorpora.xml.zip")));
    FileUtils.writeLines(new File("annot.opcorpora.no_ambig.plain"), "utf-8",
            getAnnotations(new File("annot.opcorpora.no_ambig.xml.zip")));
}

From source file:com.github.stagirs.lingvo.build.MorphStateMachineBuilder.java

public static void main(String[] args) throws IOException {
    FileUtils.writeLines(new File("src/main/resources/MorphStateMachine"), "utf-8", suf2mapping());
}

From source file:com.github.stagirs.lingvo.build.SyntaxStatisticsBuilder.java

public static void main(String[] args) throws Exception {
    TObjectIntHashMap<String> map = new TObjectIntHashMap<String>();
    for (String line : FileUtils.readLines(new File("annot.opcorpora.no_ambig.plain"), "utf-8")) {
        List<String> types = types(Annotation.parse(line));
        if (types.isEmpty()) {
            continue;
        }//from w ww . j ava 2s.c  o m
        map.adjustOrPutValue(types.get(0), 1, 1);
        map.adjustOrPutValue(" " + types.get(0), 1, 1);
        for (int i = 1; i < types.size(); i++) {
            map.adjustOrPutValue(types.get(i), 1, 1);
            map.adjustOrPutValue(types.get(i - 1) + " " + types.get(i), 1, 1);
        }
        map.adjustOrPutValue(types.get(types.size() - 1) + " ", 1, 1);
    }
    final List<String> result = new ArrayList<String>();
    map.forEachEntry(new TObjectIntProcedure<String>() {
        @Override
        public boolean execute(String key, int count) {
            result.add(key + "\t" + count);
            return true;
        }
    });
    FileUtils.writeLines(new File("src/main/resources/SyntaxStatistics"), "utf-8", result);
}

From source file:de.tudarmstadt.ukp.csniper.resbuild.stuff.FilterPipe.java

public static void main(String[] args) throws IOException {
    List<String> files = new ArrayList<String>();
    int i = 0;//  w  w w  .  j  av  a2 s  .c o  m
    for (File file : FileUtils.listFiles(new File(base), new String[] { "csv" }, true)) {
        String text = FileUtils.readFileToString(file, "UTF-8");
        files.add(StringUtils.substringBeforeLast(file.getName(), ".") + ".xml");
        if (StringUtils.containsAny(text, "")) {
            files.remove(StringUtils.substringBeforeLast(file.getName(), ".") + ".xml");
        }
        i++;
        if (i % 100 == 0) {
            System.out.println("ok:" + i);
        }
    }

    FileUtils.writeLines(new File("D:\\hadoop\\output\\BNC_new\\exclusions.txt"), "UTF-8", files);
}

From source file:edu.cuhk.hccl.evaluation.EvaluationApp.java

public static void main(String[] args) throws IOException, TasteException {
    File realFile = new File(args[0]);
    File estimateFile = new File(args[1]);

    // Build real-rating map
    Map<String, long[]> realMap = buildRatingMap(realFile);

    // Build estimate-rating map
    Map<String, long[]> estimateMap = buildRatingMap(estimateFile);

    // Compare realMap with estimateMap
    Map<Integer, List<Double>> realList = new HashMap<Integer, List<Double>>();
    Map<Integer, List<Double>> estimateList = new HashMap<Integer, List<Double>>();

    // Use set to store non-duplicate pairs only
    Set<String> noRatingList = new HashSet<String>();

    for (String pair : realMap.keySet()) {
        long[] realRatings = realMap.get(pair);
        long[] estimateRatings = estimateMap.get(pair);

        if (realRatings == null || estimateRatings == null)
            continue;

        for (int i = 0; i < realRatings.length; i++) {
            long real = realRatings[i];
            long estimate = estimateRatings[i];

            // continue if the aspect rating can not be estimated due to incomplete reviews
            if (estimate <= 0) {
                noRatingList.add(pair.replace("@", "\t"));
                continue;
            }//from  www .j a v a2 s  .  co  m

            if (real > 0 && estimate > 0) {
                if (!realList.containsKey(i))
                    realList.put(i, new ArrayList<Double>());

                realList.get(i).add((double) real);

                if (!estimateList.containsKey(i))
                    estimateList.put(i, new ArrayList<Double>());

                estimateList.get(i).add((double) estimate);
            }
        }
    }

    System.out.println("[INFO] RMSE, MAE for estimate ratings: ");
    System.out.println("------------------------------");
    System.out.println("Index \t RMSE \t MAE");
    for (int i = 1; i < 6; i++) {
        double rmse = Metric.computeRMSE(realList.get(i), estimateList.get(i));
        double mae = Metric.computeMAE(realList.get(i), estimateList.get(i));

        System.out.printf("%d \t %.3f \t %.3f \n", i, rmse, mae);
    }

    System.out.println("------------------------------");

    if (noRatingList.size() > 0) {
        String noRatingFileName = "evaluation-no-ratings.txt";
        FileUtils.writeLines(new File(noRatingFileName), noRatingList, false);

        System.out.println("[INFO] User-item pairs with no ratings are saved in file: " + noRatingFileName);
    } else {
        System.out.println("[INFO] All user-item pairs have ratings.");
    }
}

From source file:com.mmounirou.spotirss.SpotiRss.java

/**
 * @param args/*from w  w  w .j  ava2 s  .  c  o  m*/
 * @throws IOException 
 * @throws ClassNotFoundException 
 * @throws IllegalAccessException 
 * @throws InstantiationException 
 * @throws SpotifyClientException 
 * @throws ChartRssException 
 * @throws SpotifyException 
 */
public static void main(String[] args) throws IOException, InstantiationException, IllegalAccessException,
        ClassNotFoundException, SpotifyClientException {
    if (args.length == 0) {
        System.err.println("usage : java -jar spotiboard.jar <charts-folder>");
        return;
    }

    Properties connProperties = new Properties();
    InputStream inStream = SpotiRss.class.getResourceAsStream("/spotify-server.properties");
    try {
        connProperties.load(inStream);
    } finally {
        IOUtils.closeQuietly(inStream);
    }

    String host = connProperties.getProperty("host");
    int port = Integer.parseInt(connProperties.getProperty("port"));
    String user = connProperties.getProperty("user");

    final SpotifyClient spotifyClient = new SpotifyClient(host, port, user);
    final Map<String, Playlist> playlistsByTitle = getPlaylistsByTitle(spotifyClient);

    final File outputDir = new File(args[0]);
    outputDir.mkdirs();
    TrackCache cache = new TrackCache();
    try {

        for (String strProvider : PROVIDERS) {
            String providerClassName = EntryToTrackConverter.class.getPackage().getName() + "."
                    + StringUtils.capitalize(strProvider);
            final EntryToTrackConverter converter = (EntryToTrackConverter) SpotiRss.class.getClassLoader()
                    .loadClass(providerClassName).newInstance();
            Iterable<String> chartsRss = getCharts(strProvider);
            final File resultDir = new File(outputDir, strProvider);
            resultDir.mkdir();

            final SpotifyHrefQuery hrefQuery = new SpotifyHrefQuery(cache);
            Iterable<String> results = FluentIterable.from(chartsRss).transform(new Function<String, String>() {

                @Override
                @Nullable
                public String apply(@Nullable String chartRss) {

                    try {

                        long begin = System.currentTimeMillis();
                        ChartRss bilboardChartRss = ChartRss.getInstance(chartRss, converter);
                        Map<Track, String> trackHrefs = hrefQuery.getTrackHrefs(bilboardChartRss.getSongs());

                        String strTitle = bilboardChartRss.getTitle();
                        File resultFile = new File(resultDir, strTitle);
                        List<String> lines = Lists.newLinkedList(FluentIterable.from(trackHrefs.keySet())
                                .transform(Functions.toStringFunction()));
                        lines.addAll(trackHrefs.values());
                        FileUtils.writeLines(resultFile, Charsets.UTF_8.displayName(), lines);

                        Playlist playlist = playlistsByTitle.get(strTitle);
                        if (playlist != null) {
                            playlist.getTracks().clear();
                            playlist.getTracks().addAll(trackHrefs.values());
                            spotifyClient.patch(playlist);
                            LOGGER.info(String.format("%s chart exported patched", strTitle));
                        }

                        LOGGER.info(String.format("%s chart exported in %s in %d s", strTitle,
                                resultFile.getAbsolutePath(),
                                (int) TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - begin)));

                    } catch (Exception e) {
                        LOGGER.error(String.format("fail to export %s charts", chartRss), e);
                    }

                    return "";
                }
            });

            // consume iterables
            Iterables.size(results);

        }

    } finally {
        cache.close();
    }

}

From source file:edu.cuhk.hccl.cmd.AppSearchEngine.java

public static void main(String[] args) throws IOException {

    // Get parameters
    CommandLineParser parser = new BasicParser();
    Options options = createOptions();//from  ww  w.j a  va 2 s  . c o m

    File dataFolder = null;
    String queryStr = null;
    int topK = 0;
    File resultFile = null;
    String queryType = null;
    File similarityFile = null;

    try {
        CommandLine line = parser.parse(options, args);

        dataFolder = new File(line.getOptionValue('d'));
        queryStr = line.getOptionValue('q');
        queryType = line.getOptionValue('t');

        topK = Integer.parseInt(line.getOptionValue('k'));
        resultFile = new File(line.getOptionValue('f'));
        similarityFile = new File(line.getOptionValue('s'));

        if (line.hasOption('m')) {
            String modelPath = line.getOptionValue('m');

            if (queryType.equalsIgnoreCase("WordVector")) {
                expander = new WordVectorExpander(modelPath);
            } else if (queryType.equalsIgnoreCase("WordNet")) {
                expander = new WordNetExpander(modelPath);
            } else {
                System.out.println("Please choose a correct expander: WordNet or WordVector!");
                System.exit(-1);
            }
        }

    } catch (ParseException exp) {
        System.out.println("Error in parameters: \n" + exp.getMessage());
        System.exit(-1);
    }

    // Create Index
    StandardAnalyzer analyzer = new StandardAnalyzer();
    Directory index = createIndex(dataFolder, analyzer);

    // Build query
    Query query = buildQuery(analyzer, queryStr, queryType);

    // Search index for topK hits
    IndexReader reader = DirectoryReader.open(index);
    IndexSearcher searcher = new IndexSearcher(reader);
    TopScoreDocCollector collector = TopScoreDocCollector.create(topK, true);
    searcher.search(query, collector);
    ScoreDoc[] hits = collector.topDocs().scoreDocs;

    // Show search results
    System.out.println("\n[INFO] " + hits.length + " hits were returned:");
    List<String> hitLines = new ArrayList<String>();

    for (int i = 0; i < hits.length; i++) {
        int docId = hits[i].doc;
        Document d = searcher.doc(docId);

        String line = (i + 1) + "\t" + d.get(PATH_FIELD) + "\t" + hits[i].score;

        System.out.println(line);

        hitLines.add(line);
    }

    // Compute cosine similarity between documents
    List<String> simLines = new ArrayList<String>();
    for (int m = 0; m < hits.length; m++) {
        int doc1 = hits[m].doc;
        Terms terms1 = reader.getTermVector(doc1, CONTENT_FIELD);

        for (int n = m + 1; n < hits.length; n++) {
            int doc2 = hits[n].doc;
            Terms terms2 = reader.getTermVector(doc2, CONTENT_FIELD);

            CosineDocumentSimilarity cosine = new CosineDocumentSimilarity(terms1, terms2);
            double similarity = cosine.getCosineSimilarity();
            String line = searcher.doc(doc1).get(PATH_FIELD) + "\t" + searcher.doc(doc2).get(PATH_FIELD) + "\t"
                    + similarity;
            simLines.add(line);
        }
    }

    // Release resources
    reader.close();
    if (expander != null) {
        expander.close();
    }

    // Save search results
    System.out.println("\n[INFO] Search results are saved in file: " + resultFile.getPath());
    FileUtils.writeLines(resultFile, hitLines, false);

    System.out.println("\n[INFO] Cosine similarities are saved in file: " + similarityFile.getPath());
    FileUtils.writeLines(similarityFile, simLines, false);
}

From source file:com.sojw.TableNamesFinderExecutor.java

/**
 * @param args//from   ww  w .  ja  v a  2 s. c  o  m
 */
public static void main(String[] args) throws Exception {
    List<CSVRecord> fileContentList = null;
    try {
        fileContentList = getCSVFileContents(FILE_ROOT_DIR + SOURCE_FILE_NAME);
    } catch (IOException e) {
        System.out.println("  .");
        System.exit(-1);
    }

    if (fileContentList == null || fileContentList.isEmpty()) {
        System.out.println("  .");
        System.exit(1);
    }

    //  
    final Set<String> iterMapKeys = iterMap.keySet();
    for (final String mapkey : iterMapKeys) {
        final List<String> iterList = iterMap.get(mapkey);
        for (final String tableName : iterList) {
            final List<String> resultList = extractTableNameByRegex(tableName, fileContentList);
            final String outputFileName = FILE_ROOT_DIR
                    + String.format(OUTPUT_FILE_NAME_FORMAT, mapkey, tableName);

            FileUtils.deleteQuietly(new File(outputFileName));
            FileUtils.writeLines(new File(outputFileName), resultList, false);
        }
    }

    // Zql
    //      final Set<String> iterMapKeys = iterMap.keySet();
    //      for (String mapkey : iterMapKeys) {
    //         final List<String> iterList = iterMap.get(mapkey);
    //         for (final String tableName : iterList) {
    //            extractTableNameByZql(fileContentList);
    //         }
    //      }

}

From source file:com.github.stagirs.lingvo.syntax.disambiguity.mystem.MyStem.java

public static List<SyntaxItem[][]> process(List<String> text) throws IOException, InterruptedException {
    FileUtils.writeLines(new File("mystem_input"), "utf-8", text);
    Process process = Runtime.getRuntime()
            .exec(new String[] { "./mystem", "-c", "-i", "-d", "mystem_input", "mystem_output" });
    process.waitFor();//  ww  w .ja  v  a 2s  .c  om
    InputStream is = process.getInputStream();
    try {
        List<String> lines = FileUtils.readLines(new File("mystem_output"), "utf-8");
        List<SyntaxItem[][]> resultList = new ArrayList<SyntaxItem[][]>();
        for (String line : lines) {
            String[] parts = line.split(" ");
            SyntaxItem[][] result = new SyntaxItem[parts.length][];
            for (int i = 0; i < result.length; i++) {
                if (parts[i].isEmpty()) {
                    result[i] = new SyntaxItem[0];
                    continue;
                }
                if (!parts[i].contains("{")) {
                    final String word = parts[i];
                    result[i] = new SyntaxItem[] { new SyntaxItem() {
                        @Override
                        public String getName() {
                            return word;
                        }

                        @Override
                        public String getType() {
                            return "";
                        }

                        @Override
                        public double getScore() {
                            return 0;
                        }
                    } };
                    continue;
                }
                final String word = parts[i].substring(0, parts[i].indexOf('{'));
                final String[] forms = parts[i].substring(parts[i].indexOf('{') + 1, parts[i].indexOf('}'))
                        .split("\\|");
                result[i] = new SyntaxItem[forms.length];
                for (int j = 0; j < result[i].length; j++) {
                    final String type = forms[j];
                    result[i][j] = new SyntaxItem() {
                        @Override
                        public String getName() {
                            return word;
                        }

                        @Override
                        public String getType() {
                            return type;
                        }

                        @Override
                        public double getScore() {
                            return 0;
                        }
                    };
                }
            }
            resultList.add(result);
        }
        return resultList;
    } finally {
        is.close();
    }
}

From source file:alilibs.AliFile.java

public static void append(String filename, List<String> list) throws IOException {
    File f = new File(filename);
    FileUtils.writeLines(f, list, true);

    ////from  ww  w.j av a 2 s . c o m

}