Skip to content

Commit

Permalink
All files needed to create Python module cstlempy
Browse files Browse the repository at this point in the history
  • Loading branch information
BartJongejan committed Oct 11, 2021
1 parent 7efb808 commit bff2860
Show file tree
Hide file tree
Showing 7 changed files with 646 additions and 0 deletions.
4 changes: 4 additions & 0 deletions build-launch.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
del cstlempy.c*
del /s /q build
python setup.py build_ext --inplace
python launch.py
5 changes: 5 additions & 0 deletions build-launch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
rm cstlempy.c*
rm -r build
python setup.py build_ext --inplace
python launch.py
24 changes: 24 additions & 0 deletions cstlempy.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# cstlempy.pyx - Python Module, this code will be translated to C by Cython.

cdef extern from "tinylemmatizer.h":
cdef extern void startProc(const char* flexrulesFileName, int* err)
cdef extern void stringEval(const char * s,const char ** out,int * err)
cdef extern void endProc()

# Function to be called once, before the first call to lemmatize()
def init(Str):
cdef int Err
startProc(bytes(Str,'iso8859-1'),&Err)

# Function to lemmatize a full form
def lemmatize(Str):
cdef const char * Sout
cdef int Err
stringEval(bytes(Str,'iso8859-1'),&Sout,&Err)
cdef bytes py_string = Sout
return py_string.decode('UTF-8')

# Function to be called after the last call to lemmatize()
def final():
endProc()

18 changes: 18 additions & 0 deletions launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# launch.py - Python 3 stub loader, loads the 'cstlempy' module that was made by Cython.

# This code is always interpreted, like normal Python.
# It is not compiled to C.

import cstlempy

filename = "flexrules"
print("Initialise cstlempy with the call 'cstlempy.init("+filename+")'.\n")
cstlempy.init(filename)

cstlempyFullForm = "went"
print("Going to call lemmatize(<full form>).\n\nThe full form is this one: "+cstlempyFullForm)
answer = cstlempy.lemmatize(cstlempyFullForm)
print('Answer from cstlempy: '+answer+'\n')
cstlempy.final()
print("We have called cstlempy.final(), so now cstlempy is not available until we call\n'cstlempy.init()' again.")

13 changes: 13 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# setup.py
#
from setuptools import setup
from Cython.Build import cythonize
from setuptools.extension import Extension

sourcefiles = ['tinylemmatizer.c', 'cstlempy.pyx']

extensions = [Extension("cstlempy", sourcefiles, extra_compile_args=["-I.", "-D_CRT_SECURE_NO_WARNINGS","-DPYTHONINTERFACE"])]

setup(
ext_modules = cythonize(extensions, emit_linenums=True, compiler_directives={'language_level': 3})
)
Loading

0 comments on commit bff2860

Please sign in to comment.