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 ...@@ -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.
...@@ -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
......
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
......
from taggit.contrib.suggest.tests.tests import AddKeywordCase, AddRegexpCase, SuggestCase from taggit.contrib.suggest.tests.tests import SuggestCase
...@@ -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')
......
...@@ -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
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