Skip to content

Commit

Permalink
Minor C fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Dusan Malusev <dusan@dusanmalusev.dev>
  • Loading branch information
CodeLieutenant committed Jul 13, 2023
1 parent 5ce3381 commit ba54e0c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 60 deletions.
3 changes: 3 additions & 0 deletions cbits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct membuf : std::streambuf
}
};

BEGIN_EXTERN_C()
FastText_Result_t FastText_NewHandle(FastText_String_t path)
{
auto model = new fasttext::FastText();
Expand Down Expand Up @@ -179,3 +180,5 @@ FastText_PredictItem_t FastText_PredictItemAt(FastText_Predict_t predict, size_t
str,
};
}

END_EXTERN_C()
106 changes: 57 additions & 49 deletions cbits.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,72 @@
#include <stdint.h>

#ifdef __cplusplus
extern "C"
{
#define BEGIN_EXTERN_C() \
extern "C" \
{
#else
#define BEGIN_EXTERN_C()
#endif

typedef void *FastText_Handle_t;
#ifdef __cplusplus
#define END_EXTERN_C() }
#else
#define END_EXTERN_C()
#endif

typedef struct
{
float *data;
void *handle;
size_t size;
} FastText_FloatVector_t;
BEGIN_EXTERN_C()
typedef void *FastText_Handle_t;

typedef struct
{
size_t size;
char *data;
} FastText_String_t;
typedef struct
{
float *data;
void *handle;
size_t size;
} FastText_FloatVector_t;

typedef struct
{
float probability;
FastText_String_t label;
} FastText_PredictItem_t;
typedef struct
{
size_t size;
char *data;
} FastText_String_t;

typedef struct
{
size_t size;
void *data;
} FastText_Predict_t;
typedef struct
{
float probability;
FastText_String_t label;
} FastText_PredictItem_t;

typedef struct
{
size_t size;
void *data;
} FastText_Predict_t;

typedef struct
typedef struct
{
enum
{
enum
{
SUCCESS = 0,
ERROR = 1,
} status;
SUCCESS = 0,
ERROR = 1,
} status;

union {
FastText_Handle_t handle;
char *error;
};
} FastText_Result_t;
union {
FastText_Handle_t handle;
char *error;
};
} FastText_Result_t;

FastText_Result_t FastText_NewHandle(FastText_String_t path);
void FastText_DeleteHandle(const FastText_Handle_t handle);
FastText_Predict_t FastText_Predict(const FastText_Handle_t handle, FastText_String_t query, uint32_t k,
float threshold);
FastText_FloatVector_t FastText_Wordvec(const FastText_Handle_t handle, FastText_String_t word);
FastText_FloatVector_t FastText_Sentencevec(const FastText_Handle_t handle, FastText_String_t sentance);
FastText_Predict_t FastText_Analogy(const FastText_Handle_t handle, FastText_String_t word1,
FastText_String_t word2, FastText_String_t word3, uint32_t k);
FastText_Result_t FastText_NewHandle(FastText_String_t path);
void FastText_DeleteHandle(const FastText_Handle_t handle);
FastText_Predict_t FastText_Predict(const FastText_Handle_t handle, FastText_String_t query, uint32_t k,
float threshold);
FastText_FloatVector_t FastText_Wordvec(const FastText_Handle_t handle, FastText_String_t word);
FastText_FloatVector_t FastText_Sentencevec(const FastText_Handle_t handle, FastText_String_t sentance);
FastText_Predict_t FastText_Analogy(const FastText_Handle_t handle, FastText_String_t word1, FastText_String_t word2,
FastText_String_t word3, uint32_t k);

void FastText_FreeFloatVector(FastText_FloatVector_t vector);
void FastText_FreePredict(FastText_Predict_t predict);
void FastText_FreeFloatVector(FastText_FloatVector_t vector);
void FastText_FreePredict(FastText_Predict_t predict);

FastText_PredictItem_t FastText_PredictItemAt(FastText_Predict_t predict, size_t idx);
#ifdef __cplusplus
}
#endif
FastText_PredictItem_t FastText_PredictItemAt(FastText_Predict_t predict, size_t idx);
END_EXTERN_C()
20 changes: 9 additions & 11 deletions fasttext.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ var (

// A model object. Effectively a wrapper
// around the C fasttext handle
type Model struct {
p C.FastText_Handle_t
}
type (
Model struct {
p C.FastText_Handle_t
}

type ModelOpenError struct {
val string
}
ModelOpenError string
)

func (e *ModelOpenError) Error() string {
return e.val
func (e ModelOpenError) Error() string {
return string(e)
}

// Opens a model from a path and returns a model
Expand All @@ -43,9 +43,7 @@ func Open(path string) (Model, error) {
if result.status != 0 {
ch := *(**C.char)(unsafe.Pointer(&result.anon0[0]))
defer C.free(unsafe.Pointer(ch))
return Model{}, &ModelOpenError{
val: C.GoString(ch),
}
return Model{}, ModelOpenError(C.GoString(ch))
}

handle := *(*C.FastText_Handle_t)(unsafe.Pointer(&result.anon0[0]))
Expand Down

0 comments on commit ba54e0c

Please sign in to comment.