# Path: matchUtils.py
# def matchPers(p1, rgdP, conf, score = None):
#     global svmModel, mt_tmp, SVMfeatures
#     if not svmModel:
#         if 'featureSet' in conf:
#             SVMfeatures = getattr(importlib.import_module('featureSet'), conf['featureSet'])
#             svmModel = svm_load_model('conf/person_' + conf['featureSet'] + '.model')
#         else:  #default
#             SVMfeatures = getattr(importlib.import_module('featureSet'), 'personDefault')
#             svmModel = svm_load_model('conf/personDefault.model')
#     nodeScore = nodeSim(p1, rgdP)
#     #pFam = conf['families'].find_one({ 'children': p1['_id']}) #find fam if p in 'children'
#     pFam = getFamilyFromChild(p1['_id'], conf['families'], conf['relations'])
#     #rgdFam = conf['match_families'].find_one({ 'children': rgdP['_id']})
#     rgdFam = getFamilyFromChild(rgdP['_id'], conf['match_families'], conf['match_relations'])
#     famScore = familySim(pFam, conf['persons'], rgdFam, conf['match_persons']) 
#     cand_matchtxt = mt_tmp.matchtextPerson(rgdP, conf['match_persons'], conf['match_families'], conf['match_relations'])
#     matchtxt = mt_tmp.matchtextPerson(p1, conf['persons'], conf['families'], conf['relations'])
#     cosScore = cos(matchtxt, cand_matchtxt)
#     if score is None and 'featureSet' in conf:  #score not used by deault
#         try:  #Lucene FIX
#             #from luceneUtils import search
#             import traceback
#             candidates = search(matchtxt, p1['sex'], ant=100, config=conf) #Lucene search
#             score = 0.0
#             for (kid,sc) in candidates:
#                 if str(kid) == str(rgdP['_id']):
#                     score = sc
#                     break
#         #except:  #use cos instead ?? if problems running Java in Bottle
#         except Exception, e:
#             exc_type, exc_value, exc_traceback = sys.exc_info()
#             traceback.print_exception(exc_type, exc_value, exc_traceback)
# 
#     feat = SVMfeatures(p1, rgdP, conf, score, nodeScore,
#                        famScore, cosScore, matchtxtLen=len(matchtxt.split()))
# #    p_labels, p_acc, p_vals = svm_predict([0],[feat],svmModel,options="-b 1 -q")
#     p_labels, p_acc, p_vals = svm_predict([0],[feat],svmModel,options="-b 1")
#     svmstat = p_vals[0][0]
#     # Use also score, NS, FS, and coscw? to determine status? See below!
#     status = 'Manuell'
#     if svmstat<0.1: status = 'EjMatch'
#     if svmstat>0.9: status = 'Match'
#     if status == 'Manuell' and nodeScore<-0.25: status = 'EjMatch'
#     matchdata = {}
#     matchdata['workid'] = p1['_id']
#     matchdata['pwork'] = p1
#     matchdata['matchid'] = rgdP['_id']
#     matchdata['pmatch'] = rgdP
#     matchdata['score'] = score
#     matchdata['cosScore'] = cosScore
#     matchdata['nodesim'] = nodeScore
#     matchdata['familysim'] = famScore
#     matchdata['svmscore'] = svmstat
#     matchdata['status'] = status
#     return matchdata
# 
# Path: dbUtils.py
# def getFamilyFromId(famId, familyDB, relationsDB):
#     """
#     Build a full family record (husb,wife,marriage,children)
#     from a familyId
#     """
#     famRecord = familyDB.find_one({'_id': famId})
#     if famRecord is None: return None
#     famRecord['husb'] = None
#     famRecord['wife'] = None
#     famRecord['children'] = []
#     for member in relationsDB.find({'famId': famRecord['_id']}):
#         if member['relTyp'] == 'child': famRecord['children'].append(member['persId'])
#         else: famRecord[member['relTyp']] = member['persId']
#         #if 'husb' in member: famRecord['husb'] = member['husb']
#         #elif 'wife' in member: famRecord['wife'] = member['wife']
#         #elif 'child' in member: famRecord['children'].append(member['child'])
#     return famRecord


from matchUtils import matchPers
from dbUtils import getFamilyFromId
import common
import logging
            if iuid in tmpdone: continue
            for chrgd in rgdchilds:
                rgdiuid = chrgd   #['I_uid']
                if rgdiuid in rgddone: continue
                #FIXif chtmp['sex'] != chrgd['sex']: continue  #FIX
                st = getMatchPers(iuid,rgdiuid, conf)
                if (st and (st['status'] in cond[0])):
                    stat.add(cond[1])
                    rgddone.append(rgdiuid)
                    tmpdone.append(iuid)
                    try:
                        st['sort'] = st['pwork']['birth']['date']
                    except:
                        try:
                            st['sort'] = st['pmatch']['birth']['date']
                        except:
                            st['sort'] = '0'
                    famMatchData['children'].append(st)
                    famMatchSummary['children'].add(st['status'])
                    break
    #Try to find match
    for chtmp in tmpchilds:
        iuid = chtmp #['I_uid']
        if iuid in tmpdone: continue
        for chrgd in rgdchilds:
            rgdiuid = chrgd #['I_uid']
            if rgdiuid in rgddone: continue
            st = getMatchPers(iuid,rgdiuid, conf)
            if (st and (st['status'] == 'split')): continue
            #FIXif chtmp['sex'] != chrgd['sex']: continue  #FIX

# Path: matchUtils.py
# def matchPers(p1, rgdP, conf, score = None):
#     global svmModel, mt_tmp, SVMfeatures
#     if not svmModel:
#         if 'featureSet' in conf:
#             SVMfeatures = getattr(importlib.import_module('featureSet'), conf['featureSet'])
#             svmModel = svm_load_model('conf/person_' + conf['featureSet'] + '.model')
#         else:  #default
#             SVMfeatures = getattr(importlib.import_module('featureSet'), 'personDefault')
#             svmModel = svm_load_model('conf/personDefault.model')
#     nodeScore = nodeSim(p1, rgdP)
#     #pFam = conf['families'].find_one({ 'children': p1['_id']}) #find fam if p in 'children'
#     pFam = getFamilyFromChild(p1['_id'], conf['families'], conf['relations'])
#     #rgdFam = conf['match_families'].find_one({ 'children': rgdP['_id']})
#     rgdFam = getFamilyFromChild(rgdP['_id'], conf['match_families'], conf['match_relations'])
#     famScore = familySim(pFam, conf['persons'], rgdFam, conf['match_persons']) 
#     cand_matchtxt = mt_tmp.matchtextPerson(rgdP, conf['match_persons'], conf['match_families'], conf['match_relations'])
#     matchtxt = mt_tmp.matchtextPerson(p1, conf['persons'], conf['families'], conf['relations'])
#     cosScore = cos(matchtxt, cand_matchtxt)
#     if score is None and 'featureSet' in conf:  #score not used by deault
#         try:  #Lucene FIX
#             #from luceneUtils import search
#             import traceback
#             candidates = search(matchtxt, p1['sex'], ant=100, config=conf) #Lucene search
#             score = 0.0
#             for (kid,sc) in candidates:
#                 if str(kid) == str(rgdP['_id']):
#                     score = sc
#                     break
#         #except:  #use cos instead ?? if problems running Java in Bottle
#         except Exception, e:
#             exc_type, exc_value, exc_traceback = sys.exc_info()
#             traceback.print_exception(exc_type, exc_value, exc_traceback)
# 
#     feat = SVMfeatures(p1, rgdP, conf, score, nodeScore,
#                        famScore, cosScore, matchtxtLen=len(matchtxt.split()))
# #    p_labels, p_acc, p_vals = svm_predict([0],[feat],svmModel,options="-b 1 -q")
#     p_labels, p_acc, p_vals = svm_predict([0],[feat],svmModel,options="-b 1")
#     svmstat = p_vals[0][0]
#     # Use also score, NS, FS, and coscw? to determine status? See below!
#     status = 'Manuell'
#     if svmstat<0.1: status = 'EjMatch'
#     if svmstat>0.9: status = 'Match'
#     if status == 'Manuell' and nodeScore<-0.25: status = 'EjMatch'
#     matchdata = {}
#     matchdata['workid'] = p1['_id']
#     matchdata['pwork'] = p1
#     matchdata['matchid'] = rgdP['_id']
#     matchdata['pmatch'] = rgdP
#     matchdata['score'] = score
#     matchdata['cosScore'] = cosScore
#     matchdata['nodesim'] = nodeScore
#     matchdata['familysim'] = famScore
#     matchdata['svmscore'] = svmstat
#     matchdata['status'] = status
#     return matchdata
# 
# Path: dbUtils.py
# def getFamilyFromId(famId, familyDB, relationsDB):
#     """
#     Build a full family record (husb,wife,marriage,children)
#     from a familyId
#     """
#     famRecord = familyDB.find_one({'_id': famId})
#     if famRecord is None: return None
#     famRecord['husb'] = None
#     famRecord['wife'] = None
#     famRecord['children'] = []
#     for member in relationsDB.find({'famId': famRecord['_id']}):
#         if member['relTyp'] == 'child': famRecord['children'].append(member['persId'])
#         else: famRecord[member['relTyp']] = member['persId']
#         #if 'husb' in member: famRecord['husb'] = member['husb']
#         #elif 'wife' in member: famRecord['wife'] = member['wife']
#         #elif 'child' in member: famRecord['children'].append(member['child'])
#     return famRecord


# Path: utils.py
from matchUtils import matchPers
from dbUtils import getFamilyFromId
import common
import logging

            if iuid in tmpdone: continue
            for chrgd in rgdchilds:
                rgdiuid = chrgd   #['I_uid']
                if rgdiuid in rgddone: continue
                #FIXif chtmp['sex'] != chrgd['sex']: continue  #FIX
                st = getMatchPers(iuid,rgdiuid, conf)
                if (st and (st['status'] in cond[0])):
                    stat.add(cond[1])
                    rgddone.append(rgdiuid)
                    tmpdone.append(iuid)
                    try:
                        st['sort'] = st['pwork']['birth']['date']
                    except:
                        try:
                            st['sort'] = st['pmatch']['birth']['date']
                        except:
                            st['sort'] = '0'
                    famMatchData['children'].append(st)
                    famMatchSummary['children'].add(st['status'])
                    break
    #Try to find match
    for chtmp in tmpchilds:
        iuid = chtmp #['I_uid']
        if iuid in tmpdone: continue
        for chrgd in rgdchilds:
            rgdiuid = chrgd #['I_uid']
            if rgdiuid in rgddone: continue
            st = getMatchPers(iuid,rgdiuid, conf)
            if (st and (st['status'] == 'split')): continue
            #FIXif chtmp['sex'] != chrgd['sex']: continue  #FIX

            mt = matchPers( conf['persons'].find_one({'_id': iuid}),