Datasets:

ArXiv:
License:
File size: 5,637 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/*

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */
package tool.sieves;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import tool.util.Concept;
import tool.util.Ling;
import tool.util.Terminology;
import tool.util.Util;

/**

 *

 * @author

 */
public class PartialMatchNCBISieve {
    
    public static String apply(Concept concept) {
        String name = concept.getName();
        String[] nameTokens = name.split("\\s+");
        return partialMatch(name, nameTokens);
    }      
        
    private static String partialMatch(String phrase, String[] phraseTokens) {
        List<String> partialMatchedPhrases = new ArrayList<>();
        Map<Integer, Map<String, Integer>> candidateCuiDataMap = init();
        
        for (String phraseToken : phraseTokens) {
            if (Ling.getStopwordsList().contains(phraseToken))
                continue;
            List<String> candidatePhrases = null;
            int map = -1;
            
            if (Sieve.getTrainingDataTerminology().getTokenToNameListMap().containsKey(phraseToken)) {
                candidatePhrases = new ArrayList<>(Sieve.getTrainingDataTerminology().getTokenToNameListMap().get(phraseToken));
                map = 2;
            }
            else if (Sieve.getStandardTerminology().getTokenToNameListMap().containsKey(phraseToken)) {
                candidatePhrases = new ArrayList<>(Sieve.getStandardTerminology().getTokenToNameListMap().get(phraseToken));
                map = 3;
            }
            
            if (candidatePhrases == null)
                continue;
                        
            candidatePhrases.removeAll(partialMatchedPhrases);
            
            candidateCuiDataMap = ncbiPartialMatch(phrase, candidatePhrases, partialMatchedPhrases, map == 2 ? Sieve.getTrainingDataTerminology() : Sieve.getStandardTerminology(), candidateCuiDataMap);
        }        
        return !candidateCuiDataMap.get(1).isEmpty() ? getCui(candidateCuiDataMap.get(1), candidateCuiDataMap.get(2)) : "";
    }     
    
    private static Map<Integer, Map<String, Integer>> init() {
        Map<Integer, Map<String, Integer>> candidateCuiDataMap = new HashMap<>();
        candidateCuiDataMap.put(1, new HashMap<String, Integer>());
        candidateCuiDataMap.put(2, new HashMap<String, Integer>());
        return candidateCuiDataMap;
    }    
    
    private static Map<Integer, Map<String, Integer>> ncbiPartialMatch(String phrase, List<String> candidatePhrases, List<String> partialMatchedPhrases, Terminology terminology, Map<Integer, Map<String, Integer>> cuiCandidateDataMap) {
        Map<String, Integer> cuiCandidateMatchingTokensCountMap = cuiCandidateDataMap.get(1);
        Map<String, Integer> cuiCandidateLengthMap = cuiCandidateDataMap.get(2);
        
        for (String candidatePhrase : candidatePhrases) {
            partialMatchedPhrases = Util.setList(partialMatchedPhrases, candidatePhrase);

            int count = Ling.getMatchingTokensCount(phrase, candidatePhrase);
            int length = candidatePhrase.split("\\s+").length;
            String cui = terminology.getNameToCuiListMap().get(candidatePhrase).get(0);

            if (cuiCandidateMatchingTokensCountMap.containsKey(cui)) {
                int oldCount = cuiCandidateMatchingTokensCountMap.get(cui);
                if (oldCount < count) {
                    cuiCandidateMatchingTokensCountMap.put(cui, count);
                    cuiCandidateLengthMap.put(cui, length);
                }
                continue;
            }

            cuiCandidateMatchingTokensCountMap.put(cui, count);
            cuiCandidateLengthMap.put(cui, length);
        }                    
        
        cuiCandidateDataMap.put(1, cuiCandidateMatchingTokensCountMap);
        cuiCandidateDataMap.put(2, cuiCandidateLengthMap);
        return cuiCandidateDataMap;
    }    
    
    private static String getCui(Map<String, Integer> cuiCandidateMatchedTokensCountMap, Map<String, Integer> cuiCandidateLengthMap) {
        String cui = "";
        int maxMatchedTokensCount = -1;
        Map<Integer, List<String>> matchedTokensCountCuiListMap = new HashMap<>();
        for (String candidateCui : cuiCandidateMatchedTokensCountMap.keySet()) {
            int matchedTokensCount = cuiCandidateMatchedTokensCountMap.get(candidateCui);
            if (matchedTokensCount >= maxMatchedTokensCount) {
                maxMatchedTokensCount = matchedTokensCount;                
                
                List<String> cuiList = matchedTokensCountCuiListMap.get(matchedTokensCount);
                if (cuiList == null) 
                    matchedTokensCountCuiListMap.put(matchedTokensCount, cuiList = new ArrayList<>());
                cuiList = Util.setList(cuiList, candidateCui);
            }
        }
        List<String> candidateCuiList = matchedTokensCountCuiListMap.get(maxMatchedTokensCount);
        if (candidateCuiList.size() == 1)
            return candidateCuiList.get(0);
        else {
            int minCandidateLength = 1000;
            for (String candidateCui : candidateCuiList) {
                int length = cuiCandidateLengthMap.get(candidateCui);
                if (length < minCandidateLength) {
                    minCandidateLength = length;
                    cui = candidateCui;
                }
            }
        }        
        return cui;
    }            
    
}