Skip to content

Commit

Permalink
Fixed error where Properties File was not closed after loading.
Browse files Browse the repository at this point in the history
Fixed error where File resource was not closed when loading properties
from file.

Also updated incorrect examples.

This update addresses part #1 of issue #13
 (#13)
  • Loading branch information
willjrogers committed Feb 25, 2021
1 parent 3dc08d6 commit 8aae393
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 41 deletions.
39 changes: 21 additions & 18 deletions README.bioc
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,33 @@ Below is an example using BioC Processing with MetaMapLite
"config/metamaplite.properties");
Properties configProperties = new Properties();
// set any in-line properties here.
configProperties.load(new FileReader(configPropertiesFilename));
configProperties.setProperty("metamaplite.semanticgroup",
"acab,anab,bact,cgab,dsyn,emod,inpo,mobd,neop,patf,sosy");
Properties properties =
Configuration.mergeConfiguration(configProperties,
FileReader fr = new FileReader(configPropertiesFilename);
configProperties.load(fr);
fr.close();

configProperties.setProperty("metamaplite.semanticgroup",
"acab,anab,bact,cgab,dsyn,emod,inpo,mobd,neop,patf,sosy");
Properties properties =
Configuration.mergeConfiguration(configProperties,
defaultConfiguration);
BioCProcess process = new BioCProcess(properties);
BioCProcess process = new BioCProcess(properties);

// read BioC XML collection
Reader inputReader = new FileReader(inputFile);
BioCFactory bioCFactory = BioCFactory.newFactory("STANDARD");
BioCCollectionReader collectionReader =
bioCFactory.createBioCCollectionReader(inputReader);
BioCCollection collection = collectionReader.readCollection();
// read BioC XML collection
Reader inputReader = new FileReader(inputFile);
BioCFactory bioCFactory = BioCFactory.newFactory("STANDARD");
BioCCollectionReader collectionReader =
bioCFactory.createBioCCollectionReader(inputReader);
BioCCollection collection = collectionReader.readCollection();

// Run named entity recognition on collection
BioCCollection newCollection = process.processCollection(collection);
// Run named entity recognition on collection
BioCCollection newCollection = process.processCollection(collection);

// write out the annotated collection
File outputFile = new File(outputFilename);
Writer outputWriter = new PrintWriter(outputFile, "UTF-8");
BioCCollectionWriter collectionWriter = bioCFactory.createBioCCollectionWriter(outputWriter);
collectionWriter.writeCollection(newCollection);
outputWriter.close();
Writer outputWriter = new PrintWriter(outputFile, "UTF-8");
BioCCollectionWriter collectionWriter = bioCFactory.createBioCCollectionWriter(outputWriter);
collectionWriter.writeCollection(newCollection);
outputWriter.close();

This process attempts to preserve any annotation present in the
original BioC XML input. Some annotations applied to BioC
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,9 @@ Creating properties for configuring MetaMapLite Instance:

Loading properties file in "config":

myProperties.load(new FileReader("config/metamaplite.properties"));

FileReader fr = new FileReader("config/metamaplite.properties");
myProperties.load(fr);
fr.close();

Creating a metamap lite instance:

Expand Down Expand Up @@ -583,7 +584,9 @@ directory and specify that in servlet:
this.properties.setProperty("metamaplite.index.directory","data/ivf/strict");
...
// load user properties
this.properties.load(new FileReader(configPropertyFilename));
FileReader fr = new FileReader(configPropertyFilename);
myProperties.load(fr);
fr.close();
this.metaMapLiteInst = new MetaMapLite(this.properties);
...
} catch (Exception e) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/examples/DirectLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public DirectLookup()
this.myProperties.setProperty("metamaplite.excluded.termsfile", "data/specialterms.txt");
// Loading properties file in "config", overriding previously
// defined properties.
this.myProperties.load(new FileReader("config/metamaplite.properties"));
FileReader fr = new FileReader("config/metamaplite.properties");
myProperties.load(fr);
fr.close();
this.mmIndexes = new MetaMapIvfIndexes(this.myProperties);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/examples/DirectLookupTermFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public DirectLookupTermFile()
this.myProperties.setProperty("metamaplite.excluded.termsfile", "data/specialterms.txt");
// Loading properties file in "config", overriding previously
// defined properties.
this.myProperties.load(new FileReader("config/metamaplite.properties"));
FileReader fr = new FileReader("config/metamaplite.properties");
myProperties.load(fr);
fr.close();
this.mmIndexes = new MetaMapIvfIndexes(this.myProperties);
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/examples/EntityLookup5Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public EntityLookup5Client() {
MetaMapLite.expandIndexDir(myProperties, installdir + "/data/ivf/2017AA/USAbase/strict");
myProperties.setProperty("metamaplite.excluded.termsfile",
installdir + "/data/specialterms.txt");
myProperties.load(new FileReader(installdir + "/config/metamaplite.properties"));
FileReader fr = new FileReader(installdir + "/config/metamaplite.properties");
myProperties.load(fr);
fr.close();
myProperties.list(System.out);
this.sentenceAnnotator = new OpenNLPPoSTagger(myProperties);
this.sentenceExtractor = new OpenNLPSentenceExtractor(myProperties);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/examples/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public static void main(String[] args)
"/export/home/wjrogers/Projects/metamaplite/data/specialterms.txt");
// Loading properties file in "config", overriding previously
// defined properties.
myProperties.load(new FileReader("config/metamaplite.properties"));
FileReader fr = new FileReader("config/metamaplite.properties");
myProperties.load(fr);
fr.close();

