Beispiel #1
0
def merge_dicts(ionization, isotopes):
    element_data = dict()

    # Processes Ionization Levels
    for elem, ion in ionization.iteritems():
        symbol = SYMBOL_MAPPING.get(elem.lower(), elem.lower())
        ion = lower_keys(ion)
        ion['protons'] = ion['atomic number']
        ion = dict(sorted(ion.items()))
        element_data[symbol] = ion

    # Process Isotopes
    for elem, isotope in isotopes.iteritems():
        symbol = SYMBOL_MAPPING.get(elem.lower(), elem.lower())
        isotope = lower_keys(isotope)
        isotope = dict(sorted(isotope.items()))
        isotope["isotopes"] = isotope.pop("isotopes")
        if "standard atomic weight" in isotope:
            set_value(
                element_data[symbol],
                ["atomic weight", "atomic mass", "mass", "amu", "weight"],
                isotope["standard atomic weight"])

        for each in isotope["isotopes"]:
            each = lower_keys(each)
            set_value(
                each,
                ["atomic weight", "atomic mass", "mass", "amu", "weight"],
                each["relative atomic mass"])
            mass_number = each["mass number"]
            if symbol in element_data:
                element_data[symbol][mass_number] = dict(sorted(each.items()))
    return element_data
Beispiel #2
0
    def search(self, query):
        numbers = filter_numbers(query.lower())
        keywords = [numbers] + [
            SYMBOL_MAPPING.get(key, key)
                for key in indicoio.keywords(" ".join(map(preformat, query.lower().split(" "))), top_n=100).keys()
        ]
        keywords = " ".join(keywords)
        best = 0
        results = []
        for key in KEYWORDS:
            # ratio = fuzz.token_set_ratio(keywords, key)
            ratio = 0
            for word in keywords.split(" "):
                keys = key.split(" ")
                if word in keys:
                    ratio += 1

            if ratio > best:
                best = ratio
                results = []
                results.append((ratio, key, data.ELEMENT_DATA[key]))
            elif ratio == best:
                results.append((ratio, key, data.ELEMENT_DATA[key]))

        if len(results) > 3:
            for i in xrange(len(results)):
                results[i] = fuzz.ratio(keywords, results[i][1]), results[i][1], results[i][2]

        self.respond([{result[1]: result[2]} for result in sorted(results, reverse=True)[:5]])
Beispiel #3
0
def preformat(entry):
    entry = entry.strip()
    entry = SYMBOL_MAPPING.get(entry, entry)
    result = spelling(entry)
    if not result:
        return ""
    return result
Beispiel #4
0
def create_trie():
    dictionary = load(os.path.join(OUTPUT_DIR, "element_data.p"))
    # read dictionary file into a trie
    trie = TrieNode()
    words = []
    for key in dictionary:
        words += key.split(" ")
    for key in set(words):
        trie.insert(key)
    for key in SYMBOL_MAPPING.keys():
        trie.insert(key)

    cache(trie, os.path.join(OUTPUT_DIR, "trie.p"))