dictionary_loader.py 901 B

123456789101112131415161718192021222324252627
  1. import io
  2. import jieba
  3. from supabase import create_client, Client
  4. from api.openai_scripts_chinese.config import SUPABASE_URL, SUPABASE_KEY
  5. supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
  6. def load_word_database_dictionary_from_supabase():
  7. table_name = "word_database"
  8. response = supabase.table(table_name).select("term, weight, type").execute()
  9. if response.data:
  10. dict_data = io.StringIO()
  11. for item in response.data:
  12. dict_data.write(f"{item['term']} {item['weight']} {item['type']}\n")
  13. dict_data.seek(0)
  14. jieba.load_userdict(dict_data)
  15. # print("Loaded dictionary from Supabase")
  16. return True
  17. else:
  18. print(f"No data found or an error occurred: {response.error}")
  19. print("Using default dictionary as Supabase data couldn't be fetched.")
  20. return False