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
657e4703
Commit
657e4703
authored
Oct 19, 2015
by
delanoe
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[FIX] Add tools for ngrams.
parent
2f5136ce
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
113 additions
and
0 deletions
+113
-0
tools.py
ngram/tools.py
+113
-0
No files found.
ngram/tools.py
0 → 100644
View file @
657e4703
from
gargantext_web.db
import
Ngram
,
NodeNgramNgram
from
parsing.corpustools
import
get_cursor
,
bulk_insert
def
insert_ngrams
(
ngrams
,
get
=
'terms-id'
):
'''
insert_ngrams :: [String, Int] -> dict[terms] = id
'''
db
,
cursor
=
get_cursor
()
cursor
.
execute
(
'''
CREATE TEMPORARY TABLE tmp__ngram (
id INT,
terms VARCHAR(255) NOT NULL,
n INT
);
'''
)
bulk_insert
(
'tmp__ngram'
,
[
'terms'
,
'n'
],
ngrams
,
cursor
=
cursor
)
cursor
.
execute
(
'''
UPDATE
tmp__ngram
SET
id = ngram.id
FROM
%
s AS ngram
WHERE
tmp__ngram.terms = ngram.terms
AND
tmp__ngram.n = ngram.n
'''
%
(
Ngram
.
__table__
.
name
,))
cursor
.
execute
(
'''
INSERT INTO
%
s (terms, n)
SELECT
terms, n
FROM
tmp__ngram
WHERE
id IS NULL
'''
%
(
Ngram
.
__table__
.
name
,))
cursor
.
execute
(
'''
UPDATE
tmp__ngram
SET
id = ngram.id
FROM
%
s AS ngram
WHERE
ngram.terms = tmp__ngram.terms
AND
ngram.n = tmp__ngram.n
AND
tmp__ngram.id IS NULL
'''
%
(
Ngram
.
__table__
.
name
,))
ngram_ids
=
dict
()
cursor
.
execute
(
'SELECT id, terms FROM tmp__ngram'
)
for
row
in
cursor
.
fetchall
():
ngram_ids
[
row
[
1
]]
=
row
[
0
]
db
.
commit
()
return
(
ngram_ids
)
def
insert_nodengramngram
(
nodengramngram
):
db
,
cursor
=
get_cursor
()
cursor
.
execute
(
'''
CREATE TEMPORARY TABLE tmp__nnn (
id INT,
node_id INT,
ngramx_id INT,
ngramy_id INT
);
'''
)
bulk_insert
(
'tmp__nnn'
,
[
'node_id'
,
'ngramx_id'
,
'ngramy_id'
],
nodengramngram
,
cursor
=
cursor
)
# nnn = NodeNgramNgram
cursor
.
execute
(
'''
UPDATE
tmp__nnn
SET
id = nnn.id
FROM
%
s AS nnn
WHERE
tmp__nnn.node_id = nnn.node_id
AND
tmp__nnn.ngramx_id = nnn.ngramx_id
AND
tmp__nnn.ngramy_id = nnn.ngramy_id
'''
%
(
NodeNgramNgram
.
__table__
.
name
,))
cursor
.
execute
(
'''
INSERT INTO
%
s (node_id, ngramx_id, ngramy_id, score)
SELECT
node_id, ngramx_id, ngramy_id, 1
FROM
tmp__nnn
WHERE
id is NULL
'''
%
(
NodeNgramNgram
.
__table__
.
name
,))
db
.
commit
()
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