-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
39 lines (38 loc) · 1.78 KB
/
main.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
import re
import nltk
import pymorphy2
from nltk.parse.dependencygraph import DependencyGraph
text = 'Наступило лето. В саду поспела смородина. Даша и Алиса собирают ее в ведерко. Мама будет варить варенье. Дети будут пить чай с вареньем.'
sentences = re.compile(r'[.|!|?|…]').split(text.lower()) #разделяем текст на предложения
print(sentences)
tokens = nltk.word_tokenize(text.lower(), language="russian") #делим на слова и выполняем токенизацию
tagged = nltk.pos_tag(tokens)
print(tagged)
morph = pymorphy2.MorphAnalyzer(lang='ru')
gramm = {}
#определяем части речи
for word in tokens:
p = morph.parse(word)[0]
if word not in gramm:
gramm[word] = p.tag
print(gramm)
#предполагаем структуру предложений
grammar_all = nltk.CFG.fromstring("""
S -> NP VP | VP NP
VP -> V | V N | V N PP | V PP | V PP N | PP V | V V | V S
PP -> P N
NP -> N | N P N
V -> "наступило" | "поспела" | "собирают" | "будет" | "варить" | "будут" | "пить"
N -> "лето" | "даша" | "смородина" | "алиса" | "ее" | "ведерко" | "мама" | "варенье" | "дети" | "чай" | "саду" | "вареньем"
P -> "у" | "в" | "и" | "из" | "с"
""")
i = 0
#строим дерево структуры
while i < len(sentences):
structure = nltk.word_tokenize(sentences[i], language='russian')
parser = nltk.RecursiveDescentParser(grammar_all)
parses = list(parser.parse(structure))
for tree in parser.parse(structure):
tree.pretty_print()
# print(tree, "\n")
i = i + 1