added tool/ folder
Browse files- tool/break4all.sh +24 -0
- tool/combine_languages.sh +26 -0
- tool/rm_space.py +42 -0
- tool/sylbreak.py +95 -0
- tool/train_fasttext_class.sh +31 -0
tool/break4all.sh
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# Define the base directory and the Python script
|
| 4 |
+
BASE_DIR="$HOME/exp/sylbreak4all/lang_detection/embedding"
|
| 5 |
+
SYLBREAK_SCRIPT="$BASE_DIR/sylbreak.py" # Replace with the correct path to sylbreak.py
|
| 6 |
+
INPUT_DIR="$BASE_DIR/eg_input"
|
| 7 |
+
|
| 8 |
+
# Loop through each .txt file in the eg_input directory
|
| 9 |
+
for file in "$INPUT_DIR"/*.txt; do
|
| 10 |
+
input_file="$file"
|
| 11 |
+
output_file="${file%.txt}.syl"
|
| 12 |
+
|
| 13 |
+
# Run the sylbreak.py script
|
| 14 |
+
python "$SYLBREAK_SCRIPT" --input "$input_file" --separator " " --output "$output_file"
|
| 15 |
+
|
| 16 |
+
# Remove the original file and rename the output file
|
| 17 |
+
rm "$input_file"
|
| 18 |
+
mv "$output_file" "$input_file"
|
| 19 |
+
|
| 20 |
+
echo "Processed: $input_file"
|
| 21 |
+
done
|
| 22 |
+
|
| 23 |
+
echo "Syllable breaking completed for all files."
|
| 24 |
+
|
tool/combine_languages.sh
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# Define the directory containing the .syl files
|
| 4 |
+
SYL_SEG_DIR="./syl_seg"
|
| 5 |
+
# Define the output file name
|
| 6 |
+
OUTPUT_FILE="combined_language_data.txt"
|
| 7 |
+
|
| 8 |
+
# Empty the output file if it already exists
|
| 9 |
+
> "$OUTPUT_FILE"
|
| 10 |
+
|
| 11 |
+
# Loop through each text file in the syl_seg directory
|
| 12 |
+
for file in "$SYL_SEG_DIR"/*; do
|
| 13 |
+
# Extract the language name from the filename
|
| 14 |
+
filename=$(basename -- "$file")
|
| 15 |
+
language=${filename%%.*}
|
| 16 |
+
|
| 17 |
+
# Read each line in the file and append it to the output file with the language label
|
| 18 |
+
while IFS= read -r line; do
|
| 19 |
+
echo -e "$line\t$language" >> "$OUTPUT_FILE"
|
| 20 |
+
done < "$file"
|
| 21 |
+
|
| 22 |
+
echo "Processed $file"
|
| 23 |
+
done
|
| 24 |
+
|
| 25 |
+
echo "All files have been combined into $OUTPUT_FILE"
|
| 26 |
+
|
tool/rm_space.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
|
| 3 |
+
A program to remove spaces from all files located in a specified input directory.
|
| 4 |
+
Written by Ye Kyaw Thu, LU Lab., Myanmar.
|
| 5 |
+
Last updated: 23 Jan 2024
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
python ./rm_space.py --input ./syl_seg --output ./raw
|
| 9 |
+
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import os
|
| 13 |
+
import argparse
|
| 14 |
+
|
| 15 |
+
def process_files(input_dir, output_dir):
|
| 16 |
+
# Create output directory if it doesn't exist
|
| 17 |
+
if not os.path.exists(output_dir):
|
| 18 |
+
os.makedirs(output_dir)
|
| 19 |
+
|
| 20 |
+
for filename in os.listdir(input_dir):
|
| 21 |
+
input_file_path = os.path.join(input_dir, filename)
|
| 22 |
+
output_file_path = os.path.join(output_dir, f"{filename}.raw")
|
| 23 |
+
|
| 24 |
+
# Read the content and remove spaces
|
| 25 |
+
with open(input_file_path, 'r', encoding='utf-8') as file:
|
| 26 |
+
content = file.read().replace(' ', '')
|
| 27 |
+
|
| 28 |
+
# Write the content to the new file in the output directory
|
| 29 |
+
with open(output_file_path, 'w', encoding='utf-8') as file:
|
| 30 |
+
file.write(content)
|
| 31 |
+
|
| 32 |
+
print(f"Processed: {input_file_path} -> {output_file_path}")
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
parser = argparse.ArgumentParser(description="Process files to remove spaces and save with .raw extension.")
|
| 36 |
+
parser.add_argument('--input', type=str, required=True, help="Input directory path")
|
| 37 |
+
parser.add_argument('--output', type=str, required=True, help="Output directory path")
|
| 38 |
+
|
| 39 |
+
args = parser.parse_args()
|
| 40 |
+
|
| 41 |
+
process_files(args.input, args.output)
|
| 42 |
+
|
tool/sylbreak.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding:utf-8 -*-
|
| 3 |
+
|
| 4 |
+
import argparse
|
| 5 |
+
import re
|
| 6 |
+
import sys
|
| 7 |
+
|
| 8 |
+
"""
|
| 9 |
+
Syllable breaking tool for Myanmar language.
|
| 10 |
+
|
| 11 |
+
Usage: python sylbreak.py --help
|
| 12 |
+
cat test.txt | python sylbreak.py
|
| 13 |
+
python sylbreak.py --input test.txt
|
| 14 |
+
python ./sylbreak.py --input ./test.txt --print
|
| 15 |
+
python sylbreak.py --input test.txt --output out.txt
|
| 16 |
+
python ./sylbreak.py --input ./one_line.txt --separator " " --output one_line.syl
|
| 17 |
+
|
| 18 |
+
Date: 21 July 2016
|
| 19 |
+
Written by Ye Kyaw Thu, Visiting Researcher, Waseda University
|
| 20 |
+
HP: https://sites.google.com/site/yekyawthunlp/
|
| 21 |
+
|
| 22 |
+
Date: 29 Sep 2021
|
| 23 |
+
Add support for python3 by sengkyaut
|
| 24 |
+
|
| 25 |
+
Last Updated: 19 January 2024.
|
| 26 |
+
The code has been rewritten for easier readability. It now includes features for removing the leading delimiter and replacing sequences of 'delimiter-space-delimiter' with a single space.
|
| 27 |
+
Updated by Ye Kyaw Thu.
|
| 28 |
+
|
| 29 |
+
Reference of Myanmar Unicode: http://unicode.org/charts/PDF/U1000.pdf
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
def parse_arguments():
|
| 33 |
+
"""Parse command line arguments for the script."""
|
| 34 |
+
parser = argparse.ArgumentParser(description='Syllable segmentation for Myanmar language')
|
| 35 |
+
parser.add_argument('-i', '--input', type=str, help='Input file (optional)')
|
| 36 |
+
parser.add_argument('-o', '--output', type=str, help='Output file (optional)')
|
| 37 |
+
parser.add_argument('-s', '--separator', type=str, default='|', help='Separator for syllable (e.g. -s "/"), default is "|"')
|
| 38 |
+
parser.add_argument('-p', '--print', action='store_true', help='Print both input and syllable segmented sentences')
|
| 39 |
+
return parser.parse_args()
|
| 40 |
+
|
| 41 |
+
def create_break_pattern():
|
| 42 |
+
"""Creates and returns the regular expression pattern for Myanmar syllable breaking."""
|
| 43 |
+
my_consonant = r"က-အ"
|
| 44 |
+
en_char = r"a-zA-Z0-9"
|
| 45 |
+
other_char = r"ဣဤဥဦဧဩဪဿ၌၍၏၀-၉၊။!-/:-@[-`{-~\s"
|
| 46 |
+
subscript_symbol = r'္'
|
| 47 |
+
a_that = r'်'
|
| 48 |
+
|
| 49 |
+
# Regular expression pattern for Myanmar syllable breaking
|
| 50 |
+
return re.compile(
|
| 51 |
+
r"((?<!" + subscript_symbol + r")[" + my_consonant + r"]"
|
| 52 |
+
r"(?!["
|
| 53 |
+
+ a_that + subscript_symbol + r"])"
|
| 54 |
+
+ r"|[" + en_char + other_char + r"])"
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
def break_syllables(line, break_pattern, separator):
|
| 58 |
+
"""Applies syllable breaking rules to a line."""
|
| 59 |
+
line = re.sub(r'\s+', ' ', line.strip())
|
| 60 |
+
segmented_line = break_pattern.sub(separator + r"\1", line)
|
| 61 |
+
|
| 62 |
+
# Remove the leading delimiter if it exists
|
| 63 |
+
if segmented_line.startswith(separator):
|
| 64 |
+
segmented_line = segmented_line[len(separator):]
|
| 65 |
+
|
| 66 |
+
# Replace delimiter+space+delimiter with a single space
|
| 67 |
+
double_delimiter = separator + " " + separator
|
| 68 |
+
segmented_line = segmented_line.replace(double_delimiter, " ")
|
| 69 |
+
|
| 70 |
+
return segmented_line
|
| 71 |
+
|
| 72 |
+
def process_input(input_stream, output_stream, separator, print_option):
|
| 73 |
+
"""Reads, processes, and writes the syllable segmented data."""
|
| 74 |
+
for line in input_stream:
|
| 75 |
+
if print_option:
|
| 76 |
+
print("Input:\t" + line.strip())
|
| 77 |
+
segmented_line = break_syllables(line, break_pattern, separator)
|
| 78 |
+
output_stream.write(segmented_line + '\n')
|
| 79 |
+
if print_option:
|
| 80 |
+
print("Sylbreaked:\t" + segmented_line)
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
args = parse_arguments()
|
| 84 |
+
break_pattern = create_break_pattern()
|
| 85 |
+
|
| 86 |
+
input_stream = open(args.input, 'r', encoding='utf-8') if args.input else sys.stdin
|
| 87 |
+
output_stream = open(args.output, 'w', encoding='utf-8') if args.output else sys.stdout
|
| 88 |
+
|
| 89 |
+
try:
|
| 90 |
+
process_input(input_stream, output_stream, args.separator, args.print)
|
| 91 |
+
finally:
|
| 92 |
+
if args.input:
|
| 93 |
+
input_stream.close()
|
| 94 |
+
if args.output:
|
| 95 |
+
output_stream.close()
|
tool/train_fasttext_class.sh
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
mkdir -p ./profile/fasttext_class;
|
| 4 |
+
|
| 5 |
+
## Training with 3 gram
|
| 6 |
+
time python ./demo_usage.py --mode train --input ./data/all_languages.fasttext \
|
| 7 |
+
--output ./profile/fasttext_class/3gram.model.bin --epoch 25 --lr 1.0 --wordNgrams 3 \
|
| 8 |
+
--approach fasttext_class
|
| 9 |
+
|
| 10 |
+
## Training with 4 gram
|
| 11 |
+
time python ./demo_usage.py --mode train --input ./data/all_languages.fasttext \
|
| 12 |
+
--output ./profile/fasttext_class/4gram.model.bin --epoch 25 --lr 1.0 --wordNgrams 4 \
|
| 13 |
+
--approach fasttext_class
|
| 14 |
+
|
| 15 |
+
## Training with 5 gram
|
| 16 |
+
time python ./demo_usage.py --mode train --input ./data/all_languages.fasttext \
|
| 17 |
+
--output ./profile/fasttext_class/5gram.model.bin --epoch 25 --lr 1.0 --wordNgrams 5 \
|
| 18 |
+
--approach fasttext_class
|
| 19 |
+
|
| 20 |
+
## Training with 6 gram
|
| 21 |
+
time python ./demo_usage.py --mode train --input ./data/all_languages.fasttext \
|
| 22 |
+
--output ./profile/fasttext_class/6gram.model.bin --epoch 25 --lr 1.0 --wordNgrams 6 \
|
| 23 |
+
--approach fasttext_class
|
| 24 |
+
|
| 25 |
+
## Training with 7 gram
|
| 26 |
+
time python ./demo_usage.py --mode train --input ./data/all_languages.fasttext \
|
| 27 |
+
--output ./profile/fasttext_class/7gram.model.bin --epoch 25 --lr 1.0 --wordNgrams 7 \
|
| 28 |
+
--approach fasttext_class
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|