forked from ahmednabil950/Arabic_Parser_NLTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.py
70 lines (57 loc) · 2.26 KB
/
Parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 3 03:47:48 2017
@author: Ahmed
"""
class Parser(object):
"""
A natural language parser is a program that works out the grammatical
structure of sentences, for instance, which groups of words go together
(as “phrases”) and which words are the subject or object of a verb.
Probabilistic parsers use knowledge of language gained from hand-parsed
sentences to try to produce the most likely analysis of new sentences.
These statistical parsers still make some mistakes, but commonly work
rather well. Their development was one of the biggest breakthroughs in
natural language processing in the 1990s.
"""
def __init__(self, model_path, path_to_jar, path_to_models_jar):
# nltk package
from nltk.parse.stanford import StanfordParser
self.__model_path = model_path
self.__path_to_jar = path_to_jar
self.__path_to_model_jar = path_to_models_jar
self.__stf_parser = StanfordParser(
path_to_jar=path_to_jar,
path_to_models_jar=path_to_models_jar,
model_path=model_path,
encoding='utf-8'
)
def parse_sentence(self, text):
"""
Arguments:
text -- input text string to be parsed
Returns:
list of the parsed result in the form (parent_tag(tag, word))
"""
self.__text = text
return list(self.__stf_parser.raw_parse(text))
def tree_print(self):
"""
Arguments:
-- None
Returns:
-- None
"""
for line in self.__stf_parser.raw_parse(self.__text):
for sentence in line:
print(sentence)
def tree_draw(self):
"""
Arguments:
-- None
Returns:
-- None
"""
for line in self.__stf_parser.raw_parse(self.__text):
for sentence in line:
sentence.draw()