Skip to content

Commit

Permalink
Added tag suggestions to Committee Meetings when editing tags
Browse files Browse the repository at this point in the history
  • Loading branch information
nirshub committed Jan 20, 2014
1 parent ec0289f commit 7c6d6eb
Show file tree
Hide file tree
Showing 7 changed files with 377 additions and 276 deletions.
74 changes: 74 additions & 0 deletions auxiliary/tag_suggestions/__init__.py
@@ -1,6 +1,12 @@
# -*- coding: utf -*-
# encoding:utf-8

from tagging.models import Tag, TaggedItem
from django.contrib.contenttypes.models import ContentType
from laws.models import Vote, Bill
from committees.models import CommitteeMeeting

import operator

def approve(admin, request, tag_suggestions):
for tag_suggestion in tag_suggestions:
Expand All @@ -13,3 +19,71 @@ def approve(admin, request, tag_suggestions):
tag=tag, object_id=obj.pk, content_type=ct)

tag_suggestion.delete()


def sum_add_two_dictionaries(dict, dict_to_add):
"""Takes two dictionaries, assuming their values are numeric, and sum each item that exist in both,
writing the merged dictionary to the first dictionary."""
#go over the dictionary to add
for key in dict_to_add:
if key in dict:
dict[key] += dict_to_add[key]
else:
dict[key] = dict_to_add[key]


# Extract only used tags, to avoid irrelevant tags
vote_tags = Tag.objects.usage_for_model(Vote)
bill_tags = Tag.objects.usage_for_model(Bill)
cm_tags = Tag.objects.usage_for_model(CommitteeMeeting)
all_tags = list(set(vote_tags).union(bill_tags).union(cm_tags))

#A list of tags that have been tagged over 10 times in the website
all_tags_names = [tag.name for tag in all_tags]

#A list of prefix charcters to use in tag extraction
prefixes = [u'ב', u'ו', u'ה', u'מ', u'מה', u'ל', u'']

def get_tags_in_text(text):
"""Returns a dictionary, the keys are tags found in text, and the values are the number of occurrences in text"""

result_dict = {}
words = text.split()

#look for tag in word
for tag in all_tags_names:
#create tag variations according to prefixes
tag_variations = [(p + tag) for p in prefixes]

#find number of occurences of tags for each word
occurence_count = 0
for word in words:
if word in tag_variations:
occurence_count += 1

#if tag found more than once, add them
if occurence_count > 0 :
result_dict[tag] = result_dict.get(tag, 0) + occurence_count

return result_dict


def extract_suggested_tags(current_tags, text_list):
'''Returns a sorted list consisting of key/value tuples where the keys are tags found in arguments' text,
and the values are the number of occurrences in arguments text.
current_tags are removed from final list.
The list is sorted from most occuring tags to least occuring tags'''

tags_occurrences = {}

#find occurences of tags in text
for text_to_extract in text_list:
sum_add_two_dictionaries(tags_occurrences, get_tags_in_text(text_to_extract))

#remove tags that are already tagged
for tag in current_tags:
if tag.name in tags_occurrences:
del tags_occurrences[tag.name]

#sort suggestions
return sorted(tags_occurrences.iteritems(), key=operator.itemgetter(1),reverse=True)
15 changes: 12 additions & 3 deletions auxiliary/tag_suggestions/tests.py
@@ -1,6 +1,6 @@
import auxiliary.tag_suggestions
from django.test import TestCase
from auxiliary.models import TagSuggestion
from auxiliary.tag_suggestions import approve
from django.contrib.auth.models import User
from laws.models import Bill, Law
from tagging.models import Tag
Expand All @@ -23,7 +23,7 @@ def test_approve(self):
suggested_by=self.user,
object=self.bill
)
approve(None, None, [tag_suggestion])
auxiliary.tag_suggestions.approve(None, None, [tag_suggestion])
tag = Tag.objects.get(name=tag_suggestion.name)
self.assertEqual(tag.name, tag_suggestion.name)
tagged_item = tag.items.all()[0]
Expand All @@ -38,7 +38,7 @@ def test_approve_existing_tag(self):
suggested_by=self.user,
object=self.bill
)
approve(None, None, [tag_suggestion])
auxiliary.tag_suggestions.approve(None, None, [tag_suggestion])

class TestForm(TestCase):

Expand All @@ -62,3 +62,12 @@ def test_form(self):
tag_suggestion = TagSuggestion.objects.get(name='test form tag')
self.assertEqual(tag_suggestion.object, self.committee_meeting)
self.assertEqual(tag_suggestion.suggested_by, self.user)

class TestSuggestions(TestCase):
def setUp(self):
auxiliary.tag_suggestions.all_tags_names = ['tag1']

def test_get_tags_in_text(self):
text = "tag1 ate the cat"
tags_count = auxiliary.tag_suggestions.get_tags_in_text(text)
self.assertEqual(tags_count['tag1'], 1)
4 changes: 4 additions & 0 deletions committees/views.py
Expand Up @@ -5,6 +5,7 @@
import difflib
import logging
import tagging
import auxiliary.tag_suggestions
from actstream import action
from django.conf import settings
from django.contrib import messages
Expand Down Expand Up @@ -133,6 +134,9 @@ def get_context_data(self, *args, **kwargs):
context['members'] = cm.committee.members_by_presence(ids=meeting_members_ids)
context['hide_member_presence'] = False

meeting_text = [cm.topics] + [part.body for part in cm.parts.all()]
context['tag_suggestions'] = auxiliary.tag_suggestions.extract_suggested_tags(cm.tags, meeting_text)

return context

@hashnav_method_decorator(login_required)
Expand Down
2 changes: 1 addition & 1 deletion static/css/common.css
Expand Up @@ -346,7 +346,7 @@ form > p > select { width:250px; }
}


#content-main .tag {
#content-main .tag-suggested .tag {
background-color:#C0C0C0;
border-bottom:1px solid #251C1F;
border-right:1px solid #251C1F;
Expand Down

0 comments on commit 7c6d6eb

Please sign in to comment.