-
Notifications
You must be signed in to change notification settings - Fork 0
/
SUSC_run.py
150 lines (106 loc) · 4.35 KB
/
SUSC_run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
"""
@ author: Taehyeong Kim
@ e-mail: [email protected]
"""
import argparse
parser = argparse.ArgumentParser(description='Similarity-based Unsupervised Spelling Correction Using BioWordVec')
parser.add_argument('--data', type=str, help='Data', default='sample.csv')
parser.add_argument('--similar_n', type=int, help='most similar words', default=30)
parser.add_argument('--fasttext_min_similarity', type=float, help='cosine similarity ', default=0.80)
parser.add_argument('--edit_distance_threshold', type=int, help='edit distance ', default=3)
args = parser.parse_args()
filename_read = args.data
similar_n = args.similar_n
fasttext_min_similarity = args.fasttext_min_similarity
edit_distance_threshold = args.edit_distance_threshold
import utils.preprocessing
from gensim.models import FastText
from pyxdameraulevenshtein import damerau_levenshtein_distance
import collections
import numpy as np
import pandas as pd
import re
from keras.preprocessing.text import text_to_word_sequence
import warnings
warnings.filterwarnings("ignore")
## Data read
df = pd.read_csv("data/"+str(filename_read), encoding = "CP949")
df = utils.preprocessing.Preprocessing().organ_result(df)
def organ_extract(df):
temp=[]
for _ in range(len(df)):
if ':' in df.iat[_][0]:
temp.append(df.iat[_][0].split(":")[1].strip())
if ';' in df.iat[_][0]:
temp.append(df.iat[_][0].split(";")[1].strip())
return temp
organ_list = organ_extract(df["organ_result"])
organ_list = [x for x in organ_list if x]
stop_words=["ss","spp","ssp","mrsa","mssa","group"]
## Extraction of data using regular expression
organ_word=[]
for _ in range(len(organ_list)):
temp_words = text_to_word_sequence(organ_list[_]) #Tokenizer
for _ in temp_words:
if _ not in stop_words:
if len(_)>3:
organ_word.append(_)
organ_corpus = sorted(list(set(organ_word)))
print("Organism count: " + str(len(organ_corpus)))
## Misspelling Detection
print("---Misspelling Detection---")
dict_corpus=list(pd.read_csv("data/misspelling_detection.txt", header=None)[0])
misspell=sorted(list(set(organ_word)))
for _ in dict_corpus:
if _ in misspell:
misspell.remove(_)
vocab_m={}
for _ in misspell:
vocab_m[_]=organ_word.count(_)
print("Misspell count: " + str(len(vocab_m)))
vocab_c=collections.Counter(organ_word)
for _ in misspell:
del vocab_c[_]
print("Correct count: " + str(len(vocab_c)))
## Load BioWordVec
print("### Caution: take a long time ###")
print("BioWordVec loading")
bio_model = FastText.load_fasttext_format('./pretrained/BioWordVec_PubMed_MIMICIII_d200.bin')
print("BioWordVec loaded")
## Candidate Generation
print("---Candidate Generation---")
nonalphabetic = re.compile(r'[^a-zA-Z]')
def include_spell_mistake(word, similar_word, score):
return (score > fasttext_min_similarity
and damerau_levenshtein_distance(word, similar_word) <= edit_distance_threshold
and len(similar_word) > 3
and word[0] == similar_word[0]
and nonalphabetic.search(similar_word) is None)
## Candidate Ranking
print("---Candidate Ranking---")
# Grid Search
restrict_vocab_size = np.arange(100000,1000000,100000)
rank=1
word_to_mistakes = collections.defaultdict(list)
for word, freq in vocab_m.items():
if len(word) <= 3 or nonalphabetic.search(word) is not None:
continue
for i in range(len(restrict_vocab_size)):
similar_pre = bio_model.wv.most_similar(word, topn=similar_n,
restrict_vocab=restrict_vocab_size[i])
for similar_p in similar_pre:
if include_spell_mistake(word, similar_p[0], similar_p[1]) and len(word_to_mistakes[word])<rank:
word_to_mistakes[word].append(similar_p[0])
print("Spelling correction count: " + str(len(word_to_mistakes)))
## Spelling Correction
temp=[]
for word, mistakes in word_to_mistakes.items():
for mistake in mistakes:
p_score = bio_model.wv.similarity(word, mistake)
if mistake != word:
temp.append([word, mistake, p_score])
df=pd.DataFrame(temp, columns=["Misspell","Correct","score"])
df=df.sort_values(["Misspell","score"], ascending=True)
df.to_csv("result.csv", index = False, encoding = "CP949")
print("--- end ---")