MetaMapLite metaMapLiteInst = new MetaMapLite(myProperties);

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/examples/Example2.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ public static void main(String[] args)
MetaMapLite.expandIndexDir(myProperties);
// Loading properties file in "config", overriding previously
// defined properties.
myProperties.load(new FileReader("config/metamaplite.properties"));
FileReader fr = new FileReader("config/metamaplite.properties");
myProperties.load(fr);
fr.close();
MetaMapLite metaMapLiteInst = new MetaMapLite(myProperties);

// Processing Section
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/examples/Example3.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public static void main(String[] args)
MetaMapLite.expandIndexDir(myProperties);
// Loading properties file in "config", overriding previously
// defined properties.
myProperties.load(new FileReader("config/metamaplite.properties"));
FileReader fr = new FileReader("config/metamaplite.properties");
myProperties.load(fr);
fr.close();
MetaMapLite metaMapLiteInst = new MetaMapLite(myProperties);
MMI mmiInstance = new MMI();
mmiInstance.initProperties(myProperties);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/examples/ListAcronyms.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public static void main(String[] args)
"/export/home/wjrogers/Projects/metamaplite/data/specialterms.txt");
MetaMapLite.expandIndexDir(myProperties);
// override any properties with those in property file.
myProperties.load(new FileReader("config/metamaplite.properties"));
FileReader fr = new FileReader("config/metamaplite.properties");
myProperties.load(fr);
fr.close();
SentenceExtractor sentenceExtractor = new OpenNLPSentenceExtractor(myProperties);
ExtractAbbrev extractAbbr = new ExtractAbbrev();
// Processing Section
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/examples/StreamPassageSentences.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ public StreamPassageSentences(String metaMapLiteRootDir,
metaMapLiteRootDir + "/data/specialterms.txt");
// Loading properties file in "config", overriding previously
// defined properties.
// this.myProperties.load(new FileReader("config/metamaplite.properties"));
// FileReader fr = new FileReader("config/metamaplite.properties");
// myProperties.load(fr);
// fr.close();
// this.mmIndexes = new MetaMapIvfIndexes(this.myProperties);
this.myProperties.setProperty("metamaplite.postaglist",
"CD,FW,RB,NN,NNS,NNP,NNPS,JJ,JJR,JJS,LS");
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/gov/nih/nlm/nls/metamap/lite/BioCPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,9 @@ static BioCPipeline initPipeline()
logger.debug("enter initPipeline");

