00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PyPipe.h"
00025
00026 namespace Parser {
00027
00028 Enumerator<Sentence*>* Parser::pipe(PyObject* pit)
00029 {
00030
00031 if (PyIter_Check(pit))
00032 return new ParserPipePython(*this, pit);
00033 return 0;
00034 }
00035
00036
00037 struct PySwigObject {
00038 PyObject_HEAD
00039 void* ptr;
00040 void* ty;
00041 int own;
00042 PyObject* next;
00043 };
00044
00045 ParserPipePython::ParserPipePython(Parser& parser, PyObject* pit) :
00046 parser(parser),
00047 pit(pit),
00048 language(Language::get(lang->c_str()))
00049 {
00050 Py_INCREF(pit);
00051 }
00052
00053 ParserPipePython::~ParserPipePython()
00054 {
00055 Py_DECREF(pit);
00056 }
00057
00058 void ParserPipePython::Dispose()
00059 {
00060 parser.decRef();
00061 delete this;
00062 }
00063
00064 bool ParserPipePython::MoveNext()
00065 {
00066 next = PyIter_Next(pit);
00067 return next;
00068 }
00069
00070 static void* extract_swig_wrapped_pointer(PyObject* obj)
00071 {
00072 PyObject* thisAttr = PyObject_GetAttrString(obj, "this");
00073 if (thisAttr == NULL)
00074 return NULL;
00075
00076 void* pointer = ((PySwigObject*)thisAttr)->ptr;
00077 Py_DECREF(thisAttr);
00078 return pointer;
00079 }
00080
00081 Sentence* ParserPipePython::Current()
00082 {
00083
00084 PyObject* iterator = PyObject_GetIter(next);
00085 if (iterator == NULL) {
00086
00087 }
00088
00089 Sentence* sentence = new Sentence(language);
00090 int id = 1;
00091 PyObject* item;
00092 while (item = PyIter_Next(iterator)) {
00093
00094 Token* tok = (Token*)extract_swig_wrapped_pointer(item);
00095 TreeToken* token = new TreeToken(id++, tok->form, tok->attributes, tok->links);
00096 sentence->push_back(token);
00097 Py_DECREF(item);
00098 }
00099
00100 Py_DECREF(iterator);
00101
00102 if (PyErr_Occurred()) {
00103
00104 }
00105 return parser.parse(sentence);
00106 }
00107
00108 }