edu.illinois.cs.cogcomp.core.io.IOUtils类的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(9.3k)|赞(0)|评价(0)|浏览(158)

本文整理了Java中edu.illinois.cs.cogcomp.core.io.IOUtils类的一些代码示例,展示了IOUtils类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IOUtils类的具体详情如下:
包路径:edu.illinois.cs.cogcomp.core.io.IOUtils
类名称:IOUtils

IOUtils介绍

[英]Some utility functions involving files and directory. Some of these commands are modeled after Unix commands.
[中]一些实用程序函数涉及文件和目录。其中一些命令是以Unix命令为模型的。

代码示例

代码示例来源:origin: edu.illinois.cs.cogcomp/illinois-nlp-readers

/**
 * print output into a file in directory specified, with name based on annotationFile.
 * Should not create an empty file (i.e., if columnOutput is empty).
 *
 * @param nerOutputDir  directory to write output file
 * @param annotationFile    used as prefix for the name of the new file
 * @param columnOutput  a list of strings to be printed to the output file
 * @throws IOException
 */
private static void printOut(String nerOutputDir, String annotationFile, List<String> columnOutput) throws IOException {
  String outFile = nerOutputDir + "/" + annotationFile + ".ner.column.txt" ;
  if ( !columnOutput.isEmpty() ) {
    if ( !IOUtils.exists( nerOutputDir ) )
      IOUtils.mkdir( nerOutputDir );
    LineIO.write(outFile, columnOutput);
  }
}

代码示例来源:origin: CogComp/cogcomp-nlp

/**
 * Load the file.
 * 
 * @param path path to .fex file
 * @throws Exception
 */
public FeatureManifest(String path) throws Exception {
  this(IOUtils.lsResources(FeatureManifest.class, path).get(0).openStream());
}

代码示例来源:origin: CogComp/cogcomp-nlp

/**
 * Create a directory, if it does not exist.
 */
public static boolean mkdir(String dir) {
  if (!exists(dir)) {
    return (new File(dir)).mkdirs();
  } else {
    return isDirectory(dir);
  }
}

代码示例来源:origin: CogComp/cogcomp-nlp

public static String getFileStem(String path) {
  return stripFileExtension(getFileName(path));
}

代码示例来源:origin: CogComp/cogcomp-nlp

/**
 * Delete a file
 *
 * @return true only if the delete was successful
 */
public static boolean rm(String file) throws IOException {
  if (!exists(file))
    return false;
  if (!isFile(file))
    throw new IOException(file + " is not a file!");
  return (new File(file)).delete();
}

代码示例来源:origin: edu.illinois.cs.cogcomp/esrl-core

