Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions openlibrary/core/lists/engine.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Utility functions for processing lists."""

import collections
import re
from collections import Counter, defaultdict
Comment on lines 3 to +4


def reduce_seeds(values):
Expand Down Expand Up @@ -65,7 +65,7 @@ class SubjectProcessor:
"""Processor to take a dict of subjects, places, people and times and build a list of ranked subjects."""

def __init__(self):
self.subjects = collections.defaultdict(list)
self.subjects = defaultdict(list)

def add_subjects(self, subjects):
for s in subjects.get("subjects", []):
Expand All @@ -90,11 +90,10 @@ def _get_subject(self, prefix, subject_name):
return {"key": key, "name": subject_name}

def _most_used(self, seq):
d = collections.defaultdict(lambda: 0)
for x in seq:
d[x] += 1

return sorted(d, key=lambda k: d[k], reverse=True)[0]
"""Returns the most frequent element in a sequence using collections.Counter."""
if not seq:
return None
return Counter(seq).most_common(1)[0][0]
Comment on lines +93 to +96

def top_subjects(self, limit=100):
subjects = [
Expand Down