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
17c17579
Commit
17c17579
authored
Nov 15, 2014
by
Mathieu Rodic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[FEATURE] Added a RESTful URL : /api/corpus/95/ngrams?startswith=b&order=
parent
f8e11a91
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
59 additions
and
1 deletion
+59
-1
api.py
gargantext_web/api.py
+49
-0
urls.py
gargantext_web/urls.py
+5
-0
requirements.txt
init/requirements.txt
+1
-1
models.py
node/models.py
+4
-0
No files found.
gargantext_web/api.py
0 → 100644
View file @
17c17579
from
django.http
import
HttpResponseNotFound
,
HttpResponse
,
HttpResponseBadRequest
from
django.db.models
import
Avg
,
Max
,
Min
,
Count
from
node.models
import
NodeType
,
Node
,
Ngram
# from node.models import Language, ResourceType, Resource
# from node.models import Node, NodeType, Node_Resource, Project, Corpus
# from node.admin import CorpusForm, ProjectForm, ResourceForm
import
json
def
JsonHttpResponse
(
data
,
status
=
200
):
return
HttpResponse
(
content
=
json
.
dumps
(
data
,
indent
=
4
),
content_type
=
"application/json"
,
status
=
status
)
_ngrams_order_columns
=
{
"frequency"
:
"-count"
,
"alphabetical"
:
"terms"
}
def
ngrams
(
request
,
corpus_id
):
# parameters retrieval and control
corpusQuery
=
Node
.
objects
.
filter
(
id
=
corpus_id
)
if
not
corpusQuery
:
return
HttpResponseNotFound
(
"No such corpus."
)
corpus
=
corpusQuery
.
first
()
if
corpus
.
type
.
name
!=
'Corpus'
:
return
HttpResponseNotFound
(
"No such corpus."
)
order
=
request
.
GET
.
get
(
'order'
,
'frequency'
)
if
order
not
in
_ngrams_order_columns
:
return
HttpResponseBadRequest
(
'The order parameter should take one of the following values: '
+
', '
.
join
(
_ngrams_order_columns
))
order_column
=
_ngrams_order_columns
[
order
]
# query building
ngramsQuery
=
Ngram
.
objects
.
filter
(
nodes__parent
=
corpus
,
terms__startswith
=
request
.
GET
.
get
(
'startswith'
,
''
)
)
.
annotate
(
count
=
Count
(
'id'
))
# how should we order this?
orderColumn
=
{
"frequency"
:
"-count"
,
"alphabetical"
:
"terms"
}
.
get
(
request
.
GET
.
get
(
'order'
,
'frequency'
),
'-count'
)
ngramsQuery
=
ngramsQuery
.
order_by
(
orderColumn
)
# response building
return
JsonHttpResponse
({
"list"
:
[
ngram
.
terms
for
ngram
in
ngramsQuery
],
})
\ No newline at end of file
gargantext_web/urls.py
View file @
17c17579
...
...
@@ -8,6 +8,9 @@ from gargantext_web.views import delete_project, delete_corpus
from
gargantext_web.views
import
exploration
,
send_csv
,
send_graph
from
gargantext_web.views
import
explorer_graph
,
explorer_matrix
,
explorer_chart
import
gargantext_web.api
admin
.
autodiscover
()
urlpatterns
=
patterns
(
''
,
...
...
@@ -36,6 +39,8 @@ urlpatterns = patterns('',
url
(
r'^chart/corpus/(\d+)/data.csv$'
,
send_csv
),
url
(
r'^graph.json$'
,
send_graph
),
url
(
r'^api/corpus/(\d+)/ngrams$'
,
gargantext_web
.
api
.
ngrams
),
)
from
django.conf
import
settings
...
...
init/requirements.txt
View file @
17c17579
...
...
@@ -28,7 +28,7 @@ kombu==3.0.23
lxml==3.3.6
matplotlib==1.4.0
networkx==1.9
nltk==3.0a4
#
nltk==3.0a4
nose==1.3.4
numpy==1.8.2
pandas==0.14.1
...
...
node/models.py
View file @
17c17579
...
...
@@ -44,6 +44,7 @@ class Ngram(models.Model):
language
=
models
.
ForeignKey
(
Language
,
blank
=
True
,
null
=
True
,
on_delete
=
models
.
SET_NULL
)
n
=
models
.
IntegerField
()
terms
=
models
.
CharField
(
max_length
=
255
)
nodes
=
models
.
ManyToManyField
(
through
=
'Node_Ngram'
,
to
=
'Node'
)
class
Resource
(
models
.
Model
):
user
=
models
.
ForeignKey
(
User
)
...
...
@@ -89,6 +90,8 @@ class Node(CTENode):
date
=
models
.
DateField
(
default
=
timezone
.
now
,
blank
=
True
)
metadata
=
hstore
.
DictionaryField
(
blank
=
True
)
ngrams
=
models
.
ManyToManyField
(
through
=
'Node_Ngram'
,
to
=
'Ngram'
)
def
__str__
(
self
):
return
self
.
name
...
...
@@ -203,6 +206,7 @@ class Node_Ngram(models.Model):
return
"
%
s:
%
s"
%
(
self
.
node
.
name
,
self
.
ngram
.
terms
)
class
Project
(
Node
):
class
Meta
:
proxy
=
True
...
...
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