Advertisement
smeech

contractions

Mar 17th, 2024 (edited)
70
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | Software | 0 0
  1. # Export smallest unique contractions of a list of words for Espanso
  2.  
  3. from collections import defaultdict
  4.  
  5. def shortest_unique_component(word, words_count):
  6.     if words_count is None:
  7.         return None
  8.    
  9.     for i in range(len(word)):
  10.         contraction = word[:i+1]
  11.         if words_count[contraction] == 1:
  12.             return contraction
  13.  
  14. def process_words(file_name):
  15.     words_count = defaultdict(int)
  16.     with open(file_name, 'r') as file:
  17.         for line in file:
  18.             words = line.strip().split()
  19.             for word in words:
  20.                 for i in range(len(word)):
  21.                     contraction = word[:i+1]
  22.                     words_count[contraction] += 1
  23.     return words_count if words_count else None
  24.  
  25. def main():
  26.     file_name = input("Enter the file name containing the list of words: ")
  27.     words_count = process_words(file_name)
  28.    
  29.     matches = []
  30.     with open(file_name, 'r') as file:
  31.         for line in file:
  32.             words = line.strip().split()
  33.             for word in words:
  34.                 contraction = shortest_unique_component(word, words_count)
  35.                 if contraction is not None and contraction != word:
  36.                     matches.append((contraction, word))
  37.  
  38.     with open("contractions.yml", "w") as output_file:
  39.         output_file.write("matches:\n")
  40.         for contraction, word in matches:
  41.             output_file.write(f" - trigger: {contraction}\n")
  42.             output_file.write(f"   replace: {word}\n")
  43.  
  44. if __name__ == "__main__":
  45.     main()
  46.  
Advertisement
Comments
  • smeech
    280 days
    # text 0.13 KB | 0 0
    1. Takes a list of words and returns an Espanso .yml file containing trigger:/replace: pairs for those words that have unique contractions.
Add Comment
Please, Sign In to add comment
Advertisement