-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/convert ner wroc #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
flayed1
wants to merge
14
commits into
master
Choose a base branch
from
feature/convert-NER-wroc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e0b1697
Working-ish solution
flayed1 84914b7
Fix output format
flayed1 326698c
[WIP] Some really ugly stuff
flayed1 06e6e4b
Save training data, needs a bug fix
flayed1 525a283
Fix BILUO
flayed1 2f06b47
Add analysis
flayed1 9d3224b
Remove analysis (moved to another branch)
flayed1 59491bb
Remove commented out code
flayed1 b7cd610
this field has non-binary values (it can even be 2, or <text>-<number>)
flayed1 70785b2
Code cleanup
flayed1 1b371b2
Auto stash before rebase of "master"
flayed1 de41d27
Add accidentally removed function
flayed1 050dbbb
Fix rebase bugs
flayed1 96f9e2d
Remove remains of detokenization
flayed1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| import xml.etree.ElementTree as ET | ||
| import json | ||
| import os | ||
|
|
||
| path_prefix = './' | ||
| corpus_path = 'data/kpwr-1.1/' | ||
| output_path = 'data/NER/' | ||
| output = 'NER_wroc.json' | ||
|
|
||
| doc_id = 0 | ||
| corpus = [] | ||
|
|
||
| NE_njkp_to_spacy = {'persName': 'PERSON', | ||
| 'placeName': 'LOC', | ||
| 'orgName': 'ORG', | ||
| 'date': 'DATE', | ||
| 'time': 'TIME', | ||
| 'geogName': 'LOC'} | ||
|
|
||
|
|
||
| class Token: | ||
| def __init__(self, orth, attribs, id): | ||
| self.orth = orth | ||
| self.attribs = attribs | ||
| self.id = id | ||
|
|
||
| def is_NE(self): | ||
| return len(self.attribs) != 0 | ||
|
|
||
| def get_NE(self): | ||
| return self.attribs[0] if len(self.attribs) > 0 else "" | ||
|
|
||
| def __str__(self): | ||
| return (self.orth + ":" + str(self.attribs)) | ||
|
|
||
|
|
||
| def get_subdirs(dir): | ||
| return [name for name in os.listdir(dir) if os.path.isdir(os.path.join(dir, name))] | ||
|
|
||
|
|
||
| def process_token(tok): | ||
| attribs = [] | ||
| orth = tok.find("orth").text | ||
| for ann in tok.iter("ann"): | ||
| if ann.attrib['chan'].endswith("nam") and ann.text != "0": | ||
| attribs += [ann.attrib['chan']] | ||
|
|
||
| return Token(orth, attribs, -1) | ||
|
|
||
|
|
||
| def get_common_tag(t1, t2): | ||
| set1 = set(t1.attribs) | ||
| set2 = set(t2.attribs) | ||
| common = list(set1 & set2) | ||
| return common[0] if len(common) > 0 else None | ||
|
|
||
|
|
||
| def pick_tags(tokens): | ||
| # first and last separately | ||
| if len(tokens) == 0: | ||
| return tokens | ||
| if len(tokens) == 1: | ||
| if tokens[0].is_NE(): | ||
| tokens[0].attribs = [tokens[0].attribs[0]] | ||
| return tokens | ||
|
|
||
| t0 = tokens[0] | ||
| if len(t0.attribs) > 1: | ||
| new_tag = get_common_tag(t0, tokens[1]) | ||
| if new_tag is None: | ||
| t0.attribs = [t0.attribs[0]] | ||
| else: | ||
| t0.attribs = [new_tag] | ||
|
|
||
| for i in range(1, len(tokens) - 1): | ||
| if len(tokens[i].attribs) > 1: | ||
| new_tag = get_common_tag(tokens[i - 1], tokens[i]) | ||
| if new_tag is None: | ||
| new_tag = get_common_tag(tokens[i], tokens[i + 1]) | ||
| if new_tag is None: | ||
| tokens[i].attribs = [tokens[i].attribs[0]] | ||
| else: | ||
| tokens[i].attribs = [new_tag] | ||
| else: | ||
| tokens[i].attribs = [new_tag] | ||
|
|
||
| te = tokens[-1] | ||
| if len(te.attribs) > 1: | ||
| new_tag = get_common_tag(te, tokens[-2]) | ||
| if new_tag is None: | ||
| te.attribs = [te.attribs[0]] | ||
| else: | ||
| te.attribs = [new_tag] | ||
|
|
||
| assert (all(len(t.attribs) <= 1 for t in [t0] + tokens + [te])) | ||
| return [t0] + tokens[1:-2] + [te] | ||
|
|
||
|
|
||
| def convert_to_biluo(tokens): | ||
| out = [] | ||
| in_ne = False | ||
| for i, token in enumerate(tokens[:-1]): | ||
| if in_ne: | ||
| if token.is_NE(): | ||
| if tokens[i + 1].is_NE() and token.get_NE() == tokens[i + 1].get_NE(): | ||
| # inner NE | ||
| out += [Token(token.orth, ["I-" + token.get_NE()], token.id)] | ||
| else: | ||
| # last NE | ||
| out += [Token(token.orth, ["L-" + token.get_NE()], token.id)] | ||
| in_ne = False | ||
| else: | ||
| # we shouldn't ever get here | ||
| assert (False) | ||
|
|
||
| else: | ||
| if token.is_NE(): | ||
| # new NE | ||
| if tokens[i + 1].is_NE() and token.get_NE() == tokens[i + 1].get_NE(): | ||
| # beginning NE | ||
| out += [Token(token.orth, ["B-" + token.get_NE()], token.id)] | ||
| in_ne = True | ||
| else: | ||
| # unit NE | ||
| out += [Token(token.orth, ["U-" + token.get_NE()], token.id)] | ||
| in_ne = False | ||
| else: | ||
| # outside of NE | ||
| out += [Token(token.orth, ["O"], token.id)] | ||
|
|
||
| # process last token | ||
| token = tokens[-1] | ||
| if in_ne: | ||
| out += [Token(token.orth, ["L-" + token.get_NE()], token.id)] | ||
| else: | ||
| if token.is_NE(): | ||
| out += [Token(token.orth, ["U-" + token.get_NE()], token.id)] | ||
| else: | ||
| out += [Token(token.orth, ["O"], token.id)] | ||
|
|
||
| return out | ||
|
|
||
|
|
||
| docs = [] | ||
| doc_idx = 0 | ||
| for subfolder in get_subdirs(os.path.join(path_prefix, corpus_path)): | ||
| for file in os.listdir(os.path.join(path_prefix, corpus_path, subfolder)): | ||
| if not file.endswith("rel.xml") and not file.endswith(".ini"): | ||
| doc_json = {} | ||
| sentences = [] | ||
| token_idx = 0 | ||
| raw = "" | ||
| tree = ET.parse(os.path.join(path_prefix, corpus_path, subfolder, file)) | ||
| root = tree.getroot() | ||
| sents = root.iter("sentence") | ||
| for sent in sents: | ||
| tokens = [] | ||
| for tok in sent.iter("tok"): | ||
| token = process_token(tok) | ||
| token.id = token_idx | ||
| token_idx += 1 | ||
| tokens += [token] | ||
|
|
||
| tokens = pick_tags(tokens) | ||
| tokens = convert_to_biluo(tokens) | ||
|
|
||
| sent = {'tokens': [{ | ||
| 'orth': t.orth, | ||
| 'id': t.id, | ||
| 'ner': t.get_NE()} | ||
| for t in tokens | ||
| ], 'brackets': [] | ||
| } | ||
|
|
||
| sentences += [sent] | ||
|
|
||
| doc_json = { | ||
| 'id': doc_idx, | ||
| 'paragraphs': [{'sentences': sentences}] | ||
| } | ||
| corpus += [doc_json] | ||
| doc_idx += 1 | ||
|
|
||
| with open(os.path.expanduser(os.path.join(path_prefix, output_path, output)), 'w+') as f: | ||
| json.dump(corpus, f) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,3 +11,4 @@ vocab.jsonl | |
| /vectors_300.txt | ||
| tagmap.py | ||
| NKJP-PodkorpusMilionowy-1.2 | ||
| /kpwr-1.1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| md5: 58cbc0bd05749d04e4b6a5e4c9d78c01 | ||
| outs: | ||
| - cache: true | ||
| md5: d84971d4b907e5efc5d9320de6691027.dir | ||
| metric: false | ||
| path: kpwr-1.1 | ||
| wdir: . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,5 @@ | |
| /rules.json | ||
|
|
||
| /lemma_sources | ||
| /sjp_ispell.tar.bz2 | ||
| /sjp_ispell.tar.bz2 | ||
| /lemma_sources_exp | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| cmd: python lemma_rules_extraction/yield_all_suffixes.py | ||
| deps: | ||
| - md5: 947b48802b53bdff2ad02122c04063e5.dir | ||
| path: data/lemmatizer_data/lemma_sources | ||
| - md5: abb151c19621000ccc91d8e4b494674f | ||
| path: lemma_rules_extraction/yield_all_suffixes.py | ||
| md5: 53ab70bcbc27640766d00b4977b4733d | ||
| outs: | ||
| - cache: true | ||
| md5: 4f29377d4c9dd5c9997c74750af31321.dir | ||
| metric: false | ||
| path: data/lemmatizer_data/lemma_sources_exp | ||
| persist: false | ||
| wdir: . |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.