Browse Source

- Fix the get_game_id_from_nanme function to do full matches first.

Fix Word Ninja
feature-new-db-implementation
Quildra 1 year ago
parent
commit
1705e0b044
  1. 18
      database/db_controller.py
  2. 6
      ui/main_window_controller.py
  3. 62
      ui/main_window_view.py
  4. 175
      utility/data.py
  5. 9
      utility/pokemon_word_ninja.py

18
database/db_controller.py

@ -418,13 +418,25 @@ class DBController:
return [row['PFIC'] for row in results] return [row['PFIC'] for row in results]
def get_game_id_by_name(self, name): def get_game_id_by_name(self, name):
# First try: exact match against the `name` column
self.cursor.execute(''' self.cursor.execute('''
SELECT id, name, generation FROM games SELECT id, name, generation FROM games
WHERE name LIKE ? OR alt_names LIKE ? WHERE name = ?
''', (f"%{name}%", f"%{name}%")) ''', (name,))
# Fetch and print the results # Fetch the result
result = self.cursor.fetchone() result = self.cursor.fetchone()
# If no exact match found, try matching using `LIKE` with both `name` and `alt_names`
if not result:
self.cursor.execute('''
SELECT id, name, generation FROM games
WHERE name LIKE ? OR alt_names LIKE ?
''', (f"%{name}%", f"%{name}%"))
# Fetch the result from the second query
result = self.cursor.fetchone()
print(f"ID: {result[0]}, Name: {result[1]}, Generation: {result[2]}") print(f"ID: {result[0]}, Name: {result[1]}, Generation: {result[2]}")
return dict(result) return dict(result)

6
ui/main_window_controller.py

@ -195,9 +195,13 @@ class MainWindowController:
self.view.image_label.setText("Image not found") self.view.image_label.setText("Image not found")
self.load_evolution_chain(pfic) self.load_evolution_chain(pfic)
#self.load_encounter_locations(pfic) self.load_encounter_locations(pfic)
self.current_pfic = pfic self.current_pfic = pfic
def load_evolution_chain(self, pfic): def load_evolution_chain(self, pfic):
chain = db.get_full_evolution_paths(pfic) chain = db.get_full_evolution_paths(pfic)
self.view.update_evolution_tree(chain, pfic) self.view.update_evolution_tree(chain, pfic)
def load_encounter_locations(self, pfic):
encounters = db.get_encounters(pfic)
self.view.update_encounter_list(encounters, pfic)

62
ui/main_window_view.py

@ -8,6 +8,7 @@ from .main_window_controller import MainWindowController
from utility.functions import get_display_name from utility.functions import get_display_name
from db import db from db import db
import json
class PokemonUI(QWidget): class PokemonUI(QWidget):
def __init__(self, parent=None): def __init__(self, parent=None):
@ -294,3 +295,64 @@ class PokemonUI(QWidget):
current_item = tree_items[selected_pfic] current_item = tree_items[selected_pfic]
self.evolution_tree.scrollToItem(current_item) self.evolution_tree.scrollToItem(current_item)
self.evolution_tree.setCurrentItem(current_item) self.evolution_tree.setCurrentItem(current_item)
def update_encounter_list(self, encounters, pfic):
self.locations_tree.clear()
game_items = {}
for encounter in encounters:
pfic = encounter["PFIC"]
game_id = encounter["game_id"]
type = encounter["type"]
if type == "event":
continue
if type == "evolve":
continue
data = json.loads(encounter["data"])
game = db.get_game_by_id(game_id)
game_name = game["name"]
location = data["location"]
if game_name not in game_items:
#print(f'finding generation for {game}')
game_item = QTreeWidgetItem([game_name])
game_items[game_name] = game_item
# Use generation for sorting, default to 0 if not found
game_item.setData(0, Qt.ItemDataRole.UserRole, game["generation"])
#print(f'generation for {game} is {generation}')
location_item = QTreeWidgetItem([location])
details = []
if "day" in data:
details.append(f"Day: {data["day"]}")
if "time" in data:
details.append(f"Time: {data["time"]}")
if "dual_slot" in data:
details.append(f"Dual Slot: {data["dual_slot"]}")
if "static_encounter" in data:
details.append(f"Static Encounter (Count: {data["static_encounter_count"]})")
if "extra_text" in data:
details.append(f"Extra: {data["extra_text"]}")
if "stars" in data:
details.append(f"Stars: {data["stars"]}")
if "fishing" in data:
details.append(f"Fishing")
if "rods" in data:
details.append(f"Rods: {data["rods"]}")
location_item.setText(1, ", ".join(details))
game_items[game_name].addChild(location_item)
# Sort game items by generation and add them to the tree
sorted_game_items = sorted(game_items.values(), key=lambda x: x.data(0, Qt.ItemDataRole.UserRole))
self.locations_tree.addTopLevelItems(sorted_game_items)
self.locations_tree.expandAll()
# Update the cache for this Pokémon
#self.encounter_cache[pfic] = len(encounters) > 0
# After updating the locations tree
#self.update_pokemon_list_highlights()

