Created
October 6, 2023 05:22
-
-
Save markusl/836b9a79bcca63ce6d4c2c341da9726d to your computer and use it in GitHub Desktop.
Read all files from the input folder and convert all words in them to baseform words and write files to the output folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import libvoikko | |
import json | |
import os | |
# Read all files from the input folder and convert all words in them to baseform words and write files to the output folder. | |
# brew install --cask voikkospellservice | |
# brew install libvoikko | |
# pip3 install libvoikko | |
v = libvoikko.Voikko(u"fi") | |
ignore_words = ["olla"] | |
folder_path = "input_path" | |
new_folder_path = "output_path" | |
new_file_postfix = "" | |
for filename in os.listdir(folder_path): | |
if os.path.isfile(os.path.join(folder_path, filename)): | |
with open(os.path.join(folder_path, filename), 'r') as original_file: | |
txt = original_file.read() | |
txt = txt.lower().replace(".", " ").replace(",", " ").replace("\n", " ") | |
word_list = txt.split(" ") | |
bf_list = [] | |
for w in word_list: | |
voikko_dict = v.analyze(w) | |
if voikko_dict: | |
bf_word = voikko_dict[0]['BASEFORM'] | |
else: | |
bf_word = w | |
if len(bf_word) > 3 and bf_word not in ignore_words: | |
bf_list.append(bf_word) | |
new_filename = os.path.splitext(filename)[0] + new_file_postfix + os.path.splitext(filename)[1] | |
with open(os.path.join(new_folder_path, new_filename), 'w') as new_file: | |
new_file.write(json.dumps(bf_list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment