]>
Softwares of Agnibho - ddstorm.git/blob - index.py
4 This module handles the upstream ancestors of symptoms.
6 The symptoms can be classified in different levels, with the more
7 generalized symtoms at upstream and the more specialized symptoms at
11 'Acute peritonitis' is a special type of 'peritonitis'
12 Hence 'peritionitis' will be at upstream and 'acute peritonitis' will be
16 Copyright (c) 2015 Agnibho Mondal
19 This file is part of DDStorm.
21 DDStorm is free software: you can redistribute it and/or modify
22 it under the terms of the GNU General Public License as published by
23 the Free Software Foundation, either version 3 of the License, or
24 (at your option) any later version.
26 DDStorm is distributed in the hope that it will be useful,
27 but WITHOUT ANY WARRANTY; without even the implied warranty of
28 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 GNU General Public License for more details.
31 You should have received a copy of the GNU General Public License
32 along with DDStorm. If not, see <http://www.gnu.org/licenses/>.
37 from fnmatch
import fnmatch
43 ''' Provides an index of the upstream ancestors of symptoms '''
44 def __init__(self
, conf
=False):
46 Initiate the index object. Accepts a Conf object as an
58 Compile the text index files to a application usable format.
60 # Loop over all files under index_path
61 for path
, subdirs
, files
in os
.walk(self
.conf
.get("index_path")):
63 # Only files with *.txt suffix
64 if(fnmatch(name
, "*.txt")):
65 with
open(self
.conf
.get("index_path")+name
, "r") as f
:
66 # Two dimensional list buffer to hold the index
70 # Loop over all lines as separate entries
72 # Ignore commnents starting with #
73 line
=line
.rstrip().split("#")[0]
77 # Find number of leading whitespaces
78 ws
=len(line
)-len(line
.lstrip())
80 line
=line
.lstrip().capitalize()
82 # No leading whitespace means a top level entry i.e. no upstream ancestor
84 # Empty the buffer and add the entry
86 buff
[0].append(line
.lstrip())
87 # Reset the whitespace index
90 # If leading whitespace > indexed whitespace, the entry is a subcategory of previous entry
92 # Append entry to buffer as new item
93 buff
[0].append(line
.lstrip())
94 # Record leading whitespace to buffer
96 # Copy buffer to data list
97 self
.data
[buff
[0][-1]]=list(reversed(buff
[0]))
98 # If leading whitespace == indexed whitespace, the entry is at the same level with the previous entry
99 elif(ws
==buff
[1][-1]):
100 # Append entry to last item of buffer
101 buff
[0][-1]=line
.lstrip()
103 # Copy buffer to data list
104 self
.data
[buff
[0][-1]]=list(reversed(buff
[0]))
105 # If leading whitespace < indexed whitespace, the entry is at an upper category than the previous entry
106 elif(ws
<buff
[1][-1]):
107 # Loop over the buffer in reverse
108 for i
in reversed(buff
[1]):
109 # Discard all lower category items
113 #Append the entry to buffer
115 buff
[0].append(line
.lstrip())
118 # Copy buffer to data list
119 self
.data
[buff
[0][-1]]=list(reversed(buff
[0]))
121 def upstream(self
, name
):
123 Return the upstream list of a symptom
126 name - the name of the symptom as string
129 List of strings containing the upstream items
132 if name
in self
.data
:
133 return self
.data
[name
]
138 ''' Return all indexed symptoms name '''
139 return list(self
.data
.keys())
143 Prints upstream items of a symptom provided as command line
145 If no argument provided, returns a list of all indexed symptoms.
149 print(i
.upstream(sys
.argv
[1]))
153 if(__name__
=="__main__"):