Properties properties = new Properties();
properties.load(new FileReader(configPropertyFilename));
FileReader fr = new FileReader(configPropertyFilename);
properties.load(fr);
fr.close();
if (logger.isDebugEnabled()) {
for (Map.Entry<Object,Object> entry: properties.entrySet()) {
logger.debug(entry.getKey() + " -> " + entry.getValue());
Expand Down Expand Up @@ -457,7 +459,9 @@ static Properties setConfiguration(String propertiesFilename,
if (verbose) {
System.out.println("loading local configuration from " + localConfigurationFile);
}
localConfiguration.load(new FileReader(localConfigurationFile));
FileReader fr = new FileReader(localConfigurationFile);
localConfiguration.load(fr);
fr.close();
if (verbose) {
System.out.println("loaded " + localConfiguration.size() + " records from local configuration");
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/gov/nih/nlm/nls/metamap/lite/BioCProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ public static void main(String[] args)
String configPropertiesFilename = System.getProperty("metamaplite.propertyfile",
"config/metamaplite.properties");
Properties configProperties = new Properties();
configProperties.load(new FileReader(configPropertiesFilename));
FileReader fr = new FileReader(configPropertiesFilename);
configProperties.load(fr);
fr.close();
Properties properties =
Configuration.mergeConfiguration(configProperties,
defaultConfiguration);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/gov/nih/nlm/nls/metamap/lite/Pipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ static Pipeline initPipeline()
logger.debug("enter initPipeline");

Properties properties = new Properties();
properties.load(new FileReader(configPropertyFilename));
FileReader fr = new FileReader(configPropertyFilename);
properties.load(fr);
fr.close();
if (logger.isDebugEnabled()) {
for (Map.Entry<Object,Object> entry: properties.entrySet()) {
logger.debug(entry.getKey() + " -> " + entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ public void init()
throws IOException, FileNotFoundException
{
Properties properties = new Properties();
// properties.load(?);
this.entityLookup = new EntityLookup4(properties);
initSentenceDetector();
initTokenizer();
Expand Down
53 changes: 46 additions & 7 deletions src/main/java/gov/nih/nlm/nls/ner/MetaMapLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,37 @@ public List<AbbrInfo> getAcronymList(List<BioCDocument> documentList) {
return infos;
}

/** Document abbreviation information class */
public class DocInfo {
/** document id */
String id;
/** list of abbreviation information instances */
List <AbbrInfo> infolist;
public DocInfo(String id, List <AbbrInfo> infos) {
this.id = id;
this.infolist = infos;
}
public String getId() { return this.id; }
public List<AbbrInfo> getInfolist() { return this.infolist; }
}

public List<DocInfo> getDocAcronymList(List<BioCDocument> documentList) {
List <DocInfo> docInfoList = new ArrayList<DocInfo>();
for (BioCDocument document: documentList) {
List <AbbrInfo> infos = new ArrayList<AbbrInfo>();
for (BioCPassage passage: document.getPassages()) {
// for (Sentence sentence: this.sentenceExtractor.createSentenceList(passage.getText())) {
for (AbbrInfo abbrInfo: extractAbbr.extractAbbrPairsString(passage.getText())) {
infos.add(new AbbrInfo(abbrInfo.shortForm.replace("\n", " "), abbrInfo.shortFormIndex,
abbrInfo.longForm.replace("\n", " "), abbrInfo.longFormIndex));
}
//}
}
docInfoList.add(new DocInfo(document.getID(), infos));
}
return docInfoList;
}

public static List<String> loadInputFileList(String inputfileListFileName)
throws FileNotFoundException, IOException
{
Expand Down Expand Up @@ -828,7 +859,9 @@ static Properties setConfiguration(String propertiesFilename,
if (verbose) {
System.out.println("loading local configuration from " + localConfigurationFile);
}
localConfiguration.load(new FileReader(localConfigurationFile));
FileReader fr = new FileReader(localConfigurationFile);
localConfiguration.load(fr);
fr.close();
logger.info("loaded " + localConfiguration.size() + " records from local configuration");
if (verbose) {
System.out.println("loaded " + localConfiguration.size() + " records from local configuration");
Expand Down Expand Up @@ -895,9 +928,12 @@ void listSentences(List<BioCDocument> documentList)
void listAcronyms(List<BioCDocument> documentList) {
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out,
Charset.forName("utf-8")));
for (AbbrInfo acronym: this.getAcronymList(documentList)) {
pw.println(acronym.shortForm + "|" + acronym.shortFormIndex + "|" +
acronym.longForm + "|" + acronym.longFormIndex );
for (DocInfo docInfo: this.getDocAcronymList(documentList)) {
for (AbbrInfo acronym: docInfo.getInfolist()) {
pw.println(docInfo.getId() + "|" +
acronym.shortForm + "|" + acronym.shortFormIndex + "|" +
acronym.longForm + "|" + acronym.longFormIndex );
}
}
pw.flush();
}
Expand Down Expand Up @@ -1011,9 +1047,12 @@ void listAcronyms(String filename,
File outputFile = abortIfFileExists(outputFilename, overwritefile);
PrintWriter pw = new PrintWriter(new BufferedWriter
(new FileWriter(outputFile)));
for (AbbrInfo acronym: this.getAcronymList(documentList)) {
pw.println(acronym.shortForm + "|" + acronym.shortFormIndex + "|" +
acronym.longForm + "|" + acronym.longFormIndex );
for (DocInfo docInfo: this.getDocAcronymList(documentList)) {
for (AbbrInfo acronym: docInfo.getInfolist()) {
pw.println(docInfo.getId() + "|" +
acronym.shortForm + "|" + acronym.shortFormIndex + "|" +
acronym.longForm + "|" + acronym.longFormIndex );
}
}
pw.close();
}
Expand Down

0 comments on commit 8aae393

Please sign in to comment.