基于Python 3.7实现任意摩尔斯码输入转意大利语单词的技术问询
Hey Tom, let's break down how to adapt your existing code to meet your Voynich decoding goals. First, let's recap your core needs and then fix the gaps in your current implementation.
Your Research Context
You're using Python 3.7 to research the Voynich Manuscript, operating under the premise that Wilfrid Voynich created it around 1910. Your approach maps Voynich symbols to Morse code sequences, then translates those into valid Italian (and occasional English) words by matching the total dot/dash count to character lengths.
Core Requirements
You need a tool that:
- Accepts any dot/dash input (doesn't have to follow standard Morse code letter mappings)
- Automatically maps these sequences to Italian letters, combining them into recognizable Italian words
- Leverages your existing anagram engine if feasible
Issues with Your Current Code
Your current script handles standard Morse encryption/decryption and generates anagrams, but it doesn't address two critical needs for your project:
- It only works with standard Morse code mappings—it can't handle arbitrary dot/dash sequences that don't match standard letters
- It doesn't filter generated anagrams to only include valid Italian words
Modified Solution Code
Here's an updated version that fixes these gaps. It uses an Italian dictionary to validate words and maps input sequences based on dot/dash length (aligning with your symbol-to-count matching rule):
print("Author Thomas O'Neil, copyright ver 0.1,VMS Italian Steganography Morse Code to Anagrams, August 8, 2019") # Load a comprehensive Italian word list (replace with your dictionary file path) def load_italian_dictionary(file_path): try: with open(file_path, 'r', encoding='utf-8') as f: # Convert all words to uppercase for consistent matching return set(word.strip().upper() for word in f if word.strip()) except FileNotFoundError: print("Error: Italian dictionary file not found. Please check the file path.") return set() # Preload Italian words (update the path to your dictionary file) ITALIAN_WORDS = load_italian_dictionary('italian_words.txt') # Mapping: number of dots/dashes in a sequence -> list of Italian letters with that Morse length # Adjust this if your Voynich research requires custom length-to-letter mappings MORSE_LENGTH_TO_ITALIAN_LETTERS = { 1: ['E'], 2: ['I', 'T'], 3: ['A', 'N', 'M', 'S'], 4: ['D', 'R', 'U', 'W', 'J', 'B', 'G', 'O'], 5: ['F', 'L', 'P', 'Q', 'X', 'Y', 'Z', '1'], 6: ['C', 'V', '2', '3'], 7: ['4', '5'], 8: ['6', '7'], 9: ['8'], 10: ['9', '0'] } def get_candidate_letters(morse_sequence): """Return all Italian letters that match the dot/dash count of the input sequence""" # Calculate total dots/dashes (ignore spaces in the sequence) sequence_length = len(morse_sequence.replace(' ', '')) return MORSE_LENGTH_TO_ITALIAN_LETTERS.get(sequence_length, []) def generate_valid_italian_words(morse_input): """Generate all valid Italian words from arbitrary Morse input sequences""" # Split input into individual Morse sequences (split by spaces) morse_sequences = [seq for seq in morse_input.split() if seq] # Get possible letters for each sequence position letter_options_per_position = [get_candidate_letters(seq) for seq in morse_sequences] # Generate all possible letter combinations from itertools import product all_combinations = product(*letter_options_per_position) # Filter combinations to only valid Italian words valid_words = [] for combo in all_combinations: candidate_word = ''.join(combo) if candidate_word in ITALIAN_WORDS: valid_words.append(candidate_word) # Add anagrams of valid words if needed (integrate your existing anagram function) has_anagram_support = True # Toggle this to enable/disable anagrams if valid_words and has_anagram_support: anagram_words = set() for word in valid_words: for anagram in anagrams(word): if anagram in ITALIAN_WORDS: anagram_words.add(anagram) valid_words.extend(list(anagram_words)) return list(set(valid_words)) # Remove duplicates # Your existing anagram function (unchanged) def anagrams(word): """Generate all anagrams of a word.""" if len(word) < 2: yield word else: for i, letter in enumerate(word): if letter not in word[:i]: # Avoid duplicate anagrams for j in anagrams(word[:i] + word[i+1:]): yield j + letter # Driver function def main(): while True: message = input("Enter Morse Code (dots/dashes, spaces separate sequences): ") valid_words = generate_valid_italian_words(message) if valid_words: print("\nValid Italian words found:") for idx, word in enumerate(sorted(valid_words), 1): print(f"{idx}. {word}") else: print("\nNo valid Italian words matched your input.") if __name__ == '__main__': main()
Key Enhancements
- Arbitrary Morse Sequence Support: Instead of relying on standard Morse code letter mappings, this code uses the total length of each dot/dash sequence to find matching Italian letters—perfect for your Voynich symbol-to-count rule
- Italian Word Validation: Filters all generated letter combinations against a real Italian dictionary to ensure only valid words are output
- Optional Anagram Integration: You can toggle anagram generation on/off directly in the function, leveraging your existing
anagramsfunction to expand results - Error Handling: Includes basic error handling for missing dictionary files to avoid crashes
Next Steps
- Source an Italian Dictionary: Find a free, comprehensive Italian word list (text file format) and update the file path in
load_italian_dictionary - Customize Length Mappings: If your research finds that Voynich symbols correspond to non-standard length-to-letter mappings, modify the
MORSE_LENGTH_TO_ITALIAN_LETTERSdictionary to match your findings - Test with Voynich Data: Feed your mapped Morse sequences from the Voynich Manuscript into the tool to see valid Italian word matches
内容的提问来源于stack exchange,提问作者Tom E. O'Neil

