class CNLP: CNLPServerURL = 'http://localhost:9000' def __init__(self): self.parser = CoreNLPParser(url=self.CNLPServerURL) self.dep_parser = CoreNLPDependencyParser(url=self.CNLPServerURL) self.ner_tagger = CoreNLPParser(url=self.CNLPServerURL, tagtype='ner') self.pos_tagger = CoreNLPParser(url=self.CNLPServerURL, tagtype='pos') def getParse(self, sentence): if (type(sentence) == list): return self.parser.parse(sentence) else: return self.parser.raw_parse(sentence) def getDepParse(self, sentence): if (type(sentence) == list): return self.dep_parser.parse(sentence) else: return self.dep_parser.raw_parse(sentence) def getNERTags(self, sentence): if (type(sentence) != list): sentence = sentence.split() return self.ner_tagger.tag(sentence) def getPOSTags(self, sentence): if (type(sentence) == list): return self.pos_tagger.parse(sentence) else: return self.pos_tagger.raw_parse(sentence)
def convert_eng_to_isl(input_string): if len(list(input_string.split(' '))) is 1: return list(input_string.split(' ')) # Initializing stanford parser parser = CoreNLPParser() # Generates all possible parse trees sort by probability for the sentence possible_parse_tree_list = [tree for tree in parser.parse(input_string.split())] # Get most probable parse tree parse_tree = possible_parse_tree_list[0] # print(parse_tree) # output = '(ROOT # (S # (PP (IN As) (NP (DT an) (NN accountant))) # (NP (PRP I)) # (VP (VBP want) (S (VP (TO to) (VP (VB make) (NP (DT a) (NN payment)))))) # ) # )' # Convert into tree data structure parent_tree = ParentedTree.convert(parse_tree) print("\n\nParse Tree:\n") print(parent_tree) modified_parse_tree = modify_tree_structure(parent_tree) print("\n\nModified Parse Tree:\n") print(modified_parse_tree) isl_sentence = modified_parse_tree.leaves() return isl_sentence
def convert_sentence(input_string: str): java_path = '/usr/bin/java' os.environ['CLASSPATH'] = java_path if input_string.split() == 1: return None if len(input_string.split()) == 1: path = create_video(input_string) return path parser = CoreNLPParser(url='http://localhost:9000') englishtree = [tree for tree in parser.parse(input_string.split())] parsetree = englishtree[0] dict = {} # parenttree = ParentedTree(node=parsetree, children=[]) parenttree = ParentedTree.fromstring(str(parsetree)) # print("Input Sentence: ", input_string) # print("Input Sentence Tree\n") # print(parenttree) print("\n\n") for sub in parenttree.subtrees(): dict[sub.treeposition()] = 0 #----------------------------# islTree = Tree('ROOT', []) i = 0 for sub in parenttree.subtrees(): if (sub.label() == "NP" and dict[sub.treeposition()] == 0 and dict[sub.parent().treeposition()] == 0): dict[sub.treeposition()] = 1 islTree.insert(i, sub) i = i + 1 if (sub.label() == "VP" or sub.label() == "PRP"): for sub2 in sub.subtrees(): if ((sub2.label() == "NP" or sub2.label() == 'PRP') and dict[sub2.treeposition()] == 0 and dict[sub2.parent().treeposition()] == 0): dict[sub2.treeposition()] = 1 islTree.insert(i, sub2) i = i + 1 for sub in parenttree.subtrees(): for sub2 in sub.subtrees(): if (len(sub2.leaves()) == 1 and dict[sub2.treeposition()] == 0 and dict[sub2.parent().treeposition()] == 0): dict[sub2.treeposition()] = 1 islTree.insert(i, sub2) i = i + 1 parsed_sent = islTree.leaves() # words = parsed_sent # print("ISL Tree\n") # print(islTree) # print("\n\n") # nltk.download('stopwords') # nltk.download('wordnet') # print() stop_words = set(stopwords.words("english")) lemmantizer = WordNetLemmatizer() # ps = PorterStemmer() lemmantized_words = [] for w in parsed_sent: # w = ps.stem(w) lemmantized_words.append(lemmantizer.lemmatize(w)) islSentence = "" for w in lemmantized_words: if w not in stop_words: islSentence += w islSentence += " " # islSentence += w # islSentence += " " # print("ISL Sentence\n") # print(islSentence) # print("\n\n") path = create_video(islSentence) return path
import stanza # Inizializing stanza # stanza.download('em') message = 'I live in Mexico' nlp = stanza.Pipeline('en', processors='tokenize,pos') doc = nlp(message) # set java path import os java_path = r'C:\Program Files\Java\jdk1.8.0_161\bin\java.exe' os.environ['JAVAHOME'] = java_path from nltk.parse.stanford import StanfordParser scp = StanfordParser( path_to_jar= 'C:/Users/carvi/Documents/Proyectos/NLP/stanford-parser-4.0.0/stanford-parser.jar', path_to_models_jar= 'C:/Users/carvi/Documents/Proyectos/NLP/stanford-parser-4.0.0/stanford-parser-4.0.0-models.jar' ) result = list(scp.raw_parse(message)) print(result[0]) from nltk.parse.corenlp import CoreNLPParser prueba = CoreNLPParser.parse(message) print(prueba)