Skip to content

Commit

Permalink
Reformatted to follow wordnet format used locally
Browse files Browse the repository at this point in the history
  • Loading branch information
fbanados committed Sep 3, 2024
1 parent 60dc6e1 commit 93c3683
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 27 deletions.
Binary file added wordnet/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added wordnet/__pycache__/nltk.cpython-310.pyc
Binary file not shown.
Binary file added wordnet/__pycache__/settings.cpython-310.pyc
Binary file not shown.
Binary file added wordnet/__pycache__/urls.cpython-310.pyc
Binary file not shown.
Binary file added wordnet/__pycache__/views.cpython-310.pyc
Binary file not shown.
Binary file added wordnet/__pycache__/wsgi.cpython-310.pyc
Binary file not shown.
31 changes: 23 additions & 8 deletions wordnet/nltk.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
from nltk.corpus import wordnet as wn
from nltk.corpus.reader.wordnet import Synset
import re

format_regexp = r'^\W*\((?P<pos>\w+)\)\W+(?P<stem>\w+)\W*\#\W*(?P<num>\d+)\W*\Z'

def candidates(keyword):
if not keyword:
return []
result = wn.synsets(keyword)
real_keyword = normalize_keyword(keyword)
result = wn.synsets(real_keyword)
if len(result) == 0 :
result = [wn.synset(keyword)]
try:
result = [wn.synset(real_keyword)]
except ValueError:
result = []
return result

def name_format(name):
return name
def name_format(synset : Synset):
data = synset.name().split(".")
return f"({data[1]}) {data[0]}#{int(data[2])}"

def normalize_keyword(keyword: str) -> str:
matches = re.match(format_regexp, keyword)
if matches:
return matches['stem']+'.'+matches['pos']+'.'+matches['num']
return keyword

def normalize_result(result):
return {
"name" : name_format(result.name),
"name" : name_format(result),
"pos" : result.pos,
"definition" : result.definition,
"lemmas" : [name_format(lemma.name) for lemma in result.lemmas()],
"hyponyms" : [name_format(r.name) for r in result.hyponyms()],
"hypernyms" : [name_format(r.name) for r in result.hypernyms()]
"lemmas" : [lemma.name for lemma in result.lemmas()],
"hyponyms" : [name_format(r) for r in result.hyponyms()],
"hypernyms" : [name_format(r) for r in result.hypernyms()]
}

def search(keyword):
Expand Down
2 changes: 1 addition & 1 deletion wordnet/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = [ "localhost" , "vm-dev"]
ALLOWED_HOSTS = [ "localhost" , "vm-dev", "127.0.0.1" ]


# Application definition
Expand Down
27 changes: 9 additions & 18 deletions wordnet/templates/wordnet/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,19 @@
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</head>
<body>
<div>
<nav class="navbar navbar-expand-lg bg-body-tertiary">
<div class="container-fluid">
<nav class="navbar navbar-expand bg-body-tertiary justify-content-between ">
<a class="navbar-brand" href="#">Altlab Wordnet</a>
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
</ul>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search Wordnet" name="search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
<form class="form-inline my-2 my-lg-0 d-flex flex-row align-items-center" role="search">
<input class="form-control mr-sm-2" type="search" placeholder="Search Wordnet" name="search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>

</div>
</nav>
</div>
<div class="container" id="contents">
</nav>
<div class="container" id="contents">
{% if search %}
<h1>{{search}}</h1>
<table class="table">
<table class="table table-striped">
<thead> <tr>
<th scope="col">Name</th>
<th scope="col">POS</th>
<th scope="col">Definition</th>
<th scope="col">Lemmas</th>
<th scope="col">Hyponyms</th>
Expand All @@ -35,7 +27,6 @@ <h1>{{search}}</h1>
{% for result in results %}
<tr>
<td>{{result.name}}</td>
<td>{{result.pos}}</td>
<td>{{result.definition}}</td>
<td>
{% for lemma in result.lemmas %}
Expand All @@ -46,14 +37,14 @@ <h1>{{search}}</h1>
</td>
<td>
{% for hyponym in result.hyponyms %}
<a href="{{main_url}}?search={{hyponym}}">
<a href="{{main_url}}?search={{hyponym|urlencode}}">
{{hyponym}}
</a><br/>
{% endfor %}

</td>
<td>{% for hypernym in result.hypernyms %}
<a href="{{main_url}}?search={{hypernym}}">
<a href="{{main_url}}?search={{hypernym|urlencode}}">
{{hypernym}}
</a><br/>
{% endfor %}</td>
Expand Down

0 comments on commit 93c3683

Please sign in to comment.