| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 class Occurrence(list): | 5 class Occurrence(list): |
| 6 """A list of indices where something occurs in a list. | 6 """A list of indices where something occurs in a list. |
| 7 | 7 |
| 8 The list of indices can be accessed directly, since this class is a | 8 The list of indices can be accessed directly, since this class is a |
| 9 subclass of ``list``. In addition to this list, we also have a ``name` | 9 subclass of ``list``. In addition to this list, we also have a ``name` |
| 10 property which specifies what thing is occurring in those positions. For | 10 property which specifies what thing is occurring in those positions. For |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 they're actually allowed to be anything which can serve as a key | 33 they're actually allowed to be anything which can serve as a key |
| 34 in a dict. | 34 in a dict. |
| 35 | 35 |
| 36 Returns: | 36 Returns: |
| 37 A dict mapping each "name" in ``names`` to an Occurrence object, | 37 A dict mapping each "name" in ``names`` to an Occurrence object, |
| 38 each of which contains a list of the indices where that name occurs | 38 each of which contains a list of the indices where that name occurs |
| 39 in ``names``. | 39 in ``names``. |
| 40 """ | 40 """ |
| 41 occurrences = {} | 41 occurrences = {} |
| 42 for index, name in enumerate(names or []): | 42 for index, name in enumerate(names or []): |
| 43 if not name: |
| 44 continue |
| 45 |
| 43 if name not in occurrences: | 46 if name not in occurrences: |
| 44 occurrences[name] = Occurrence(name, [index]) | 47 occurrences[name] = Occurrence(name, [index]) |
| 45 else: | 48 else: |
| 46 occurrences[name].append(index) | 49 occurrences[name].append(index) |
| 47 | 50 |
| 48 return occurrences | 51 return occurrences |
| 49 | 52 |
| 50 | 53 |
| 51 def DefaultOccurrenceRanking(occurrence): | 54 def DefaultOccurrenceRanking(occurrence): |
| 52 """Default function for ranking an occurrence. | 55 """Default function for ranking an occurrence. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 rank_function (callable): what rank value to give an occurrence. If | 96 rank_function (callable): what rank value to give an occurrence. If |
| 94 you don't supply this argument, or if you provide a falsy value, | 97 you don't supply this argument, or if you provide a falsy value, |
| 95 then we will fall back to using the ``DefaultOccurrenceRanking``. | 98 then we will fall back to using the ``DefaultOccurrenceRanking``. |
| 96 | 99 |
| 97 Returns: | 100 Returns: |
| 98 A length-``top_n`` list of "names" ordered by the ``rank_function``. | 101 A length-``top_n`` list of "names" ordered by the ``rank_function``. |
| 99 """ | 102 """ |
| 100 if not rank_function: # pragma: no cover. | 103 if not rank_function: # pragma: no cover. |
| 101 rank_function = DefaultOccurrenceRanking | 104 rank_function = DefaultOccurrenceRanking |
| 102 | 105 |
| 103 # TODO(wrengr): can't we do this sorting/filtering/truncation in one pass? | 106 # TODO(wrengr): generalize the filter function into another parameter. |
| 104 occurrences = sorted(GetOccurrences(names).values(), key=rank_function) | 107 occurrences = sorted(GetOccurrences(names).values(), key=rank_function) |
| 105 | 108 |
| 106 # Filter out unnamed classes. Alas, we can't sort these out before | 109 return [occurrence.name for occurrence in occurrences[:top_n]] |
| 107 # constructing the concordance, because then our indices would be off. | |
| 108 # TODO(wrengr): generalize the filter function into another parameter. | |
| 109 classes = [occurrence.name for occurrence in occurrences if occurrence.name] | |
| 110 | |
| 111 return classes[:top_n] | |
| OLD | NEW |