# Path: wikihead.py
# def headings():
#     xml = requests.get("http://en.wikipedia.org/wiki/Special:Export/List_of_sovereign_states").content
#     root = lxml.etree.fromstring(xml)
#     text = root.xpath("//*[local-name()='text']")[0].text
# 
#     wrapped_text = u"".join([u"<html>", text, u"</html>"])
#     w_root = lxml.html.fromstring(wrapped_text)
#     countries = w_root.xpath("//span/@id")
# 
#     builder = {}
#     for country in countries:
#         html = requests.get("http://en.wikipedia.org/wiki/%s" % country).content
#         root = lxml.html.fromstring(html)
#         cats = {2: root.xpath("//div[@id='mw-content-text']/h2/span/@id"),
#                 3: root.xpath("//div[@id='mw-content-text']/h3/span/@id")}
#         if cats == {2: [], 3: []}:
#             print repr(country)
#             raise RuntimeError()
#         builder[country] = cats
#     return builder


import codecs
import sys
import orm
from wikihead import headings
                  'is_number': False}


def exact_match(level, header, country):
    matches = [x.lower() for x in country[level] if x == header]
    if len(matches) > 1:
        print matches, "\n\n\n\n"
    if matches:
        return matches[0]


def partial_match(level, header, country):
    matches = []
    for cand_header in country[level]:
        if header in cand_header.lower():
            matches.append(cand_header)
    if len(matches) > 1:
        print matches, "\n\n\n\n"
    if matches:
        return matches[0]


def best_match(header, country):
    return exact_match(2, header, country) or \
        exact_match(3, header, country) or \
        partial_match(2, header, country) or \
        partial_match(3, header, country)


wikibase = "http://en.wikipedia.org/wiki/%s#%s"

# Path: wikihead.py
# def headings():
#     xml = requests.get("http://en.wikipedia.org/wiki/Special:Export/List_of_sovereign_states").content
#     root = lxml.etree.fromstring(xml)
#     text = root.xpath("//*[local-name()='text']")[0].text
# 
#     wrapped_text = u"".join([u"<html>", text, u"</html>"])
#     w_root = lxml.html.fromstring(wrapped_text)
#     countries = w_root.xpath("//span/@id")
# 
#     builder = {}
#     for country in countries:
#         html = requests.get("http://en.wikipedia.org/wiki/%s" % country).content
#         root = lxml.html.fromstring(html)
#         cats = {2: root.xpath("//div[@id='mw-content-text']/h2/span/@id"),
#                 3: root.xpath("//div[@id='mw-content-text']/h3/span/@id")}
#         if cats == {2: [], 3: []}:
#             print repr(country)
#             raise RuntimeError()
#         builder[country] = cats
#     return builder


# Path: wikipedia.py
import codecs
import sys
import orm
from wikihead import headings

                  'is_number': False}


def exact_match(level, header, country):
    matches = [x.lower() for x in country[level] if x == header]
    if len(matches) > 1:
        print matches, "\n\n\n\n"
    if matches:
        return matches[0]


def partial_match(level, header, country):
    matches = []
    for cand_header in country[level]:
        if header in cand_header.lower():
            matches.append(cand_header)
    if len(matches) > 1:
        print matches, "\n\n\n\n"
    if matches:
        return matches[0]


def best_match(header, country):
    return exact_match(2, header, country) or \
        exact_match(3, header, country) or \
        partial_match(2, header, country) or \
        partial_match(3, header, country)


wikibase = "http://en.wikipedia.org/wiki/%s#%s"

headinglist = headings()