-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored master branch #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,8 +40,11 @@ def switch_word(word): | |
| </style>""", unsafe_allow_html=True) | ||
|
|
||
| placeholder = st.sidebar.empty() | ||
| search_text_box = placeholder.text_input('Word', value=st.session_state['current_word'], key='sidebar_text_input') | ||
| if search_text_box: | ||
| if search_text_box := placeholder.text_input( | ||
| 'Word', | ||
| value=st.session_state['current_word'], | ||
| key='sidebar_text_input', | ||
| ): | ||
|
Comment on lines
-43
to
+47
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| switch_word(search_text_box) | ||
|
|
||
| buttons = {} | ||
|
|
@@ -55,6 +58,5 @@ def switch_word(word): | |
|
|
||
| output_data = json_viewer(json_object=data, label=0) | ||
|
|
||
| save_button = st.button('Save') | ||
| if save_button: | ||
| if save_button := st.button('Save'): | ||
| dictionary.save(st.session_state['current_word'], output_data) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,14 +37,13 @@ def extract_dictionary_from_elasticsearch(data): | |
| index_name = "dictionary" | ||
| with open(DICTIONARY_FILE, "w") as f: | ||
| f.write("") | ||
| f = open(DICTIONARY_FILE, "a") | ||
| i = 0 | ||
| for res in query_doc(es, index_name): | ||
| docs = res["hits"]["hits"] | ||
| print(len(docs)) | ||
| doc_content = extract_dictionary_from_elasticsearch(docs) | ||
| if len(doc_content) > 1: | ||
| dict_content = yaml.dump(doc_content, allow_unicode=True, sort_keys=True) | ||
| f.write(dict_content) | ||
| i += 1 | ||
| f.close() | ||
| with open(DICTIONARY_FILE, "a") as f: | ||
| i = 0 | ||
| for res in query_doc(es, index_name): | ||
| docs = res["hits"]["hits"] | ||
| print(len(docs)) | ||
| doc_content = extract_dictionary_from_elasticsearch(docs) | ||
| if len(doc_content) > 1: | ||
| dict_content = yaml.dump(doc_content, allow_unicode=True, sort_keys=True) | ||
| f.write(dict_content) | ||
| i += 1 | ||
|
Comment on lines
-40
to
+49
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,7 @@ | |
| es = Elasticsearch() | ||
|
|
||
| words = yaml.safe_load(content) | ||
| i = 0 | ||
| for word_key, value in words.items(): | ||
| i += 1 | ||
| for i, (word_key, value) in enumerate(words.items(), start=1): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| data = { | ||
| "headword": word_key, | ||
| "senses": value | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,9 +42,7 @@ def find_word(ud_dataset, word) -> list: | |
| st.write('Loaded corpus: wiki') | ||
| st.write(f'Loaded dictionary: {dictionary_n_words} words (202108.yaml)') | ||
|
|
||
| word = st.text_input('Word') | ||
|
|
||
| if word: | ||
| if word := st.text_input('Word'): | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| sentences = find_word(ud_dataset, word) | ||
| st.write(f"## {word}") | ||
| st.write("Output:") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,8 +36,7 @@ def tag_correct(data): | |
| tag = node['tag'] | ||
| if tag in TAG_MAP: | ||
| correct_data[word][i]['tag'] = TAG_MAP[tag] | ||
| # case for word 'khối' | ||
| if word == 'khối' and node['tag'] == 'S': | ||
| if word == 'khối' and tag == 'S': | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ): |
||
| correct_data[word][i]['tag'] = 'adjective' | ||
| return correct_data | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,20 +24,14 @@ def add(self, word): | |
| def load(file): | ||
| with open(file) as f: | ||
| yaml.safe_load(f) | ||
| new_dict = Dictionary() | ||
| return new_dict | ||
| return Dictionary() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def to_data(self): | ||
| data = {} | ||
| words = sorted(self.words) | ||
| for text in words: | ||
| word = self.words[text] | ||
| if word.data is None: | ||
| content = '' | ||
| elif len(word.data) == 0: | ||
| content = '' | ||
| else: | ||
| content = word.data | ||
| content = '' if word.data is None or len(word.data) == 0 else word.data | ||
|
Comment on lines
-35
to
+34
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| data[text] = content | ||
| return data | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,13 +24,7 @@ | |
| 'n': 'NOUN', | ||
| 'S': 'NOUN' # khối | ||
| } | ||
| TEMP_IGNORE_POS = set([ | ||
| 'R', # phụ từ tiếng Việt | ||
| 'X', # không phân loại | ||
| 'Z', # yếu tố cấu tạo từ | ||
| 'D', # không có định nghĩa (ví dụ: chút ít) | ||
| 'O', # úi chà | ||
| ]) | ||
| TEMP_IGNORE_POS = {'R', 'X', 'Z', 'D', 'O'} | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
This removes the following comments ( why? ): |
||
| logger.info("Start loading") | ||
| dict = Dictionary() | ||
| pos_count = {} | ||
|
|
@@ -45,7 +39,7 @@ | |
| defs = [] | ||
| pos_tags = {} | ||
| text = key | ||
| for definition in data[key]: | ||
| for definition in data[text]: | ||
| pos_tag = definition['pos'] | ||
| if pos_tag not in pos_tags: | ||
| i = len(pos_tags) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,10 +6,8 @@ | |
|
|
||
| def warn(file, line_number, message, type=None): | ||
| global total_errors | ||
| text = "" | ||
| if type: | ||
| text = f"[{type}] " | ||
| text += basename(file) + ":" + str(line_number) | ||
| text = f"[{type}] " if type else "" | ||
| text += f"{basename(file)}:{str(line_number)}" | ||
|
Comment on lines
-9
to
+10
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if total_errors < MAX_SHOW_ERRORS: | ||
| print(colored(text, 'red'), colored(message, 'red')) | ||
|
|
||
|
|
@@ -73,6 +71,6 @@ def stats(): | |
| print("\n# DICTIONARY STATISTICS") | ||
| global NUM_WORDS | ||
| print("* Number of words:", NUM_WORDS) | ||
| print("* Tags:", len(VALID_TAGS), "(" + str(sorted(VALID_TAGS))[1:-1] + ")") | ||
| print("* Tags:", len(VALID_TAGS), f"({str(sorted(VALID_TAGS))[1:-1]})") | ||
|
Comment on lines
-76
to
+74
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| stats() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,25 +13,41 @@ | |
| MAX_SHOW_ERRORS = 100 | ||
| total_errors = 0 | ||
|
|
||
| punct = {"!", "/", ",", ".", "...", "?", "-", "\"", "-", ":", "(", ")", "–", "&", ";", "‘", "’", "+"} | ||
| punct = { | ||
| "!", | ||
| "/", | ||
| ",", | ||
| ".", | ||
| "...", | ||
| "?", | ||
| "\"", | ||
| "-", | ||
| ":", | ||
| "(", | ||
| ")", | ||
| "–", | ||
| "&", | ||
| ";", | ||
| "‘", | ||
| "’", | ||
| "+", | ||
| } | ||
|
|
||
|
Comment on lines
-16
to
+35
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| specials = {"rbkt", "lbkt"} | ||
|
|
||
|
|
||
| def warn(file, line_number, message, type=None): | ||
| global total_errors | ||
| text = "" | ||
| if type: | ||
| text = f"[{type}] " | ||
| text += basename(file) + ":" + str(line_number) | ||
| text = f"[{type}] " if type else "" | ||
| text += f"{basename(file)}:{str(line_number)}" | ||
|
Comment on lines
-22
to
+42
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| if total_errors < MAX_SHOW_ERRORS: | ||
| print(colored(text, 'red'), colored(message, 'red')) | ||
|
|
||
| total_errors += 1 | ||
|
|
||
|
|
||
| def load_dictionary(dict_file): | ||
| dict = joblib.load(dict_file) | ||
| return dict | ||
| return joblib.load(dict_file) | ||
|
Comment on lines
-33
to
+50
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def load_corpus(file): | ||
|
|
@@ -60,12 +76,29 @@ def load_corpus(file): | |
| continue | ||
| tags = list(words[word]) | ||
| ignores_tags = { | ||
| "ADJ", "ADV", "INTJ", "NOUN", "PROPN", "PRON", "SYM", "X", "N:G", "VERB:G", "NY", | ||
| "N", "NB", "NNPy", | ||
| "NNP", "NNPy", | ||
| "V", "VERB", | ||
| "Num", "NUMx", "NUM", "NUMX" | ||
| "ADJ", | ||
| "ADV", | ||
| "INTJ", | ||
| "NOUN", | ||
| "PROPN", | ||
| "PRON", | ||
| "SYM", | ||
| "X", | ||
| "N:G", | ||
| "VERB:G", | ||
| "NY", | ||
| "N", | ||
| "NB", | ||
| "NNP", | ||
| "NNPy", | ||
| "V", | ||
| "VERB", | ||
| "Num", | ||
| "NUMx", | ||
| "NUM", | ||
| "NUMX", | ||
| } | ||
|
|
||
|
Comment on lines
-63
to
+101
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| if tags[0] in ignores_tags: | ||
| continue | ||
| if word not in dict: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,7 @@ def add_lemma_column(sentence): | |
| tokens.insert(1, lemma) | ||
| new_row = "\t".join(tokens) | ||
| result.append(new_row) | ||
| new_sentence = "\n".join(result) | ||
| return new_sentence | ||
| return "\n".join(result) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| filepath = join(dirname(dirname(__file__)), "corpus", "ud", "202108.txt") | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,9 +42,7 @@ def send(self, message): | |
| payload = {'sender': self.id, 'message': 'hey there'} | ||
| headers = {'content-type': 'application/json'} | ||
| r = requests.post('http://localhost:5005/webhooks/rest/webhook', json=payload, headers=headers) | ||
| if r is None: | ||
| return None | ||
| return r.json() | ||
| return None if r is None else r.json() | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,9 +26,9 @@ def transform_sentence(sentence): | |
| f.write(content) | ||
|
|
||
|
|
||
| max_len = 0 | ||
| for file in ["train.txt", "test.txt", "dev.txt"]: | ||
| print(file) | ||
| max_len = 0 | ||
|
Comment on lines
+29
to
-31
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lines
|
||
| src_folder = join("bert-ner", "data", "vlsp2016") | ||
| dest_folder = join("bert-ner", "data") | ||
| transform(src_file=join(src_folder, file), dest_file=join(dest_folder, file)) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,14 +25,13 @@ def encode(self, labels): | |
| if type(labels) == list: | ||
| return [self.encode(label) for label in labels] | ||
| label = labels # label is a string | ||
| if label not in self.label2index: | ||
| index = self.vocab_size | ||
| self.label2index[label] = index | ||
| self.index2label[index] = label | ||
| self.vocab_size += 1 | ||
| return index | ||
| else: | ||
| if label in self.label2index: | ||
| return self.label2index[label] | ||
| index = self.vocab_size | ||
| self.label2index[label] = index | ||
| self.index2label[index] = label | ||
| self.vocab_size += 1 | ||
| return index | ||
|
Comment on lines
-28
to
+34
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| class TokenClassificationCorpus: | ||
|
|
@@ -187,8 +186,7 @@ def test_step(self, batch, batch_idx): | |
| self.log('test_loss', loss) | ||
|
|
||
| def configure_optimizers(self): | ||
| optimizer = AdamW(self.parameters(), lr=2e-5) | ||
| return optimizer | ||
| return AdamW(self.parameters(), lr=2e-5) | ||
|
Comment on lines
-190
to
+189
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| def main(): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,20 +51,19 @@ def write_predictions_to_file(self, writer: TextIO, test_input_reader: TextIO, p | |
| if not preds_list[example_id]: | ||
| example_id += 1 | ||
| elif preds_list[example_id]: | ||
| output_line = line.split()[0] + " " + preds_list[example_id].pop(0) + "\n" | ||
| output_line = f"{line.split()[0]} {preds_list[example_id].pop(0)}" + "\n" | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| writer.write(output_line) | ||
| else: | ||
| logger.warning("Maximum sequence length exceeded: No prediction for '%s'.", line.split()[0]) | ||
|
|
||
| def get_labels(self, path: str) -> List[str]: | ||
| if path: | ||
| with open(path, "r") as f: | ||
| labels = f.read().splitlines() | ||
| if "O" not in labels: | ||
| labels = ["O"] + labels | ||
| return labels | ||
| else: | ||
| if not path: | ||
| return ["O", "B-MISC", "I-MISC", "B-PER", "I-PER", "B-ORG", "I-ORG", "B-LOC", "I-LOC"] | ||
| with open(path, "r") as f: | ||
| labels = f.read().splitlines() | ||
| if "O" not in labels: | ||
| labels = ["O"] + labels | ||
| return labels | ||
|
Comment on lines
-60
to
+66
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
|
|
||
| class Chunk(NER): | ||
|
|
@@ -73,13 +72,7 @@ def __init__(self): | |
| super().__init__(label_idx=-2) | ||
|
|
||
| def get_labels(self, path: str) -> List[str]: | ||
| if path: | ||
| with open(path, "r") as f: | ||
| labels = f.read().splitlines() | ||
| if "O" not in labels: | ||
| labels = ["O"] + labels | ||
| return labels | ||
| else: | ||
| if not path: | ||
|
Comment on lines
-76
to
+75
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| return [ | ||
| "O", | ||
| "B-ADVP", | ||
|
|
@@ -103,6 +96,11 @@ def get_labels(self, path: str) -> List[str]: | |
| "I-CONJP", | ||
| "I-PP", | ||
| ] | ||
| with open(path, "r") as f: | ||
| labels = f.read().splitlines() | ||
| if "O" not in labels: | ||
| labels = ["O"] + labels | ||
| return labels | ||
|
|
||
|
|
||
| class POS(TokenClassificationTask): | ||
|
|
@@ -127,15 +125,17 @@ def read_examples_from_file(self, data_dir, mode: Union[Split, str]) -> List[Inp | |
| return examples | ||
|
|
||
| def write_predictions_to_file(self, writer: TextIO, test_input_reader: TextIO, preds_list: List): | ||
| example_id = 0 | ||
| for sentence in parse_incr(test_input_reader): | ||
| for example_id, sentence in enumerate(parse_incr(test_input_reader)): | ||
| s_p = preds_list[example_id] | ||
| out = "" | ||
| for token in sentence: | ||
| out += f'{token["form"]} ({token["upos"]}|{s_p.pop(0)}) ' | ||
| out += "\n" | ||
| out = ( | ||
| "".join( | ||
| f'{token["form"]} ({token["upos"]}|{s_p.pop(0)}) ' | ||
| for token in sentence | ||
| ) | ||
| + "\n" | ||
| ) | ||
|
|
||
| writer.write(out) | ||
| example_id += 1 | ||
|
Comment on lines
-130
to
-138
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
|
||
| def get_labels(self, path: str) -> List[str]: | ||
| if path: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
Dictionary.get_wordrefactored with the following changes:inline-immediately-returned-variable)