-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
169 lines (145 loc) · 6.37 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# Press the green button in the gutter to run the script.
from hazm.dependency_parser import SpacyDependencyParser
from hazm import POSTagger, Lemmatizer, DependencyParser, word_tokenize
from Requirement import Requirement
from ClassDiagramExtractor import ClassDiagramExtractor, ExtractorEvaluator
from Diagram import ClassDiagram
from Extractors import HazmExtractor, StanzaExtractor
import json
from hazm.utils import verbs_list
import subprocess
import os
def printGraph(dg):
for node in dg.nodes.values():
print(node)
# print('({h[0]}, {h[1]}), {r}, ({d[0]}, {d[1]})'.format(h=head, r=rel, d=dep))
# print(f'head:{head[0]}, {head[1]}')
# print(rel)
# print(f'dep:{dep[0]}, {dep[1]}')
# print('-----------------------------------')
def save_diagram_svg(diagram, name):
# Save the mermaid markdown to a temporary file
with open('temp.mmd', 'w', encoding='utf-8') as f:
f.write(diagram.to_mermaid())
# Run mermaid-cli to generate the SVG
subprocess.run(['mmdc', '-i', 'temp.mmd', '-o', f'./dataset/output/{name}.svg'],shell=True)
# Remove the temporary markdown file
# subprocess.run(['rm', 'temp.mmd'],shell=True)
os.remove('temp.mmd')
print(f"SVG file saved as {name}.svg")
def print_for_debug(extractor):
classes = sorted(extractor.diagram.classes, key=lambda x: x.text)
for element in classes:
print(element.text, element.node.meta_rel if element.node else '', element.count, element.frequency)
print('attrs:')
for attr in element.attributes:
print(attr.text, attr.node.meta_rel)
print('operations:')
for operation in element.operations:
print(operation.text)
print('---------------------------')
# print('\n\nBase Relations:')
# for relation in extractor.diagram.base_relations:
# print(relation.sentence.text)
# print(
# f"{relation.source.text},{relation.relation_title.text},{relation.target.text if relation.target else 'None'} {relation.target_node.text if relation.target_node else 'None'}")
# print('----------------')
print('\n\nRelations:')
for relation in extractor.diagram.relations:
# print(relation.sentence.text)
print(
f"{relation.source.text},{relation.relation_type},{relation.target.text},{relation.label if relation.label else 'None'}")
print('----------------')
def extract_and_evaluate_from_file(name, extractor, print_elements, save_diagram):
try:
with open(f'./dataset/refined-requirements-1/{name}.txt', 'r', encoding='utf-8') as file:
text = file.read()
# text = "بازیکنها اطلاعاتی مانند نام، ژانر، جنسیت، سطح و سلاح مورد استفاده خود دارند."
test_req = Requirement(text, extractor.extract)
test_extractor = ClassDiagramExtractor(test_req)
test_extractor.extract_diagram()
print(name)
print("\n")
if print_elements:
print_for_debug(test_extractor)
if save_diagram:
save_diagram_svg(test_extractor.diagram,name)
# except FileNotFoundError:
# print("File not Found")
except json.JSONDecodeError:
print("Error decoding JSON")
try:
with open(f'./dataset/design-elements/{name}.json', 'r', encoding='utf-8') as json_file:
elements = json.load(json_file)
standard_diagram = ClassDiagram(elements)
evaluator = ExtractorEvaluator(test_extractor.diagram, standard_diagram)
print(evaluator.evaluate_classes())
print(evaluator.evaluate_attributes())
except FileNotFoundError:
print("File not Found")
except json.JSONDecodeError:
print("Error decoding JSON")
print("//////////////////////////////////////////////////////")
def print_requirement_data(name, extractor):
try:
with open(f'./dataset/refined-requirements-1/{name}.txt', 'r', encoding='utf-8') as file:
text = file.read()
# text = "بازیکنها اطلاعاتی مانند نام، ژانر، جنسیت، سطح و سلاح مورد استفاده خود دارند."
test_req = Requirement(text, extractor.extract)
print(name)
print(f"sentences: {test_req.get_sentences_count()} tokens: {test_req.get_tokens_count()}")
except FileNotFoundError:
print("File not Found")
except json.JSONDecodeError:
print("Error decoding JSON")
if __name__ == '__main__':
lemmatizer = Lemmatizer()
tagger = POSTagger(model='pos_tagger.model')
parser = DependencyParser(tagger=tagger, lemmatizer=lemmatizer)
spacy_parser = SpacyDependencyParser(tagger=tagger, lemmatizer=lemmatizer,
model_file='./spacy_dependency_parser',
working_dir='./spacy_dependency_parser')
file_names = [
"ATM",
"Course enrollment",
"Restaurant",
"Game",
"Rented-car gallery",
"File manager",
# "Video rental",
"Football team",
"Music band",
"Musical store",
"Cinema",
"Company",
"Fitness center",
]
new_file_names = [
"Academic program",
# "Monitoring Pressure",
# "Airport",
"Timbered house",
"Dental clinic",
"Circe",
"Online shopping",
"Police system",
"Hospital",
"Library"
]
hazm_extractor = HazmExtractor(spacy_parser, lemmatizer, with_ezafe_tag=True)
# stanza_extractor = StanzaExtractor()
for file in ['test']:
extract_and_evaluate_from_file(file, hazm_extractor, True, False)
# print_requirement_data(file, hazm_extractor)
# text = "کاربر ماشین میخرد. جرخ از قسمتهای ماشین است. چرخ برند و سایز دارد."
# test_req = Requirement(text, hazm_extractor.extract)
# test_extractor = ClassDiagramExtractor(test_req)
# test_extractor.extract_class_names()
# test_extractor.extract_attributes()
# test_extractor.extract_relations()
# test_extractor.extract_operations()
# print_for_debug(test_extractor)
# standard_diagram = ClassDiagram(elements)
# evaluator = ExtractorEvaluator(test_extractor.diagram, standard_diagram)
# print(evaluator.evaluate_classes())
# print(evaluator.evaluate_attributes())