| | import json |
| | from collections import defaultdict |
| |
|
| | class Founder: |
| | def __init__(self, filepath='datas/founder.jsonl'): |
| | self.filepath = filepath |
| | self.datas = {} |
| | self.founder2items = defaultdict(list) |
| |
|
| | try: |
| | self.load_founder() |
| | except FileNotFoundError: |
| | self.datas = {} |
| |
|
| | |
| | for word, founder in self.datas.items(): |
| | self.founder2items[founder].append(word) |
| |
|
| | def load_founder(self): |
| | """Load founder data from a jsonl file.""" |
| | with open(self.filepath, 'r', encoding='utf-8') as file: |
| | for line in file: |
| | data = json.loads(line.strip()) |
| | self.datas.update(data) |
| |
|
| | def save_founder(self): |
| | """Save founder data to a jsonl file.""" |
| | with open(self.filepath, 'w', encoding='utf-8') as file: |
| | for word, founder in self.datas.items(): |
| | file.write(json.dumps({word: founder}, ensure_ascii=False) + '\n') |
| |
|
| | def get_founder(self, word): |
| | """Get the founder of a given word.""" |
| | return self.datas.get(word, None) |
| |
|
| | def set_founder(self, word, founder, enforce=False): |
| | """Set the founder of a word if it's not already set or if enforce is True.""" |
| | if word in self.datas and not enforce: |
| | print(f"Warning: {word} already has a founder: {self.datas[word]}. Use enforce=True to override.") |
| | else: |
| | self.datas[word] = founder |
| | self.founder2items[founder].append(word) |
| | self.save_founder() |
| |
|
| | def get_all_items_from_founder(self, founder): |
| | """Get all words discovered by a specific founder.""" |
| | return self.founder2items.get(founder, []) |
| |
|
| | def get_top_rank(self, top_k=20): |
| | """Get the top_k founders with the most discovered words.""" |
| | sorted_founders = sorted(self.founder2items.items(), key=lambda x: len(x[1]), reverse=True) |
| | return sorted_founders[:top_k] |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | if __name__ == '__main__': |
| | founder = Founder() |
| | founder.set_founder('test_apple', '鲁鲁道祖') |
| | founder.set_founder('test_banana', '鲁鲁道祖') |
| | founder.set_founder('test_orange', "文钊道祖") |
| | print(founder.get_founder('test_apple')) |
| | print(founder.get_all_items_from_founder('Alice')) |
| | print(founder.get_top_rank()) |