Commit 7ffeae3e by Frank Wiles

Alex's style and change recommendations

parent 9ec89d70
......@@ -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
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
Python NLTK.
TODO
====
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.
* Basic stemming of the keywords for you! Which will require the Python NLTK.
* 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.
......@@ -4,6 +4,7 @@ from taggit.models import Tag
from taggit.admin import TaggedItemInline
from taggit.contrib.suggest.models import TagKeyword, TagRegExp
class TagKeywordInline(admin.StackedInline):
model = TagKeyword
......@@ -12,10 +13,10 @@ class TagRegExpInline(admin.StackedInline):
class TagSuggestAdmin(admin.ModelAdmin):
inlines = [
TaggedItemInline,
TagKeywordInline,
TagRegExpInline,
]
TaggedItemInline,
TagKeywordInline,
TagRegExpInline,
]
admin.site.unregister(Tag)
admin.site.register(Tag, TagSuggestAdmin)
import re
from django.db import models
from django.core.exceptions import ValidationError
from taggit.models import Tag
class TagKeyword(models.Model):
""" Model to associate simple keywords to a Tag """
tag = models.ForeignKey(Tag, related_name='keywords')
......@@ -25,7 +27,6 @@ class TagRegExp(models.Model):
regexp = models.CharField(max_length=250,
validators=[validate_regexp],
)
case_insensitive = models.BooleanField(default=False)
def __unicode__(self):
return self.name
......
from taggit.contrib.suggest.tests.tests import AddKeywordCase, AddRegexpCase, SuggestCase
from taggit.contrib.suggest.tests.tests import SuggestCase
DATABASE_ENGINE = 'sqlite3'
INSTALLED_APPS = [
'django.contrib.contenttypes',
'taggit',
'taggit.contrib.suggest',
'django.contrib.contenttypes',
'taggit',
'taggit.contrib.suggest',
]
......@@ -4,25 +4,6 @@ from taggit.models import Tag
from taggit.contrib.suggest.models import TagKeyword, TagRegExp
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):
def test_simple_suggest(self):
ku_tag = Tag.objects.create(name='ku')
......
......@@ -3,6 +3,7 @@ from taggit.models import Tag
from taggit.contrib.suggest.models import TagKeyword, TagRegExp
from django.conf import settings
HAS_NLTK = True
try:
from nltk.stemmer.porter import PorterStemmer
......@@ -16,7 +17,7 @@ def _suggest_keywords(content=None):
for k in keywords:
if k[0] in content:
suggested_keywords.add(str(k[1]))
suggested_keywords.add(k[1])
return suggested_keywords
......@@ -28,42 +29,26 @@ def _suggest_regexps(content=None):
regexp_keywords = TagRegExp.objects.values_list(
'regexp',
'tag',
'case_insensitive')
)
for r in regexp_keywords:
try:
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]))
regexps.add((re.compile(r[0]), r[1]))
# Look for our regular expressions in the content
for r in regexps:
if r[0].search(content):
suggested_regexps.add(str(r[1]))
suggested_regexps.add(r[1])
return suggested_regexps
def suggest_tags(content=None):
""" 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_regexps = _suggest_regexps(content)
suggested_tag_ids = suggested_keywords | suggested_regexps
# 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])
return tags
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 sign in to comment