Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
django-taggit
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7ffeae3e
authored
Apr 05, 2010
by
Frank Wiles
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Alex's style and change recommendations
parent
9ec89d70
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
15 additions
and
46 deletions
+15
-46
taggit/contrib/suggest/README.txt
+5
-4
taggit/contrib/suggest/admin.py
+1
-0
taggit/contrib/suggest/models.py
+2
-1
taggit/contrib/suggest/tests/__init__.py
+1
-1
taggit/contrib/suggest/tests/settings.py
+0
-0
taggit/contrib/suggest/tests/tests.py
+0
-19
taggit/contrib/suggest/utils.py
+6
-21
No files found.
taggit/contrib/suggest/README.txt
View file @
7ffeae3e
...
@@ -11,9 +11,10 @@ For example, if your site is a humor site you might want to collapse all of
...
@@ -11,9 +11,10 @@ For example, if your site is a humor site you might want to collapse all of
suggest_tags() function in taggit.contrib.suggest.utils will give you a list
suggest_tags() function in taggit.contrib.suggest.utils will give you a list
of tags that seem appropriate for the text content given to it.
of tags that seem appropriate for the text content given to it.
It will also do some basic stemming of the keywords for you! Which requires the
TODO
Python NLTK.
====
In a later version I hope to a simple way to help determine keywords for you
* Basic stemming of the keywords for you! Which will require the Python NLTK.
automatically, by learning from your past tags and content.
* In a later version I hope to a simple way to help determine keywords for you
automatically, by learning from your past tags and content.
taggit/contrib/suggest/admin.py
View file @
7ffeae3e
...
@@ -4,6 +4,7 @@ from taggit.models import Tag
...
@@ -4,6 +4,7 @@ from taggit.models import Tag
from
taggit.admin
import
TaggedItemInline
from
taggit.admin
import
TaggedItemInline
from
taggit.contrib.suggest.models
import
TagKeyword
,
TagRegExp
from
taggit.contrib.suggest.models
import
TagKeyword
,
TagRegExp
class
TagKeywordInline
(
admin
.
StackedInline
):
class
TagKeywordInline
(
admin
.
StackedInline
):
model
=
TagKeyword
model
=
TagKeyword
...
...
taggit/contrib/suggest/models.py
View file @
7ffeae3e
import
re
import
re
from
django.db
import
models
from
django.db
import
models
from
django.core.exceptions
import
ValidationError
from
django.core.exceptions
import
ValidationError
from
taggit.models
import
Tag
from
taggit.models
import
Tag
class
TagKeyword
(
models
.
Model
):
class
TagKeyword
(
models
.
Model
):
""" Model to associate simple keywords to a Tag """
""" Model to associate simple keywords to a Tag """
tag
=
models
.
ForeignKey
(
Tag
,
related_name
=
'keywords'
)
tag
=
models
.
ForeignKey
(
Tag
,
related_name
=
'keywords'
)
...
@@ -25,7 +27,6 @@ class TagRegExp(models.Model):
...
@@ -25,7 +27,6 @@ class TagRegExp(models.Model):
regexp
=
models
.
CharField
(
max_length
=
250
,
regexp
=
models
.
CharField
(
max_length
=
250
,
validators
=
[
validate_regexp
],
validators
=
[
validate_regexp
],
)
)
case_insensitive
=
models
.
BooleanField
(
default
=
False
)
def
__unicode__
(
self
):
def
__unicode__
(
self
):
return
self
.
name
return
self
.
name
...
...
taggit/contrib/suggest/tests/__init__.py
View file @
7ffeae3e
from
taggit.contrib.suggest.tests.tests
import
AddKeywordCase
,
AddRegexpCase
,
SuggestCase
from
taggit.contrib.suggest.tests.tests
import
SuggestCase
taggit/contrib/suggest/tests/settings.py
View file @
7ffeae3e
taggit/contrib/suggest/tests/tests.py
View file @
7ffeae3e
...
@@ -4,25 +4,6 @@ from taggit.models import Tag
...
@@ -4,25 +4,6 @@ from taggit.models import Tag
from
taggit.contrib.suggest.models
import
TagKeyword
,
TagRegExp
from
taggit.contrib.suggest.models
import
TagKeyword
,
TagRegExp
from
taggit.contrib.suggest.utils
import
suggest_tags
from
taggit.contrib.suggest.utils
import
suggest_tags
class
AddKeywordCase
(
TestCase
):
def
test_adding_keyword
(
self
):
new_tag
=
Tag
.
objects
.
create
(
name
=
'ku'
)
new_keyword
=
TagKeyword
.
objects
.
create
(
tag
=
new_tag
,
keyword
=
'kansas university'
)
self
.
assertTrue
(
new_keyword
)
self
.
assertTrue
(
new_keyword
.
tag
==
new_tag
)
class
AddRegexpCase
(
TestCase
):
def
test_adding_regexp
(
self
):
new_tag
=
Tag
.
objects
.
create
(
name
=
'ku'
)
new_regexp
=
TagRegExp
.
objects
.
create
(
tag
=
new_tag
,
name
=
'Find University of Kansas'
,
regexp
=
'University
\
s+of
\
s+Kansas'
)
self
.
assertTrue
(
new_regexp
)
self
.
assertTrue
(
new_regexp
.
tag
==
new_tag
)
class
SuggestCase
(
TestCase
):
class
SuggestCase
(
TestCase
):
def
test_simple_suggest
(
self
):
def
test_simple_suggest
(
self
):
ku_tag
=
Tag
.
objects
.
create
(
name
=
'ku'
)
ku_tag
=
Tag
.
objects
.
create
(
name
=
'ku'
)
...
...
taggit/contrib/suggest/utils.py
View file @
7ffeae3e
...
@@ -3,6 +3,7 @@ from taggit.models import Tag
...
@@ -3,6 +3,7 @@ from taggit.models import Tag
from
taggit.contrib.suggest.models
import
TagKeyword
,
TagRegExp
from
taggit.contrib.suggest.models
import
TagKeyword
,
TagRegExp
from
django.conf
import
settings
from
django.conf
import
settings
HAS_NLTK
=
True
HAS_NLTK
=
True
try
:
try
:
from
nltk.stemmer.porter
import
PorterStemmer
from
nltk.stemmer.porter
import
PorterStemmer
...
@@ -16,7 +17,7 @@ def _suggest_keywords(content=None):
...
@@ -16,7 +17,7 @@ def _suggest_keywords(content=None):
for
k
in
keywords
:
for
k
in
keywords
:
if
k
[
0
]
in
content
:
if
k
[
0
]
in
content
:
suggested_keywords
.
add
(
str
(
k
[
1
])
)
suggested_keywords
.
add
(
k
[
1
]
)
return
suggested_keywords
return
suggested_keywords
...
@@ -28,42 +29,26 @@ def _suggest_regexps(content=None):
...
@@ -28,42 +29,26 @@ def _suggest_regexps(content=None):
regexp_keywords
=
TagRegExp
.
objects
.
values_list
(
regexp_keywords
=
TagRegExp
.
objects
.
values_list
(
'regexp'
,
'regexp'
,
'tag'
,
'tag'
,
'case_insensitive'
)
)
for
r
in
regexp_keywords
:
for
r
in
regexp_keywords
:
try
:
regexps
.
add
((
re
.
compile
(
r
[
0
]),
r
[
1
]))
if
r
[
2
]:
reg
=
re
.
compile
(
r
[
0
],
re
.
IGNORE_CASE
)
else
:
reg
=
re
.
compile
(
r
[
0
])
except
:
# Skip any badly formed regular expressions silently
continue
regexps
.
add
((
reg
,
r
[
1
]))
# Look for our regular expressions in the content
# Look for our regular expressions in the content
for
r
in
regexps
:
for
r
in
regexps
:
if
r
[
0
]
.
search
(
content
):
if
r
[
0
]
.
search
(
content
):
suggested_regexps
.
add
(
str
(
r
[
1
])
)
suggested_regexps
.
add
(
r
[
1
]
)
return
suggested_regexps
return
suggested_regexps
def
suggest_tags
(
content
=
None
):
def
suggest_tags
(
content
=
None
):
""" Suggest tags based on text content """
""" Suggest tags based on text content """
if
not
content
:
return
MAX_LENGTH
=
getattr
(
settings
,
'TAGGIT_SUGGEST_MAX_LENGTH'
,
None
)
if
MAX_LENGTH
:
content
=
content
[
0
:
settings
.
TAGGIT_SUGGEST_MAX_LENGTH
]
suggested_keywords
=
_suggest_keywords
(
content
)
suggested_keywords
=
_suggest_keywords
(
content
)
suggested_regexps
=
_suggest_regexps
(
content
)
suggested_regexps
=
_suggest_regexps
(
content
)
suggested_tag_ids
=
suggested_keywords
|
suggested_regexps
suggested_tag_ids
=
suggested_keywords
|
suggested_regexps
# Turn the found IDs into tags
# Turn the found IDs into tags
where_string
=
'id IN (
%
s)'
%
','
.
join
(
suggested_tag_ids
)
where_string
=
'id IN (
%
s)'
%
','
.
join
(
[
str
(
x
)
for
x
in
suggested_tag_ids
]
)
tags
=
Tag
.
objects
.
extra
(
where
=
[
where_string
])
tags
=
Tag
.
objects
.
extra
(
where
=
[
where_string
])
return
tags
return
tags
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