Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
humanities
gargantext
Commits
96420c1b
Commit
96420c1b
authored
Oct 28, 2014
by
Mathieu Rodic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[DOC] parsing.Caches is now fully documented.
parent
05170a8f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
10 deletions
+18
-10
Caches.py
parsing/Caches.py
+18
-10
No files found.
parsing/Caches.py
View file @
96420c1b
...
...
@@ -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
.
iso
3
language
=
key
.
iso
2
# 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
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment