Skip to content

Commit

Permalink
#10 - Using WordNet as Maven Dependency
Browse files Browse the repository at this point in the history
- Added default resource file containing a wordnet resource used when none is specified otherwise
- Allow loading wordnet from Maven dependency
- Use Mavenized wordnet in test
- Formatting
  • Loading branch information
reckart committed Apr 7, 2018
1 parent 0133a27 commit af85460
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class ResourceFactory
{
public static final String ENV_DKPRO_HOME = "DKPRO_HOME";
public final static String CONFIG_FILE = "resources.xml";
public final static String DEFAULT_CONFIG_FILE = "/de/tudarmstadt/ukp/dkpro/lexsemresource/core/default-resources.xml";

private static ResourceFactory loader;

Expand Down Expand Up @@ -72,8 +73,8 @@ public static synchronized ResourceFactory getInstance()
// Check in classpath
if (resourceXmlUrl == null) {
resourceXmlUrl = ResourceFactory.class
.getResource(CONFIG_FILE);
locs.add("Classpath: " + CONFIG_FILE);
.getResource("/"+CONFIG_FILE);
locs.add("Classpath: /" + CONFIG_FILE);
}

// Check in default file system location
Expand All @@ -87,6 +88,14 @@ public static synchronized ResourceFactory getInstance()
locs.add(new File(CONFIG_FILE).getAbsolutePath());
}

// Check default resources file in classpath (this should never fail)
if (resourceXmlUrl == null) {
resourceXmlUrl = ResourceFactory.class
.getResource(DEFAULT_CONFIG_FILE);
locs.add("Classpath: " + DEFAULT_CONFIG_FILE);
}


// Bail out if still not found
if (resourceXmlUrl == null) {
throw new ResourceLoaderException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="wordnet-en" lazy-init="true"
class="de.tudarmstadt.ukp.lexsemresource.resource.WordNetResource">
</bean>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,31 @@ public class EntityGraphJGraphT

private LexicalSemanticResource lexSemRes;

public EntityGraphJGraphT()
{
this(null);
}

public EntityGraphJGraphT(File aGraphDirectory)
{
if (aGraphDirectory != null) {
graphDirectory = aGraphDirectory;
}
else {
if (System.getenv(ResourceFactory.ENV_DKPRO_HOME) == null) {
throw new IllegalStateException(
"Environment variable [" + ResourceFactory.ENV_DKPRO_HOME + "] not set");
}

graphDirectory = new File(System.getenv(ResourceFactory.ENV_DKPRO_HOME)
+ "/" + EntityGraphJGraphT.class.getName());
}

if (!graphDirectory.exists()) {
graphDirectory.mkdirs();
}
}

protected EntityGraphJGraphT getEntityGraphJGraphT(LexicalSemanticResource aLsr)
throws LexicalSemanticResourceException
{
Expand All @@ -114,17 +139,6 @@ protected EntityGraphJGraphT getEntityGraphJGraphT(LexicalSemanticResource lexSe
graphId = "graphSer_" + lexSemResource.getResourceName() + nameSuffix + "_"
+ lexSemResource.getResourceVersion();

if (System.getenv(ResourceFactory.ENV_DKPRO_HOME) == null) {
throw new LexicalSemanticResourceException("Environment variable ["
+ ResourceFactory.ENV_DKPRO_HOME + "] not set");
}

graphDirectory = new File(System.getenv(ResourceFactory.ENV_DKPRO_HOME)
+ "/" + EntityGraphJGraphT.class.getName());
if (!graphDirectory.exists()) {
graphDirectory.mkdir();
}

serializedGraphFile = new File(graphDirectory, graphId);
if (serializedGraphFile.exists()) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*******************************************************************************/
package de.tudarmstadt.ukp.dkpro.lexsemresource.graph;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -41,14 +42,21 @@ public enum EntityGraphType
JGraphT, JUNG
}

public static EntityGraph getEntityGraph(LexicalSemanticResource lsr, EntityGraphType type)
throws LexicalSemanticResourceException
{
public static EntityGraph getEntityGraph(LexicalSemanticResource lsr, EntityGraphType type)
throws LexicalSemanticResourceException
{
return getEntityGraph(lsr, type, null);
}

public static EntityGraph getEntityGraph(LexicalSemanticResource lsr, EntityGraphType type,
File aGraphDirectory)
throws LexicalSemanticResourceException
{
String graphID = getGraphID(lsr, "");
if (!entityGraphMap.containsKey(graphID)) {
EntityGraph entityGraph = null;
if (type.equals(EntityGraphType.JGraphT)) {
EntityGraphJGraphT entityGraphJGraphT = new EntityGraphJGraphT();
EntityGraphJGraphT entityGraphJGraphT = new EntityGraphJGraphT(aGraphDirectory);
entityGraph = entityGraphJGraphT.getEntityGraphJGraphT(lsr, lsr.getEntities(),
"", lsr.getNumberOfEntities());
}
Expand Down
72 changes: 40 additions & 32 deletions de.tudarmstadt.ukp.dkpro.lexsemresource.wordnet-asl/pom.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<!--
Copyright 2016
Ubiquitous Knowledge Processing (UKP) Lab
Technische Universität Darmstadt
Copyright 2016
Ubiquitous Knowledge Processing (UKP) Lab
Technische Universität Darmstadt
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>de.tudarmstadt.ukp.dkpro.lexsemresource-asl</artifactId>
Expand All @@ -24,28 +26,34 @@
</parent>
<artifactId>de.tudarmstadt.ukp.dkpro.lexsemresource.wordnet-asl</artifactId>
<dependencies>
<dependency>
<groupId>de.tudarmstadt.ukp.dkpro.lexsemresource</groupId>
<artifactId>de.tudarmstadt.ukp.dkpro.lexsemresource.api-asl</artifactId>
</dependency>
<dependency>
<groupId>de.tudarmstadt.ukp.dkpro.lexsemresource</groupId>
<artifactId>de.tudarmstadt.ukp.dkpro.lexsemresource.core-asl</artifactId>
</dependency>
<dependency>
<groupId>net.sf.extjwnl</groupId>
<artifactId>extjwnl</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>edu.mit</groupId>
<artifactId>jwi</artifactId>
<version>2.2.3</version>
<groupId>de.tudarmstadt.ukp.dkpro.lexsemresource</groupId>
<artifactId>de.tudarmstadt.ukp.dkpro.lexsemresource.api-asl</artifactId>
</dependency>
<dependency>
<groupId>de.tudarmstadt.ukp.dkpro.lexsemresource</groupId>
<artifactId>de.tudarmstadt.ukp.dkpro.lexsemresource.graph-asl</artifactId>
<scope>test</scope>
<groupId>de.tudarmstadt.ukp.dkpro.lexsemresource</groupId>
<artifactId>de.tudarmstadt.ukp.dkpro.lexsemresource.core-asl</artifactId>
</dependency>
<dependency>
<groupId>net.sf.extjwnl</groupId>
<artifactId>extjwnl</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>edu.mit</groupId>
<artifactId>jwi</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>de.tudarmstadt.ukp.dkpro.lexsemresource</groupId>
<artifactId>de.tudarmstadt.ukp.dkpro.lexsemresource.graph-asl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sf.extjwnl</groupId>
<artifactId>extjwnl-data-wn30</artifactId>
<version>1.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@
import net.sf.extjwnl.dictionary.Dictionary;
import net.sf.extjwnl.dictionary.Dictionary.Version;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.tudarmstadt.ukp.dkpro.lexsemresource.Entity;
import de.tudarmstadt.ukp.dkpro.lexsemresource.Entity.PoS;
import de.tudarmstadt.ukp.dkpro.lexsemresource.core.AbstractResource;
import de.tudarmstadt.ukp.dkpro.lexsemresource.exception.LexicalSemanticResourceException;
import de.tudarmstadt.ukp.dkpro.lexsemresource.wordnet.util.WordNetEntityIterable;
import de.tudarmstadt.ukp.dkpro.lexsemresource.wordnet.util.WordNetUtils;

public class WordNetResource extends AbstractResource {
private final Log logger = LogFactory.getLog(getClass());

public class WordNetResource
extends AbstractResource
{
private static final String RESOURCE_NAME = "WordNet";

private Dictionary dict;
Expand All @@ -62,32 +59,48 @@ public class WordNetResource extends AbstractResource {

private int numberOfEntities = -1;

public WordNetResource(String wordNetPropertiesFile) throws LexicalSemanticResourceException {
public WordNetResource() throws LexicalSemanticResourceException
{
this(null);
}

public WordNetResource(String wordNetPropertiesFile) throws LexicalSemanticResourceException
{
try {
InputStream is;
URL url = getClass().getResource("/"+wordNetPropertiesFile);
if (url != null) {
is = url.openStream();
}
else {
try {
url = new URL(wordNetPropertiesFile);
is = url.openStream();
}
catch (MalformedURLException e) {
// Ignore, we try if it is a file.
is = new FileInputStream(wordNetPropertiesFile);
}
}
this.dict = Dictionary.getInstance(is);
InputStream is;
if (wordNetPropertiesFile != null) {
try {
URL url = getClass().getResource("/" + wordNetPropertiesFile);
if (url != null) {
is = url.openStream();
}
else {
try {
url = new URL(wordNetPropertiesFile);
is = url.openStream();
}
catch (MalformedURLException e) {
// Ignore, we try if it is a file.
is = new FileInputStream(wordNetPropertiesFile);
}
}
this.dict = Dictionary.getInstance(is);
}
catch (IOException e) {
throw new LexicalSemanticResourceException(
"Could not access WordNet properties file: " + wordNetPropertiesFile,
e);
}
}
else {
dict = Dictionary.getDefaultResourceInstance();
}
this.v = dict.getVersion();
setIsCaseSensitive(isCaseSensitive); //zhu
} catch (IOException e) {
logger.info("Could not access WordNet properties file: " + wordNetPropertiesFile);
throw new LexicalSemanticResourceException(e);
} catch (JWNLException e) {
logger.info("JWNL exception while initializing reader.");
throw new LexicalSemanticResourceException(e);
}
catch (JWNLException e) {
throw new LexicalSemanticResourceException("JWNL exception while initializing reader.",
e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -47,18 +47,11 @@ public class WordNetResourceTest

private static LexicalSemanticResource wordnet;

@BeforeClass
public static void initializeWordNet()
{
try {
wordnet = new WordNetResource(
"src/main/resources/resource/WordNet_3/wordnet_properties.xml");
}
catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
@BeforeClass
public static void initializeWordNet() throws LexicalSemanticResourceException
{
wordnet = new WordNetResource();
}

@Test
public void testContainsLexeme() throws Exception
Expand Down Expand Up @@ -476,7 +469,8 @@ public void testHyponymMap()
throws Exception
{
wordnet.setIsCaseSensitive(false);
EntityGraph eg = EntityGraphManager.getEntityGraph(wordnet, EntityGraphType.JGraphT);
EntityGraph eg = EntityGraphManager.getEntityGraph(wordnet, EntityGraphType.JGraphT,
new File("target/test-output/getEntityGraph"));
eg.getIntrinsicInformationContent(wordnet.getEntity("tree").iterator().next());
}

Expand Down

0 comments on commit af85460

Please sign in to comment.