Skip to content

Commit

Permalink
fix: correct foma2js.py (fixes mhulden#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhdaines committed May 12, 2024
1 parent e0d8122 commit f87ab40
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ Makefile
*.pc
*.cmake
CMake*
__pycache__/
16 changes: 8 additions & 8 deletions foma/python/foma2js.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,16 @@ def main():
if '\'' in symbol:
symbol = symbol.replace('\'', '\\\'')
sigma[number] = symbol
if number > 2 and len(symbol) > longest_symbol_length:
longest_symbol_length = len(symbol)
if number > 2:
longest_symbol_length = max(longest_symbol_length,
len(symbol.encode("UTF-16LE")) // 2)
elif mode is Mode.NONE:
raise ValueError('Format error')

print('var {} = new Object;'.format(args.name))
print('{}.t = Array;'.format(args.name))
print('{}.f = Array;'.format(args.name))
print('{}.s = Array;'.format(args.name))
print('{}.t = new Object;'.format(args.name))
print('{}.f = new Object;'.format(args.name))
print('{}.s = new Object;'.format(args.name))
print()

for key in trans:
Expand All @@ -128,9 +129,8 @@ def main():
if i in finals:
print('{}.f[{}] = 1;'.format(args.name, i))

for i in range(3, len(sigma)):
if i in sigma:
print('{}.s[\'{}\'] = {};'.format(args.name, sigma[i], i))
for i in range(3, max(sigma.keys()) + 1):
print('{}.s[\'{}\'] = {};'.format(args.name, sigma[i], i))

print('{}.maxlen = {} ;'.format(args.name, longest_symbol_length))

Expand Down
41 changes: 40 additions & 1 deletion foma/python/test_foma.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# -*- coding: utf-8 -*-

"""
Test cases for Foma Python bindings.
"""

import os
import pytest
import subprocess
from foma import FST


Expand All @@ -27,3 +29,40 @@ def test_apply_down(eat_fst):
@pytest.fixture
def eat_fst():
return FST.load('ate.fsm')


GOODJS = r"""var myNet = new Object;
myNet.t = new Object;
myNet.f = new Object;
myNet.s = new Object;
myNet.t[0 + '|' + '[NO\'UN]'] = [{1:'[NO\'UN]'}];
myNet.t[1 + '|' + '[VERB]'] = [{2:'[VERB]'}];
myNet.t[2 + '|' + 'c'] = [{5:''},{3:'d'}];
myNet.t[3 + '|' + 'a'] = [{4:'o'}];
myNet.t[4 + '|' + 't'] = [{7:'g'}];
myNet.t[5 + '|' + 'a'] = [{6:''}];
myNet.t[6 + '|' + 't'] = [{7:''}];
myNet.t[7 + '|' + 'ROTFLMAO🤣'] = [{8:'ROTFLMAO🤣'}];
myNet.f[8] = 1;
myNet.s['ROTFLMAO🤣'] = 3;
myNet.s['[NO\'UN]'] = 4;
myNet.s['[VERB]'] = 5;
myNet.s['a'] = 6;
myNet.s['c'] = 7;
myNet.s['d'] = 8;
myNet.s['g'] = 9;
myNet.s['o'] = 10;
myNet.s['t'] = 11;
myNet.maxlen = 10 ;
"""


def test_js():
foma2js = os.path.join(os.path.dirname(__file__), "foma2js.py")
testfoma = os.path.join(os.path.dirname(__file__), os.path.pardir,
"tests", "testjs.foma")
out = subprocess.run(["python", foma2js, testfoma],
capture_output=True, check=True)
js = out.stdout.decode()
assert js.strip() == GOODJS.strip()
Binary file added foma/tests/testjs.foma
Binary file not shown.

0 comments on commit f87ab40

Please sign in to comment.