175
utility/data.py

@ -15,261 +15,298 @@ regional_descriptors = ["Kantonian", "Johtonian", "Hoennian", "Sinnohan", "Unova
yellow = { yellow = {
"Name": "Yellow", "Name": "Yellow",
"AltNames": ["Pokemon Yellow", "Pokémon Yellow", "Y"], "AltNames": ["Pokemon Yellow", "Pokémon Yellow"],
"Generation": 1, "Generation": 1,
"Mark": "Game Boy" "Mark": "Game Boy",
"Abvr": "Y"
} }
red = { red = {
"Name": "Red", "Name": "Red",
"AltNames": ["Pokemon Red", "Pokémon Red", "R"], "AltNames": ["Pokemon Red", "Pokémon Red"],
"Generation": 1, "Generation": 1,
"Mark": "Game Boy" "Mark": "Game Boy",
"Abvr": "R"
} }
blue = { blue = {
"Name": "Blue", "Name": "Blue",
"AltNames": ["Pokemon Blue", "Pokémon Blue", "B"], "AltNames": ["Pokemon Blue", "Pokémon Blue"],
"Generation": 1, "Generation": 1,
"Mark": "Game Boy" "Mark": "Game Boy",
"Abvr": "B"
} }
crystal = { crystal = {
"Name": "Crystal", "Name": "Crystal",
"AltNames": ["Pokemon Crystal", "Pokémon Crystal", "C"], "AltNames": ["Pokemon Crystal", "Pokémon Crystal"],
"Generation": 2, "Generation": 2,
"Mark": "Game Boy" "Mark": "Game Boy",
"Abvr": "C"
} }
gold = { gold = {
"Name": "Gold", "Name": "Gold",
"AltNames": ["Pokemon Gold", "Pokémon Gold", "G"], "AltNames": ["Pokemon Gold", "Pokémon Gold"],
"Generation": 2, "Generation": 2,
"Mark": "Game Boy" "Mark": "Game Boy",
"Abvr": "G"
} }
silver = { silver = {
"Name": "Silver", "Name": "Silver",
"AltNames": ["Pokemon Silver", "Pokémon Silver", "S"], "AltNames": ["Pokemon Silver", "Pokémon Silver"],
"Generation": 2, "Generation": 2,
"Mark": "Game Boy" "Mark": "Game Boy",
"Abvr": "S"
} }
emerald = { emerald = {
"Name": "Emerald", "Name": "Emerald",
"AltNames": ["Pokemon Emerald", "Pokémon Emerald", "E"], "AltNames": ["Pokemon Emerald", "Pokémon Emerald"],
"Generation": 3, "Generation": 3,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "E"
} }
fire_red = { fire_red = {
"Name": "FireRed", "Name": "FireRed",
"AltNames": ["Pokemon FireRed", "Pokémon FireRed", "FR"], "AltNames": ["Pokemon FireRed", "Pokémon FireRed"],
"Generation": 3, "Generation": 3,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "FR"
} }
leaf_green = { leaf_green = {
"Name": "LeafGreen", "Name": "LeafGreen",
"AltNames": ["Pokemon LeafGreen", "Pokémon LeafGreen", "LG"], "AltNames": ["Pokemon LeafGreen", "Pokémon LeafGreen"],
"Generation": 3, "Generation": 3,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "LG"
} }
ruby = { ruby = {
"Name": "Ruby", "Name": "Ruby",
"AltNames": ["Pokemon Ruby", "Pokémon Ruby", "R"], "AltNames": ["Pokemon Ruby", "Pokémon Ruby"],
"Generation": 3, "Generation": 3,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "R"
} }
sapphire = { sapphire = {
"Name": "Sapphire", "Name": "Sapphire",
"AltNames": ["Pokemon Sapphire", "Pokémon Sapphire", "S"], "AltNames": ["Pokemon Sapphire", "Pokémon Sapphire"],
"Generation": 3, "Generation": 3,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "S"
} }
platinum = { platinum = {
"Name": "Platinum", "Name": "Platinum",
"AltNames": ["Pokemon Platinum", "Pokémon Platinum", "Pt"], "AltNames": ["Pokemon Platinum", "Pokémon Platinum"],
"Generation": 4, "Generation": 4,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "Pt"
} }
heart_gold = { heart_gold = {
"Name": "HeartGold", "Name": "HeartGold",
"AltNames": ["Pokemon HeartGold", "Pokémon HeartGold", "HG"], "AltNames": ["Pokemon HeartGold", "Pokémon HeartGold"],
"Generation": 4, "Generation": 4,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "HG"
} }
soul_silver = { soul_silver = {
"Name": "SoulSilver", "Name": "SoulSilver",
"AltNames": ["Pokemon SoulSilver", "Pokémon SoulSilver", "SS"], "AltNames": ["Pokemon SoulSilver", "Pokémon SoulSilver"],
"Generation": 4, "Generation": 4,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "SS"
} }
diamond = { diamond = {
"Name": "Diamond", "Name": "Diamond",
"AltNames": ["Pokemon Diamond", "Pokémon Diamond", "D"], "AltNames": ["Pokemon Diamond", "Pokémon Diamond"],
"Generation": 4, "Generation": 4,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "D"
} }
pearl = { pearl = {
"Name": "Pearl", "Name": "Pearl",
"AltNames": ["Pokemon Pearl", "Pokémon Pearl", "P"], "AltNames": ["Pokemon Pearl", "Pokémon Pearl"],
"Generation": 4, "Generation": 4,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "P"
} }
black = { black = {
"Name": "Black", "Name": "Black",
"AltNames": ["Pokemon Black", "Pokémon Black", "B"], "AltNames": ["Pokemon Black", "Pokémon Black"],
"Generation": 5, "Generation": 5,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "B"
} }
white = { white = {
"Name": "White", "Name": "White",
"AltNames": ["Pokemon White", "Pokémon White", "W"], "AltNames": ["Pokemon White", "Pokémon White"],
"Generation": 5, "Generation": 5,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "W"
} }
black_2 = { black_2 = {
"Name": "Black 2", "Name": "Black 2",
"AltNames": ["Pokemon Black 2", "Pokémon Black 2", "B2"], "AltNames": ["Pokemon Black 2", "Pokémon Black 2"],
"Generation": 5, "Generation": 5,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "B2"
} }
white_2 = { white_2 = {
"Name": "White 2", "Name": "White 2",
"AltNames": ["Pokemon White 2", "Pokémon White 2", "W2"], "AltNames": ["Pokemon White 2", "Pokémon White 2"],
"Generation": 5, "Generation": 5,
"Mark": "Markless" "Mark": "Markless",
"Abvr": "W2"
} }
x = { x = {
"Name": "X", "Name": "X",
"AltNames": ["Pokemon X", "Pokémon X"], "AltNames": ["Pokemon X", "Pokémon X"],
"Generation": 6, "Generation": 6,
"Mark": "Kalos" "Mark": "Kalos",
"Abvr": "X"
} }
y = { y = {
"Name": "Y", "Name": "Y",
"AltNames": ["Pokemon Y", "Pokémon Y"], "AltNames": ["Pokemon Y", "Pokémon Y"],
"Generation": 6, "Generation": 6,
"Mark": "Kalos" "Mark": "Kalos",
"Abvr": "Y"
} }
omega_ruby = { omega_ruby = {
"Name": "Omega Ruby", "Name": "Omega Ruby",
"AltNames": ["Pokemon Omega Ruby", "Pokémon Omega Ruby", "OR"], "AltNames": ["Pokemon Omega Ruby", "Pokémon Omega Ruby"],
"Generation": 6, "Generation": 6,
"Mark": "Kalos" "Mark": "Kalos",
"Abvr": "OR"
} }
alpha_sapphire = { alpha_sapphire = {
"Name": "Alpha Sapphire", "Name": "Alpha Sapphire",
"AltNames": ["Pokemon Alpha Sapphire", "Pokémon Alpha Sapphire", "AS"], "AltNames": ["Pokemon Alpha Sapphire", "Pokémon Alpha Sapphire"],
"Generation": 6, "Generation": 6,
"Mark": "Kalos" "Mark": "Kalos",
"Abvr": "AS"
} }
sun = { sun = {
"Name": "Sun", "Name": "Sun",
"AltNames": ["Pokemon Sun", "Pokémon Sun"], "AltNames": ["Pokemon Sun", "Pokémon Sun"],
"Generation": 7, "Generation": 7,
"Mark": "Alola" "Mark": "Alola",
"Abvr": ""
} }
moon = { moon = {
"Name": "Moon", "Name": "Moon",
"AltNames": ["Pokemon Moon", "Pokémon Moon"], "AltNames": ["Pokemon Moon", "Pokémon Moon"],
"Generation": 7, "Generation": 7,
"Mark": "Alola" "Mark": "Alola",
"Abvr": ""
} }
ultra_sun = { ultra_sun = {
"Name": "Ultra Sun", "Name": "Ultra Sun",
"AltNames": ["Pokemon Ultra Sun", "Pokémon Ultra Sun", "US"], "AltNames": ["Pokemon Ultra Sun", "Pokémon Ultra Sun"],
"Generation": 7, "Generation": 7,
"Mark": "Alola" "Mark": "Alola",
"Abvr": "US"
} }
ultra_moon = { ultra_moon = {
"Name": "Ultra Moon", "Name": "Ultra Moon",
"AltNames": ["Pokemon Ultra Moon", "Pokémon Ultra Moon", "UM"], "AltNames": ["Pokemon Ultra Moon", "Pokémon Ultra Moon"],
"Generation": 7, "Generation": 7,
"Mark": "Alola" "Mark": "Alola",
"Abvr": "UM"
} }
sword = { sword = {
"Name": "Sword", "Name": "Sword",
"AltNames": ["Pokemon Sword", "Pokémon Sword", "Expansion Pass", "Expansion Pass (Sword)"], "AltNames": ["Pokemon Sword", "Pokémon Sword", "Expansion Pass", "Expansion Pass (Sword)"],
"Generation": 8, "Generation": 8,
"Mark": "Galar" "Mark": "Galar",
"Abvr": ""
} }
shield = { shield = {
"Name": "Shield", "Name": "Shield",
"AltNames": ["Pokemon Shield", "Pokémon Shield", "Expansion Pass", "Expansion Pass (Shield)"], "AltNames": ["Pokemon Shield", "Pokémon Shield", "Expansion Pass", "Expansion Pass (Shield)"],
"Generation": 8, "Generation": 8,
"Mark": "Galar" "Mark": "Galar",
"Abvr": ""
} }
brilliant_diamond = { brilliant_diamond = {
"Name": "Brilliant Diamond", "Name": "Brilliant Diamond",
"AltNames": ["Pokemon Brilliant Diamond", "Pokémon Brilliant Diamond", "BD"], "AltNames": ["Pokemon Brilliant Diamond", "Pokémon Brilliant Diamond"],
"Generation": 8, "Generation": 8,
"Mark": "Sinnoh" "Mark": "Sinnoh",
"Abvr": "BD"
} }
shining_pearl = { shining_pearl = {
"Name": "Shining Pearl", "Name": "Shining Pearl",
"AltNames": ["Pokemon Shining Pearl", "Pokémon Shining Pearl", "SP"], "AltNames": ["Pokemon Shining Pearl", "Pokémon Shining Pearl"],
"Generation": 8, "Generation": 8,
"Mark": "Sinnoh" "Mark": "Sinnoh",
"Abvr": "SP"
} }
legends_arceus = { legends_arceus = {
"Name": "Legends: Arceus", "Name": "Legends: Arceus",
"AltNames": ["Pokemon Legends: Arceus", "Pokémon Legends: Arceus", "LA", "Legends Arceus", "Arceus"], "AltNames": ["Pokemon Legends: Arceus", "Pokémon Legends: Arceus", "Legends Arceus", "Arceus"],
"Generation": 8, "Generation": 8,
"Mark": "Hisui" "Mark": "Hisui",
"Abvr": "LA"
} }
scarlet = { scarlet = {
"Name": "Scarlet", "Name": "Scarlet",
"AltNames": ["Pokemon Scarlet", "Pokémon Scarlet", "The Hidden Treasure of Area Zero", "The Hidden Treasure of Area Zero (Scarlet)", "The Teal Mask", "The Teal Mask (Scarlet)"], "AltNames": ["Pokemon Scarlet", "Pokémon Scarlet", "The Hidden Treasure of Area Zero", "The Hidden Treasure of Area Zero (Scarlet)", "The Teal Mask", "The Teal Mask (Scarlet)"],
"Generation": 9, "Generation": 9,
"Mark": "Paldea" "Mark": "Paldea",
"Abvr": ""
} }
violet = { violet = {
"Name": "Violet", "Name": "Violet",
"AltNames": ["Pokemon Violet", "Pokémon Violet", "The Hidden Treasure of Area Zero", "The Hidden Treasure of Area Zero (Violet)", "The Teal Mask", "The Teal Mask (Violet)"], "AltNames": ["Pokemon Violet", "Pokémon Violet", "The Hidden Treasure of Area Zero", "The Hidden Treasure of Area Zero (Violet)", "The Teal Mask", "The Teal Mask (Violet)"],
"Generation": 9, "Generation": 9,
"Mark": "Paldea" "Mark": "Paldea",
"Abvr": ""
} }
lets_go_pikachu = { lets_go_pikachu = {
"Name": "Lets Go Pikachu", "Name": "Lets Go Pikachu",
"AltNames": [], "AltNames": [],
"Generation": 8, "Generation": 8,
"Mark": "Let's Go" "Mark": "Let's Go",
"Abvr": ""
} }
lets_go_eevee = { lets_go_eevee = {
"Name": "Lets Go Eevee", "Name": "Lets Go Eevee",
"AltNames": [], "AltNames": [],
"Generation": 8, "Generation": 8,
"Mark": "Let's Go" "Mark": "Let's Go",
"Abvr": ""
} }
main_line_games = [ main_line_games = [
@ -351,7 +388,15 @@ POKEMON_PROPER_NOUNS = {
"Augurite", "Augurite",
"Electirizer", "Electirizer",
"Magmarizer", "Magmarizer",
"Gigantamax" "Gigantamax",
"Hangry",
"Amped",
"Eternamax",
"Terastal",
"Pa'u",
"Sensu",
"Debutante",
"Douse"
} }
POKEMON_PROPER_NOUNS = POKEMON_PROPER_NOUNS | set(regions) POKEMON_PROPER_NOUNS = POKEMON_PROPER_NOUNS | set(regions)

9
utility/pokemon_word_ninja.py

@ -31,12 +31,15 @@ class PokemonWordNinja:
def split(self, text: str) -> str: def split(self, text: str) -> str:
working_text = text working_text = text
working_text = working_text.replace("-", " ")
# First handle exact custom words to preserve capitalization # First handle exact custom words to preserve capitalization
for word in self.custom_words: for word in self.custom_words:
placeholder = self.word_to_placeholder_map[word] # Use word boundaries to make sure we only match full words
pattern = re.compile(re.escape(word), re.IGNORECASE) pattern = re.compile(r'\b' + re.escape(word) + r'\b', re.IGNORECASE)
working_text = pattern.sub(placeholder, working_text) if pattern.search(working_text):
placeholder = self.word_to_placeholder_map[word]
working_text = pattern.sub(placeholder, working_text)
# Clean up spaces # Clean up spaces
working_text = ' '.join(working_text.split()) working_text = ' '.join(working_text.split())

Loading…
Cancel
Save