|
| 1 | +from webstruct.fuzzymatch import FuzzyMatchClassifier, SPACES_SRE, merge_bio_tags |
| 2 | +from webstruct import HtmlTokenizer |
| 3 | +from webstruct.utils import html_document_fromstring |
| 4 | + |
| 5 | +def test_fuzzy_assign_bio_tags(): |
| 6 | + html = """<div id="intro_contact"> |
| 7 | + 013-Witgoedreparaties.nl<br/>Postbus 27<br/> |
| 8 | + 4500 AA Oostburg<br/><a href="mailto:[email protected]">[email protected]</a><br/> tel: 013-5444999<br/></div></div> |
| 9 | + </div> |
| 10 | + """ |
| 11 | + html_tokenizer = HtmlTokenizer() |
| 12 | + html_tokens, tags = html_tokenizer.tokenize_single(html_document_fromstring(html)) |
| 13 | + pattern = ur'(^|{0})Postbus.*?Oostburg({0}|$)'.format(SPACES_SRE) |
| 14 | + choices = ['Postbus 22 4500AA Oostburg'] |
| 15 | + |
| 16 | + clf = FuzzyMatchClassifier(entity='ADDR', pattern=pattern, choices=choices) |
| 17 | + tags = clf.predict([html_tokens]) |
| 18 | + |
| 19 | + assert tags[0] == ['O', 'B-ADDR', 'I-ADDR', 'I-ADDR', 'I-ADDR', 'I-ADDR', 'O', 'O', 'O'] |
| 20 | + |
| 21 | + html = """<div id="intro_contact"> |
| 22 | + 013-Witgoedreparaties.nl<br/>Postbus 27<br/> |
| 23 | + 4500 AA Oostburg<br/>The Netherlands</br><a href="mailto:[email protected]">[email protected]</a><br/> tel: 013-5444999<br/></div></div> |
| 24 | + </div> |
| 25 | + """ |
| 26 | + html_tokens, tags = html_tokenizer.tokenize_single(html_document_fromstring(html)) |
| 27 | + pattern = ur'(^|{0})Postbus.*?Oostburg{0}((the\s+)?ne(d|th)erlands?)?'.format(SPACES_SRE) |
| 28 | + choices = ['Postbus 22 4500AA Oostburg', 'Postbus 22 4500AA Oostburg the netherlands'] |
| 29 | + |
| 30 | + clf = FuzzyMatchClassifier(entity='ADDR', pattern=pattern, choices=choices) |
| 31 | + tags = clf.predict([html_tokens]) |
| 32 | + assert tags[0] == ['O', 'B-ADDR', 'I-ADDR', 'I-ADDR', 'I-ADDR', 'I-ADDR', 'I-ADDR', 'I-ADDR', 'O', 'O', 'O'] |
| 33 | + |
| 34 | + html = """<title>013-witgoedreparaties.nl | 013-witgoedreparaties.nl | title </title>""" |
| 35 | + html_tokens, tags = html_tokenizer.tokenize_single(html_document_fromstring(html)) |
| 36 | + pattern = ur'(^|{0})013-Witgoedreparaties.nl({0}|$)'.format(SPACES_SRE) |
| 37 | + choices = ['013-witgoedreparaties.nl'] |
| 38 | + |
| 39 | + clf = FuzzyMatchClassifier(entity='ORG', pattern=pattern, choices=choices) |
| 40 | + tags = clf.predict([html_tokens]) |
| 41 | + |
| 42 | + assert tags[0] == ['B-ORG', 'O', 'B-ORG', 'O', 'O'] |
| 43 | + |
| 44 | + |
| 45 | +def test_fuzzy_assign_bio_tags_with_non_break_spaces(): |
| 46 | + html_tokenizer = HtmlTokenizer() |
| 47 | + html = """<title>013-witgoedreparaties.nl | ​013-witgoedreparaties.nl | title </title>""" |
| 48 | + html_tokens, tags = html_tokenizer.tokenize_single(html_document_fromstring(html)) |
| 49 | + pattern = ur'(^|{0})013-Witgoedreparaties.nl({0}|$)'.format(SPACES_SRE) |
| 50 | + choices = ['013-witgoedreparaties.nl'] |
| 51 | + |
| 52 | + clf = FuzzyMatchClassifier(entity='ORG', pattern=pattern, choices=choices) |
| 53 | + tags = clf.predict([html_tokens]) |
| 54 | + |
| 55 | + assert tags[0] == ['B-ORG', 'O', 'B-ORG', 'O', 'O'] |
| 56 | + |
| 57 | + html = """<title>013-witgoedreparaties.nl | 013-witgoedreparaties.nl | title </title>""" |
| 58 | + html_tokens, tags = html_tokenizer.tokenize_single(html_document_fromstring(html)) |
| 59 | + choices = ['013-witgoedreparaties.nl'] |
| 60 | + |
| 61 | + clf = FuzzyMatchClassifier(entity='ORG', pattern=pattern, choices=choices) |
| 62 | + tags = clf.predict([html_tokens]) |
| 63 | + |
| 64 | + assert tags[0] == ['B-ORG', 'O', 'B-ORG', 'O', 'O'] |
| 65 | + |
| 66 | + |
| 67 | +def test_merge_bio_tags(): |
| 68 | + tags = merge_bio_tags(['B-ORG', 'O', 'B-ORG', 'O', 'O'], ['B-ORG', 'B-ADDR', 'B-ORG', 'O', 'O']) |
| 69 | + assert tags == ['B-ORG', 'B-ADDR', 'B-ORG', 'O', 'O'] |
| 70 | + |
| 71 | + # I-ORG conflict with O |
| 72 | + tags = merge_bio_tags(['B-ORG', 'I-ORG', 'B-ORG', 'O', 'O'], ['B-ORG', 'O', 'B-ORG', 'O', 'O']) |
| 73 | + assert tags == ['B-ORG', 'I-ORG', 'B-ORG', 'O', 'O'] |
| 74 | + |
| 75 | + # merge 3 tag list |
| 76 | + tags = merge_bio_tags(['B-ORG', 'O', 'B-ORG', 'O', 'O'], ['B-ORG', 'I-ORG', 'B-ORG', 'O', 'O'], |
| 77 | + ['O', 'O', 'O', 'B-ADDR', 'I-ADDR']) |
| 78 | + assert tags == ['B-ORG', 'I-ORG', 'B-ORG', 'B-ADDR', 'I-ADDR'] |
0 commit comments