failed = new ArrayList<>();
failedFile = directory + File.separator + "failed.txt";
if (IOUtils.exists(directory) && IOUtils.isDirectory(directory)) {
  String[] files = new String[0];
  try {
    if (IOUtils.exists(failedFile)) {
      failed = LineIO.read(failedFile, new ITransformer<String, Integer>() {
        @Override
    files = IOUtils.lsFiles(directory, new FilenameFilter() {
      @Override
      public boolean accept(File dir, String name) {
        (files.length + failed.size()), failed.size());
  for (String file : files) {
    String taHashStr = IOUtils.stripFileExtension(IOUtils.getFileName(file));
  IOUtils.mkdir(directory);

代码示例来源:origin: CogComp/cogcomp-nlp

throw new IllegalArgumentException("Input directory and output directory must have different names ('" + inDir + "'");
if (!IOUtils.isDirectory(inDir)) {
  throw new IOException("input directory '" + inDir + "' is not a directory.");
if (IOUtils.exists(outDir)) {
  if (!IOUtils.isDirectory(outDir))
    throw new IOException("output directory '" + outDir + "' is not a directory.");
  IOUtils.mkdir(outDir);
String[] inFiles = IOUtils.lsFilesRecursive(inDir, filter);

代码示例来源:origin: CogComp/cogcomp-nlp

if (IOUtils.exists(conllDir))
  if (!IOUtils.isDirectory(conllDir)) {
    System.err.println("Output directory '" + conllDir
        + "' exists and is not a directory.");
    System.exit(-1);
  } else
    IOUtils.mkdir(conllDir);

代码示例来源:origin: CogComp/cogcomp-nlp

public static boolean dbFileExists(String dbFile) {
  boolean create = false;
  if (!IOUtils.exists(dbFile + ".mv.db"))
    create = true;
  return create;
}

代码示例来源:origin: edu.illinois.cs.cogcomp/esrl-core

/**
 * A workaround for the unit tests in Maven that move the relative path of the root directory
 * in {@link ESRLConfigurator} to the directory of each module. <b>NB:</b> This code assumes
 * the default data directory to be <i>$ROOT_DIR/data/</i>.
 *
 * @param file The file/directory to be used
 * @return The same file/directory moved to the root dir of the main project
 */
protected String getCorrectPath(String file) {
  if (file.contains("data") && !IOUtils.exists(new File(file).getParent())) {
    int dataIndex = file.indexOf("data") - 1;
    int prevSlashIndex = file.lastIndexOf(File.separator, dataIndex - 1);
    String fileWithParentDir = file.substring(0, prevSlashIndex) + file.substring(dataIndex, file.length());
    logger.warn("{} doesn't exist, trying parent directory: {}.", IOUtils.getFileName(file), fileWithParentDir);
    file = fileWithParentDir;
  }
  return file;
}

代码示例来源:origin: CogComp/cogcomp-nlp

/**
 * A table is built from either a given source corpus file or source corpus directory by simply
 * counting the number of times that each form-POS association appear in a source corpus.
 * 
 * @param home file name or directory name of the source corpus
 * @throws Exception
 **/
public void buildTable(String home) throws Exception {
  if (IOUtils.isFile(home))
    this.buildTableHelper(home);
  else if (IOUtils.isDirectory(home)) {
    String[] files = IOUtils.lsFiles(home);
    for (String file : files) {
      // logger.info(file);
      this.buildTableHelper(home + "\\" + file);
    }
  }
}

代码示例来源:origin: CogComp/cogcomp-nlp

@CommandDescription(
    description = "Pre-extracts the features for the verb-sense model. Run this before training.",
    usage = "preExtract")
public static void preExtract() throws Exception {
  SenseManager manager = getManager(true);
  ResourceManager conf = new VerbSenseConfigurator().getDefaultConfig();
  // If models directory doesn't exist create it
  if (!IOUtils.isDirectory(conf.getString(conf
      .getString(VerbSenseConfigurator.MODELS_DIRECTORY))))
    IOUtils.mkdir(conf.getString(conf.getString(VerbSenseConfigurator.MODELS_DIRECTORY)));
  int numConsumers = Runtime.getRuntime().availableProcessors();
  Dataset dataset = Dataset.PTBTrainDev;
  log.info("Pre-extracting features");
  ModelInfo modelInfo = manager.getModelInfo();
  String featureSet = "" + modelInfo.featureManifest.getIncludedFeatures().hashCode();
  String allDataCacheFile =
      VerbSenseConfigurator.getFeatureCacheFile(featureSet, dataset, rm);
  FeatureVectorCacheFile featureCache =
      preExtract(numConsumers, manager, dataset, allDataCacheFile);
  pruneFeatures(numConsumers, manager, featureCache,
      VerbSenseConfigurator.getPrunedFeatureCacheFile(featureSet, rm));
  Lexicon lexicon = modelInfo.getLexicon().getPrunedLexicon(manager.getPruneSize());
  log.info("Saving lexicon  with {} features to {}", lexicon.size(),
      manager.getLexiconFileName());
  log.info(lexicon.size() + " features in the lexicon");
  lexicon.save(manager.getLexiconFileName());
}

代码示例来源:origin: CogComp/cogcomp-nlp

files = IOUtils.lsResources(GazetteerViewGenerator.class, directory);
else
  files = IOUtils.getListOfFilesInDir(directory);
  String file = IOUtils.getFileName(url.getPath());

代码示例来源:origin: CogComp/cogcomp-nlp

public void writeModelsToDisk(String dir, String modelName){
  IOUtils.mkdir(dir);
  chunker.write(dir + File.separator + modelName + ".lc", dir + File.separator + modelName + ".lex");
  logger.info("Done training, models are in " + dir+File.separator+modelName+".lc (.lex)");
}
public static void main(String[] args) {

代码示例来源:origin: edu.illinois.cs.cogcomp/illinois-core-utilities

/**
 * Filters the files contained in a directory or in its subdirectory structure. Returns all
 * files (not directories) that pass the filter.
 */
public static String[] lsFilesRecursive(String directory, FilenameFilter filter)
    throws IOException {
  File dir = new File(directory);
  ArrayList<String> files = new ArrayList<>();
  for (File filepath : dir.listFiles(filter)) {
    if (isFile(filepath.getAbsolutePath()))
      files.add(filepath.getAbsolutePath());
    else if (isDirectory(filepath.getAbsolutePath()))
      files.addAll(Arrays.asList(lsFilesRecursive(filepath.getAbsolutePath(), filter)));
  }
  return files.toArray(new String[files.size()]);
}

代码示例来源:origin: CogComp/cogcomp-nlp

String lemma = IOUtils.stripFileExtension(fileName);

代码示例来源:origin: CogComp/cogcomp-nlp

String outDir = args[1];
IOUtils.mkdir(outDir);
  inFiles = IOUtils.lsFiles(inDir);
} catch (IOException e) {
  e.printStackTrace();

代码示例来源:origin: CogComp/cogcomp-nlp

String tmpFile = tmpDir + "/google.ngrams.get1t" + (new Random()).nextInt();
IOUtils.mkdir(tmpDir);
  IOUtils.rm(tmpFile);
  IOUtils.rm(outputFile);
  IOUtils.rm(numTokens + "-total.txt");

代码示例来源:origin: edu.illinois.cs.cogcomp/saul-examples

private void readPropbankFrameData(String dir) throws Exception {
  frameData = new HashMap<>();
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  dbf.setNamespaceAware(false);
  dbf.setValidating(false);
  for (String file : IOUtils.lsFiles(dir, (dir1, name) -> name.endsWith("xml"))) {
    // IOUtils.getFileName(file) doesn't work in Windows
    int slashIndex = file.lastIndexOf(File.separator);
    String fileName = file.substring(slashIndex + 1);
    // A hack to deal with percent-sign in nombank. There is another
    // file called perc-sign that will fill this void.
    if (fileName.contains("percent-sign.xml"))
      continue;
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);
    NodeList predicateElements = doc.getElementsByTagName("predicate");
    for (int i = 0; i < predicateElements.getLength(); i++) {
      String lemma = IOUtils.stripFileExtension(fileName);
      FrameData fData = new FrameData(lemma);
      frameData.put(lemma, fData);
      NodeList roleSets = doc.getElementsByTagName("roleset");
      addRoleSets(fileName, lemma, fData, roleSets);
    }
  }
}

代码示例来源:origin: CogComp/cogcomp-nlp

public void readFile(String fileName) {
  try {
    List<String> lines = LineIO.read(fileName);
    for (int i = 0; i < lines.size(); i++) {
      String line = lines.get(i);
      String sentId = IOUtils.getFileName(fileName) + ":" + i;
      textAnnotations.add(createTextAnnotation(line, sentId));
    }
  } catch (FileNotFoundException e) {
    logger.error("Could not read {}; unable to continue.", fileName);
    throw new RuntimeException(e);
  }
}

相关文章