MCPcopy Create free account
hub / github.com/chengsen/PyTorch_TextGCN / CorpusProcess

Class CorpusProcess

data_processor.py:97–149  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

95
96
97class CorpusProcess:
98 def __init__(self, dataset, encoding=None):
99 corpus_path = "data/text_dataset/corpus"
100 clean_corpus_path = "data/text_dataset/clean_corpus"
101 if not os.path.exists(clean_corpus_path):
102 os.makedirs(clean_corpus_path)
103
104 self.dataset = dataset
105 self.corpus_name = f"{corpus_path}/{dataset}.txt"
106 self.save_name = f"{clean_corpus_path}/{dataset}.txt"
107 self.context_dct = defaultdict(dict)
108
109 self.encoding = encoding
110 self.clean_text()
111
112 def clean_text(self):
113 sp = StringProcess()
114 word_lst = list()
115 with open(self.corpus_name, mode="rb", encoding=self.encoding) as fin:
116 for indx, item in tqdm(enumerate(fin), desc="clean the text"):
117 data = item.strip().decode('latin1')
118 data = sp.clean_str(data)
119 if self.dataset not in {"mr"}:
120 data = sp.remove_stopword(data)
121 word_lst.extend(data.split())
122
123 word_st = set()
124 if self.dataset not in {"mr"}:
125 for word, value in Counter(word_lst).items():
126 if value < 5:
127 continue
128 word_st.add(word)
129 else:
130 word_st = set(word_lst)
131
132 doc_len_lst = list()
133 with open(self.save_name, mode='w') as fout:
134 with open(self.corpus_name, mode="rb", encoding=self.encoding) as fin:
135 for line in tqdm(fin):
136 lines_str = line.strip().decode('latin1')
137 lines_str = sp.clean_str(lines_str)
138 if self.dataset not in {"mr"}:
139 lines_str = sp.remove_stopword(lines_str)
140 lines_str = remove_less_word(lines_str, word_st)
141
142 fout.write(lines_str)
143 fout.write(" \n")
144
145 doc_len_lst.append(len(lines_str.split()))
146
147 print("Average length:", np.mean(doc_len_lst))
148 print("doc count:", len(doc_len_lst))
149 print("Total number of words:", len(word_st))
150
151
152def main():

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected