-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTNUCrawler.py
More file actions
57 lines (44 loc) · 2.04 KB
/
TNUCrawler.py
File metadata and controls
57 lines (44 loc) · 2.04 KB
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
import Punctuation
import os
import ConvertHtmlToText
import datetime
import SeparateDocumentToSentences
def extractContentNews(src_link, language):
content = ""
if (language == "zh" or language == "en"):
content = ConvertHtmlToText.getTextFromTagsWithId(src_link= src_link,tag= "div",id= "wrapper")
return content
return ConvertHtmlToText.getTextFromTagsWithId(src_link = src_link, tag= "div", id="container")
def crawlWithLanguage(language):
"""
:param language: "en", "zh"
:return: None
"""
if(language != "en" and language != 'zh'):
raise Exception("Resource not supported")
current_dir = os.path.dirname(os.path.realpath(__file__))
map_Punctuation = Punctuation.getPunctuationForLanguage(language)
resource_file = "{}/TNUCrawler/{}-{}.txt".format(current_dir,"vi",language)
Document_folder = current_dir + "/Data/crawler_success/TNU/Document/"
if not os.path.exists(Document_folder):
os.makedirs(Document_folder)
f = open(resource_file, "r",encoding="utf-8")
if not f:
raise Exception("Resource file not exist")
for line in f:
src_link, tgt_link, mutil_page = (line.split("\t"))
file_name = datetime.datetime.now().timestamp()
list_src = SeparateDocumentToSentences.slpit_text( text = extractContentNews(src_link, "vi")
,list_sign= list(map_Punctuation.keys()) )
file = open(Document_folder+"{}.vi.txt".format(file_name), "w", encoding="utf-8")
for line in list_src:
file.write("{} \n".format(line))
file.close()
list_tgt = SeparateDocumentToSentences.slpit_text( text= extractContentNews(tgt_link, "zh")
, list_sign=list(map_Punctuation.keys()))
file = open(Document_folder + "{}.{}.txt".format(file_name, language), "w", encoding="utf-8")
for line in list_tgt:
file.write("{} \n".format(line))
file.close()
f.close()
crawlWithLanguage("zh")