# ******************************************************************************
# Copyright 2017-2018 Intel Corporation
#
# 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
#
# 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.
# ******************************************************************************
import logging
from os import listdir
from os.path import join, isfile
from nlp_architect.data.cdc_resources.data_types.wn.wordnet_page import WordnetPage
from nlp_architect.utils.io import load_json_file
logger = logging.getLogger(__name__)
[docs]class WordnetOffline(object):
def __init__(self, wordnet_dump):
if wordnet_dump:
self.dump = self.load_dump(wordnet_dump)
logger.info("Wikipedia dump loaded successfully!")
[docs] def get_pages(self, mention):
page = None
if mention.tokens_str is not None and mention.tokens_str in self.dump:
page = self.dump[mention.tokens_str]
return page
[docs] def load_dump(self, wn_dump):
onlyfiles = []
for _file in listdir(wn_dump):
file_path = join(wn_dump, _file)
if isfile(file_path):
onlyfiles.append(file_path)
json_dump_list = {}
for _file in onlyfiles:
json_dump_list.update(load_json_file(_file))
dump_final = {}
for key, value in json_dump_list.items():
dump_final[key] = self.extract_json_values(value)
return dump_final