コード例 #1
0
def test_return_values():
    # test() behaviour
    assert sm.test("*.py", "hello.py") is True
    assert sm.test("{}.py", "hello.py") is True
    assert sm.test("*.py", "hello.__") is False
    assert sm.test("{}.py", "hello.__") is False

    # match() behaviour
    assert sm.match("*.py", "hello.py") == {}
    assert sm.match("{}.py", "hello.py") == {0: "hello"}
    assert sm.match("*.py", "hello.__") is None
    assert sm.match("{}.py", "hello.__") is None
コード例 #2
0
def extract_keyword(query):
    # regex from narrow to broader matches
    query = query.lower()

    match = simplematch.match("who is {query}", query) or \
            simplematch.match("what is {query}", query) or \
            simplematch.match("when is {query}", query) or \
            simplematch.match("tell me about {query}", query)
    if match:
        match = match["query"]
    else:
        # let's try to extract the best keyword and use it as query
        kwords = rake_keywords(query)
        match = kwords[0][0]
    return match
コード例 #3
0
def test_readme_example_basic_usage():
    result = sm.match(
        pattern="Invoice*_{year}_{month}_{day}.pdf",
        string="Invoice_RE2321_2021_01_15.pdf",
    )
    assert result == {"year": "2021", "month": "01", "day": "15"}
    assert sm.test("ABC-{value:int}", "ABC-13")
コード例 #4
0
def main():
    pattern = input("Enter a pattern (or 'q' to quit):\n")
    if pattern != 'q':
        word = input("Enter a word:\n")
        if (simplematch.match(pattern, word)):
            print("It's a match.")
        else:
            print("They don't match.")
        main()
コード例 #5
0
def getMatches(pattern: str, lines: list) -> list:
    """Returns all the lines that match the given mattern"""
    matches = []
    for line in lines:
        line = str(line).strip('\n')
        if simplematch.match(pattern, line):
            matches.append(line)

    return matches
コード例 #6
0
    def calc_intents(self, query):
        for intent_name, regexes in self.intent_samples.items():
            regexes = sorted(regexes, key=len, reverse=True)
            for r in regexes:
                penalty = 0
                if "*" in r:
                    # penalize wildcards
                    penalty = 0.15

                entities = simplematch.match(r, query, case_sensitive=True)
                if entities is not None:
                    for k, v in entities.items():
                        if k not in self.entity_samples:
                            # penalize unregistered entities
                            penalty += 0.1
                        elif str(v) not in self.entity_samples[k]:
                            # penalize unknown samples
                            penalty += 0.05
                    yield {
                        "entities": entities or {},
                        "conf": 1 - penalty,
                        "name": intent_name
                    }
                    break

                entities = simplematch.match(r, query, case_sensitive=False)
                if entities is not None:
                    # penalize case mismatch
                    penalty += 0.05
                    for k, v in entities.items():
                        if k not in self.entity_samples:
                            # penalize unregistered entities
                            penalty += 0.1
                        elif str(v) not in self.entity_samples[k]:
                            # penalize unknown samples
                            penalty += 0.05
                    yield {
                        "entities": entities or {},
                        "conf": 1 - penalty,
                        "name": intent_name
                    }
                    break
コード例 #7
0
def test_case_sensitive():
    assert sm.test("Hello {}", "Hello World")
    assert not sm.test("hello {}", "Hello World")

    assert sm.test("Hello {}", "Hello World", case_sensitive=False)
    assert sm.test("hello {}", "Hello World", case_sensitive=False)

    # keep capture group names
    assert sm.match("HELLO {PlAnEt}", "Hello Earth", case_sensitive=False) == {
        "PlAnEt": "Earth"
    }
コード例 #8
0
def test_simple_matching():
    # should return the right match dict
    assert sm.match("{folder}/{filename}.py", "home/hello.py") == {
        "folder": "home",
        "filename": "hello",
    }

    # should return None object if no match
    assert sm.match("{folder}/{filename}?{params}", "hello.js?p=1") is None

    # should match strings with . (dot) and ? (question mart) sights
    assert sm.match("{folder}/{filename}?{params}",
                    "home/hello.js?p=1") == dict(folder="home",
                                                 filename="hello.js",
                                                 params="p=1")

    # should match wild cards
    assert sm.match("*/{filename}",
                    "home/hello.js") == dict(filename="hello.js")

    # NOT: should tolerate *{param} syntax - it acts as */{param}
    # this is different from the easypattern library!
    assert not sm.match("*{filename}",
                        "home/hello.js") == dict(filename="hello.js")

    # should save wild cards
    assert sm.match("{}/{filename}?{}", "www.site.com/home/hello.js?p=1") == {
        0: "www.site.com/home",
        1: "p=1",
        "filename": "hello.js",
    }
コード例 #9
0
def test_register_type():
    def mood_detect(smiley):
        moods = {
            ":)": "good",
            ":(": "bad",
            ":/": "sceptic",
        }
        return moods.get(smiley, "unknown")

    sm.register_type("smiley", r":[\(\)\/]", mood_detect)

    assert sm.match("I'm feeling {mood:smiley} *",
                    "I'm feeling :) today!") == {
                        "mood": "good"
                    }
コード例 #10
0
def match_infobox_field(query):
    query = query.lower()

    # known for
    match = simplematch.match("what is {query} known for", query) or \
            simplematch.match("what is {query} famous for", query)
    if match:
        return match["query"], "known for"

    # resting place
    match = simplematch.match("where is {query} resting place*", query) or \
            simplematch.match("where is {query} resting buried*", query)
    if match:
        return match["query"], "resting place"

    # birthday
    match = simplematch.match("when was {query} born*", query) or \
            simplematch.match("when is {query} birth*", query)
    if match:
        return match["query"], "born"

    # death
    match = simplematch.match("when was {query} death*", query) or \
            simplematch.match("when did {query} die*", query) or \
            simplematch.match("what was {query} *death", query) or \
            simplematch.match("what is {query} *death", query)

    if match:
        return match["query"], "died"

    # children
    match = simplematch.match("how many children did {query} have*",
                              query) or \
            simplematch.match("how many children does {query} have*",
                              query)
    if match:
        return match["query"], "children"

    # alma mater
    match = simplematch.match("what is {query} alma mater", query) or \
            simplematch.match("where did {query} study*", query)
    if match:
        return match["query"], "alma mater"

    return None, None
コード例 #11
0
 def _parse_tag(self, s):
     """ parse a tag definition and return a tuple (name, color) """
     result = sm.match("{name} ({color})", s)
     if not result:
         return s, "none"
     return result["name"], result["color"].lower()
コード例 #12
0
def test_unnamed_wildcards():
    assert sm.match("{} sees {}", "Tim sees Jacob") == {0: "Tim", 1: "Jacob"}
コード例 #13
0
def test_readme_example_opener():
    assert sm.match("He* {planet}!", "Hello World!") == {"planet": "World"}
    assert sm.match("It* {temp:float}°C *", "It's -10.2°C outside!") == {
        "temp": -10.2
    }