Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnlpengine.py
More file actions
Latest commit
79 lines (63 loc) · 3.19 KB
/
Copy pathnlpengine.py
File metadata and controls
79 lines (63 loc) · 3.19 KB
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
# DESC: NLPEngine Python class - run nlp.exe analyzers from Python
# NLPEngine Python class that uses nlp.exe
importshutil
importsubprocess
importos
classNLPEngine:
def__init__(self, engineDir, analyzersDir ):
self.engineDir=engineDir
self.analyzersDir=analyzersDir
defanalyzerPath(self, analyzerFolder):
returnos.path.join(self.analyzersDir, analyzerFolder)
defkbPath(self, analyzerFolder):
returnos.path.join(self.analyzerPath(analyzerFolder), "kb", "user")
defspecPath(self, analyzerFolder):
returnos.path.join(self.analyzerPath(analyzerFolder), "spec")
defoutputDir(self, analyzerFolder, textPath):
returnos.path.join(self.analyzerPath(analyzerFolder), "input", textPath+"_log")
defoutputFileContents(self, analyzerFolder, filename, outputFile):
outputPath=os.path.join(self.outputDir(analyzerFolder, filename), outputFile)
withopen(outputPath, "r") asfile:
contents=file.read()
returncontents
definputFileDir(self, analyzerFolder, textPath):
returnos.path.join(self.analyzerPath(analyzerFolder), "input", textPath)
defanalyzeFile(self, analyzerFolder, textPath, dev=False):
self.clearLogFiles(analyzerFolder)
analyzerPath=os.path.join(self.analyzersDir, analyzerFolder)
textPath=os.path.join(analyzerPath, "input", textPath)
try:
executable_path=os.path.join(self.engineDir, "nlp.exe")
args= [executable_path, "-ANA", analyzerPath, "-WORK", self.engineDir, textPath]
ifdev:
args.append("-DEV")
withopen("output.txt", "w") asoutput_file, open("errors.txt", "w") aserror_file:
subprocess.run(args, stdout=output_file, stderr=error_file, text=True)
exceptsubprocess.CalledProcessErrorase:
print(f"An error occurred: {e}")
return
defanalyzeStr(self, analyzerFolder, filename, textStr):
inputPath=self.inputFileDir(analyzerFolder,filename)
withopen(inputPath, "w") asinput_file:
input_file.write(textStr)
self.analyzeFile(analyzerFolder, filename, False)
defisAnalyzerFolder(self, analyzerFolder):
required_folders= ['spec', 'input', 'kb/user']
forfolderinrequired_folders:
ifnotos.path.isdir(os.path.join(self.analyzersDir, analyzerFolder, folder)):
returnFalse
returnTrue
defclearLogFiles(self, analyzerFolder):
logPath=os.path.join(os.path.join(self.analyzersDir,analyzerFolder), "input")
forroot, dirs, filesinos.walk(logPath):
fordirindirs:
ifdir.endswith("_log"):
shutil.rmtree(os.path.join(root, dir))
defcreateInputDir(self, analyzer, inputFolder, clearFolder=True):
inputPath=os.path.join(self.analyzerPath(analyzer), "input", inputFolder)
ifclearFolderandos.path.exists(inputPath):
shutil.rmtree(inputPath)
shutil.os.makedirs(inputPath)
ifnotos.path.exists(inputPath):
os.makedirs(inputPath)
returninputPath