diff options
| -rw-r--r-- | cli/commands/bridge/words.py | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/cli/commands/bridge/words.py b/cli/commands/bridge/words.py index d695bc9..558f6e3 100644 --- a/cli/commands/bridge/words.py +++ b/cli/commands/bridge/words.py @@ -38,10 +38,11 @@ def cli(ctx, opt_word_a, opt_word_b, opt_include_oe, opt_include_slang, opt_word newqueue = [] skip = {} found = False + should_reset = False # First compute distance to each node to find a path while len(queue): step = step + 1 - print(f"Iteration step {step}, distance {max_dist}, {len(queue) + len(newqueue)} items in queue") + print(f"Iteration step {step}, depth {max_dist}, {len(queue) + len(newqueue)} items in queue") print(f"Words: {', '.join(queue[:7])} ...") if step > 1: print_chain(thesaurus, opt_word_a, queue, marked, skip, prompt_to_remove=False) @@ -55,6 +56,8 @@ def cli(ctx, opt_word_a, opt_word_b, opt_include_oe, opt_include_slang, opt_word catid = cat['catid'] if catid in marked: continue + if word_q in skip and catid in skip[word_q]: + continue marked[catid] = marked[word_q] + 1 category_result = thesaurus.category(catid) # print(json.dumps(category_result, indent=2)) @@ -71,13 +74,28 @@ def cli(ctx, opt_word_a, opt_word_b, opt_include_oe, opt_include_slang, opt_word word_n = word_n.split("/")[0].strip() if word_n in marked: continue + if catid in skip and word_n in skip[catid]: + continue marked[word_n] = marked[catid] + 1 max_dist = max(max_dist, marked[word_n]) if word_n == opt_word_b: thesaurus.search(word_n) # print(queue) - print_chain(thesaurus, opt_word_a, [opt_word_b], marked, skip, prompt_to_remove=True) - newqueue.append(word_n) + should_reset = print_chain(thesaurus, opt_word_a, [opt_word_b], marked, skip, prompt_to_remove=True) + if should_reset: + marked = { opt_word_a: 0 } + queue = [opt_word_a] + newqueue = [] + break + else: + newqueue.append(word_n) + if should_reset: + break + if should_reset: + break + if should_reset: + should_reset = False + continue if step > 1 and len(newqueue) > opt_words_per_step: random.shuffle(newqueue) queue = newqueue[:opt_words_per_step] @@ -91,6 +109,10 @@ def cli(ctx, opt_word_a, opt_word_b, opt_include_oe, opt_include_slang, opt_word def print_chain(thesaurus, opt_word_a, opt_words_b, marked, skip, prompt_to_remove=False): """Follow the chain of shortest distance from the end back to the start""" # print(opt_word_a) + if prompt_to_remove: + print("") + print("--------------- PATH FOUND ---------------") + print("") word_n = opt_words_b[0] dist = marked[word_n] tries = 0 @@ -149,6 +171,7 @@ def print_chain(thesaurus, opt_word_a, opt_words_b, marked, skip, prompt_to_remo word_n = chain[-1] depth_tries += 1 elif depth_tries >= 100: + print(f"{depth_tries} {tries}") tries += 1 if tries >= len(opt_words_b): return @@ -166,6 +189,7 @@ def print_chain(thesaurus, opt_word_a, opt_words_b, marked, skip, prompt_to_remo else: print(f"{i+1} => {word}") if prompt_to_remove: + print("") print("If you don't like this path, enter the IDs of words to remove separated by spaces, or Ctrl-C to exit.") ids = input("Enter numbers > ") ids = ids.split(" ") @@ -175,11 +199,11 @@ def print_chain(thesaurus, opt_word_a, opt_words_b, marked, skip, prompt_to_remo id = int(id) id -= 1 word_a = chain[id] - print(f"Removing {word_a}") + # print(f"Removing {word_a}") if word_a in cat_reverse: word_a = cat_reverse[word_a] word_b = chain[id-1] - print(f"Connected upward to {word_b}") + # print(f"Connected upward to {word_b}") if word_b in cat_reverse: word_b = cat_reverse[word_b] if word_a in skip: @@ -188,4 +212,5 @@ def print_chain(thesaurus, opt_word_a, opt_words_b, marked, skip, prompt_to_remo skip[word_a] = [word_b] except Exception as e: continue - print(skip) + return True + return False |
