Skip to content
Open
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
7 changes: 4 additions & 3 deletions transcript_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

import json
import re
from collections import Counter
from collections import Counter, defaultdict
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Iterable, Mapping
Expand Down Expand Up @@ -167,7 +167,7 @@ class TranscriptIndex:
def __init__(self) -> None:
"""Create an empty index."""
# token -> set of entry positions in self._entries
self._postings: dict[str, set[int]] = {}
self._postings: dict[str, set[int]] = defaultdict(set)
self._entries: list[_Entry] = []

def __len__(self) -> int:
Expand Down Expand Up @@ -203,7 +203,8 @@ def add(self, recording_id: str, segments: Iterable[Any]) -> int:
position = len(self._entries)
self._entries.append(entry)
for token in entry.counts:
self._postings.setdefault(token, set()).add(position)
# 성능 최적화: dict.setdefault 대신 defaultdict를 사용하여 불필요한 set() 객체 생성을 방지합니다.
self._postings[token].add(position)
added += 1
return added

Expand Down
Loading