Commit 96420c1b authored by Mathieu Rodic's avatar Mathieu Rodic

[DOC] parsing.Caches is now fully documented.

parent 05170a8f
......@@ -6,13 +6,16 @@ from parsing.NgramsExtractors import EnglishNgramsExtractor, FrenchNgramsExtract
class NgramsCache(collections.defaultdict):
"""This allows the fast retrieval of ngram ids
from a cache instead of calling the database every time
"""
from a cache instead of calling the database every time.
This class is language-specific."""
def __init__(self, language):
"""The cache only works with one language,
which is the required parameter of the constructor."""
self.language = language
def __missing__(self, terms):
"""If the terms are not yet present in the dictionary,
retrieve it from the database or insert it."""
try:
ngram = Ngram.get(terms=terms, language=self.language)
except:
......@@ -22,22 +25,25 @@ class NgramsCache(collections.defaultdict):
return self[terms]
class NgramsCaches(collections.defaultdict):
"""This allows the fast retrieval of ngram ids
from a cache instead of calling the database every time."""
def __missing__(self, language):
"""If the cache for this language is not reachable,
add id to the dictionary."""
self[language] = NgramsCache(language)
return self[language]
class NgramsExtractorsCache(collections.defaultdict):
"""This allows the fast retrieval of ngram ids
from a cache instead of calling the database every time
"""
from a cache instead of calling the database every time."""
def __missing__(self, key):
"""If the ngrams extractor is not instancianted yet
for the given language, do it!"""
# format the language
if isinstance(key, str):
language = key.strip().lower()
else:
language = key.iso3
language = key.iso2
# find the proper extractor
if language in ["en", "eng", "english"]:
Extractor = EnglishNgramsExtractor
......@@ -45,13 +51,14 @@ class NgramsExtractorsCache(collections.defaultdict):
Extractor = FrenchNgramsExtractor
else:
Extractor = NgramsExtractor
# try to see if already instanciated, otherwise do it
# try to see if already instanciated with another key
found = False
for extractor in self.values():
if type(extractor) == Extractor:
self[key] = extractor
found = True
break
# well if not, let's instanciate it...
if not found:
self[key] = Extractor()
# return the proper extractor
......@@ -60,7 +67,8 @@ class NgramsExtractorsCache(collections.defaultdict):
class Cache:
"""This is THE cache of the caches."""
"""This is THE cache of the caches.
See NgramsCaches and NgramsExtractorsCache for better understanding."""
def __init__(self):
self.ngrams = NgramsCaches()
self.extractors = NgramsExtractorsCache()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment