refactor filename_matching
@@ -44,43 +44,20 @@ class KeywordsMatching(object):
return new_matcher
def matching_subjects_and_filenames(self):
- list_final = []
- name_list_final = []
- for p in self.list_paths:
- for filename in os.listdir(p):
- if any(c not in filename for c in self.list_contain):
- continue
- if any(c in filename for c in self.list_not_contain):
- continue
- full_file_name = os.path.join(p, filename)
- list_final.append(full_file_name)
- name_list_final.append(self.extract_subject_id_from(filename))
+ path_file=[(p,filename) for p in self.list_paths for filename in os.listdir(p)]
+ func_match = lambda x: all(c in x[1] for c in self.list_contain) and
+ not any(c in x[1] for c in self.list_not_contain)
+ matching_path_file=filter(func_match,path_file)
+ list_final=[os.path.join(p,filename) for p,filename in matching_path_file]
+ name_list_final=[self.extract_subject_id_from(filename) for p,filename in matching_path_file]
return list_final, name_list_final
def extract_subject_id_from(self, filename):
path, name, ext = util.split_filename(filename)
-
- index_constraint = []
- length_constraint = []
- for c in self.list_contain:
- index_constraint.append(name.find(c))
- length_constraint.append(len(c))
- sort_indices = np.argsort(index_constraint)
-
- index_init = 0 if index_constraint[sort_indices[0]] > 0 else \
- length_constraint[sort_indices[0]]
- name_pot = []
- index_start = 0 if index_constraint[sort_indices[0]] > 0 else 1
- for i in range(index_start, len(self.list_contain)):
- name_pot_temp = name[index_init: index_constraint[sort_indices[i]]]
- name_pot_temp = ''.join(x for x in name_pot_temp if x.isalnum())
- if name_pot_temp is not '':
- name_pot.append(name_pot_temp)
- index_init = index_constraint[sort_indices[i]] + \
- len(self.list_contain[sort_indices[i]])
-
- name_temp = name[index_init:]
- name_temp = ''.join(x for x in name_temp if x.isalnum())
- if name_temp is not '':
- name_pot.append(name_temp)
- return name_pot
+ # split name into parts that might be the subject_id
+ noncapturing_regex_delimiters=['(?:'+re.escape(c)+')' for c in self.list_contain]
+ potential_names=re.split('|'.join(noncapturing_regex_delimiters),name)
+ # filter out non-alphanumeric characters and blank strings
+ potential_names=[re.sub(r'\W+', '', name) for name in potential_names)
+ potential_names=[name for name in potential_names if name is not '']
+ return potential_names
\ No newline at end of file
