using WordNet to perform stemming, io error: Too many open files

using WordNet to perform stemming, io error: Too many open files

我正在使用 WordNet 对输入文本文件执行词干提取。

对于具有少量实例的输入,一切正常,但是当我增加输入实例的数量时,程序崩溃并出现以下错误:

Exception in thread "main" java.io.FileNotFoundException: /usr/local/WordNet-3.0/dict/index.adj (Too many open files)

我想我需要关闭一些打开的东西,但我不太确定是什么,我尝试根据 this 问题实施解决方案,试图关闭正在读入的文件,但是没用。这使我相信它更多地与 WordNet 有关,尽管我不能确定。

我对java io 操作不是很熟悉。

也许这方面的专家可以指出我哪里出错了。

这是我读取输入文件的地方:

public void readFile( String input, 
                      Ontology ontology, 
                      List<Sentence> sentences, 
                      Map<String,List<Integer>> subject2index, 
                      Map<String,List<Integer>> object2index,
                      Set<String> joints) throws IOException 
{   
    // do what we came here to do, read the input
    @SuppressWarnings("resource")
    BufferedReader br = new BufferedReader(new FileReader( input ));

    //now that we're reading the file we need to send it on
    //to the regex extractot after we're through
    RegEx regex_extractor = new RegEx();

    String line;
    while ((line = br.readLine()) != null) 
    {
        regex_extractor.match_regex_patterns(line, ontology, sentences, subject2index, object2index, joints);
    }
}

这是 WordNet 组件:

public WordNet() throws IOException
{ 
    // construct the URL to the Wordnet dictionary directory
    wnhome = System.getenv("WNHOME");
    path = wnhome + File.separator + "dict";
    url = new URL ("file", null , path );

    // construct the dictionary object and open it
    dict = new Dictionary ( url ) ;
    dict.open();
}

public String getStem(String word)
{
    WordnetStemmer stem =  new WordnetStemmer( dict );

    List<String> stemmed_words = stem.findStems(word,  POS.VERB);

    if ( !stemmed_words.isEmpty() )
        return stemmed_words.get(0);
    else
        return word;
}

¡试试这个!

    // do what we came here to do, read the input
    @SuppressWarnings("resource")
    BufferedReader br = new BufferedReader(new FileReader( input ));

    try 
    {

        //now that we're reading the file we need to send it on
        //to the regex extractot after we're through
        RegEx regex_extractor = new RegEx();

        String line;
        while ((line = br.readLine()) != null) 
        {
            regex_extractor.match_regex_patterns(line, ontology, sentences, subject2index, object2index, joints);
        }
    } 
    catch (IOException ex) 
    {
        // ...
    } 
    finally 
    {
        if (br != null) 
        {
            try 
            {
                br.close();
            } 
            catch (IOException ex) 
            {
                // ignore
            }
        }
    }