-
Notifications
You must be signed in to change notification settings - Fork 29
/
corpus.py
49 lines (36 loc) · 1.17 KB
/
corpus.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
import os
import json
from gensim import corpora
from article import Article
class Corpus(object):
def __init__(self):
self.corpus = []
pass
class PTTCorpus(Corpus):
def __init__(self):
self.corpus = []
def load_data(self, path, is_dir=False):
data = []
filename = None
if is_dir:
filenames = [name for name in os.listdir(path) if not name.startswith(".")]
else:
filenames = [path]
for filename in filenames:
with open(os.path.join(path, filename),'r', encoding="utf-8") as data:
tp = json.load(data)
for article in tp:
try:
self.corpus.append(Article(article))
except:
print("於 %s 發生某篇文章的解析錯誤" % filename)
def get_text(self):
for article in self.corpus:
title = article.title
resp = ""
for r in article.responses:
resp += ' ' + r["Content"]
yield title + resp
def get_titles(self):
for article in self.corpus:
yield article.title