Skip to content
This repository was archived by the owner on Sep 13, 2022. It is now read-only.

Commit 7fddc1b

Browse files
committed
Added methods to dependency parse tagged sentence and file.
1 parent f4a791b commit 7fddc1b

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

zpar/DepParser.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@ def __init__(self, modelpath, libptr, zpar_session_obj):
2626
self._dep_parse_sentence.restype = c.c_char_p
2727
self._dep_parse_sentence.argtypes = [c.c_void_p, c.c_char_p, c.c_bool]
2828

29-
self._parse_file = libptr.dep_parse_file
30-
self._parse_file.restype = None
31-
self._parse_file.argtypes = [c.c_void_p, c.c_char_p, c.c_char_p, c.c_bool]
29+
self._dep_parse_file = libptr.dep_parse_file
30+
self._dep_parse_file.restype = None
31+
self._dep_parse_file.argtypes = [c.c_void_p, c.c_char_p, c.c_char_p, c.c_bool]
32+
33+
self._dep_parse_tagged_sentence = libptr.dep_parse_tagged_sentence
34+
self._dep_parse_tagged_sentence.restype = c.c_char_p
35+
self._dep_parse_tagged_sentence.argtypes = [c.c_void_p, c.c_char_p, c.c_char]
36+
37+
self._dep_parse_tagged_file = libptr.dep_parse_tagged_file
38+
self._dep_parse_tagged_file.restype = None
39+
self._dep_parse_tagged_file.argtypes = [c.c_void_p, c.c_char_p, c.c_char_p, c.c_char]
3240

3341
if self._load_depparser(self._zpar_session_obj, modelpath.encode('utf-8')):
3442
raise OSError('Cannot find dependency parser model at {}\n'.format(modelpath))
@@ -47,7 +55,23 @@ def dep_parse_sentence(self, sentence, tokenize=True):
4755

4856
def dep_parse_file(self, inputfile, outputfile, tokenize=True):
4957
if os.path.exists(inputfile):
50-
self._parse_file(self._zpar_session_obj, inputfile.encode('utf-8'), outputfile.encode('utf-8'), tokenize)
58+
self._dep_parse_file(self._zpar_session_obj, inputfile.encode('utf-8'), outputfile.encode('utf-8'), tokenize)
59+
else:
60+
raise OSError('File {} does not exist.'.format(inputfile))
61+
62+
def dep_parse_tagged_sentence(self, tagged_sentence, sep='/'):
63+
if not tagged_sentence.strip():
64+
# return empty string if the input is empty
65+
ans = ""
66+
else:
67+
zpar_compatible_sentence = tagged_sentence.strip().encode('utf-8')
68+
parsed_sent = self._dep_parse_tagged_sentence(self._zpar_session_obj, zpar_compatible_sentence, sep.encode('utf-8'))
69+
ans = parsed_sent.decode('utf-8')
70+
return ans
71+
72+
def dep_parse_tagged_file(self, inputfile, outputfile, sep='/'):
73+
if os.path.exists(inputfile):
74+
self._dep_parse_tagged_file(self._zpar_session_obj, inputfile.encode('utf-8'), outputfile.encode('utf-8'), sep.encode('utf-8'))
5175
else:
5276
raise OSError('File {} does not exist.'.format(inputfile))
5377

0 commit comments

Comments
 (0)