-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_extracted_dom.rb
134 lines (100 loc) · 4.05 KB
/
process_extracted_dom.rb
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
#***
require_relative "corpus_tools"
require_relative 'net_tools.rb'
require_relative 'read_cmd.rb'
outhash = process_cmd
corpus_and_label = outhash["corpus_and_label"]
maincorpus = corpus_and_label.split("-")[0]
query = outhash["query"]
variable = outhash["variable"]
only_process_local = outhash["only_process_local"]
granularity = outhash["granularity"]
variable_source = outhash["variable_source"]
var_output = outhash["var_output"]
context = outhash["context"]
limit = outhash["var_output"]
show = outhash["show"]
structs = outhash["structs"]
sort = outhash["sort"]
granularity = outhash["granularity"]
corpora = read_corpus_label(corpus_and_label,outputmode="array")
def cleant(parameter)
if !parameter.nil?
parameter.gsub!("\t","")
end
return parameter
end
#conll_file = File.open("#{var_output}#{variable}.txt","w:utf-8")
sample_file = File.open("#{var_output}#{variable}_#{maincorpus}.tsv","w:utf-8")
header = "unique_id\ttoken_id\tsentence\tmaincorpus\tsubcorpus\tyear"
sample_file.puts header
input_dir = "#{var_output}Jsons\\#{variable}\\Jsons_#{maincorpus}"
filelist = Dir.children(input_dir)
#corpora.each do |corpus|
#output_dir = "#{var_output}Tsvs\\#{variable}\\Tsvs_#{maincorpus}"
#if !Dir.exist?("#{var_output}Tsvs\\#{variable}\\")
# Dir.mkdir("#{var_output}Tsvs\\#{variable}\\")
#end
#if !Dir.exist?(output_dir)
# Dir.mkdir(output_dir)
#end
#date_sep = {"familjeliv" => " ", "svt" => "T", "da" => " ", "flashback" => " ", "twitter" => " "}
unique_id = 0
filelist.each do |filename2|
filename2 = filename2.encode('utf-8')
if filename2.split(".")[1] == "json"
STDERR.puts filename2
STDERR.puts "Processing Korp's output..."
file = File.read("#{input_dir}\\#{filename2}")
year = filename2.split("_")[-2].to_i
data_hash = JSON.parse(file)
if data_hash["ERROR"].nil?
hits = data_hash["kwic"]
#if maincorpus == "familjeliv"
# gender_hash = extract_gender
# age_hash = extract_age
#end
hits.each do |hit|
#if hit["tokens"][0]["ref"].to_i == 1
unique_id += 1
match_start = hit["match"]["start"].to_i
match_end = hit["match"]["end"].to_i
#output_array = []
#output_array << unique_id
corpus_from_json = hit["corpus"]
#output_array << corpus_from_json
maincorpus = get_maincorpus(corpus_from_json)
#conll_file.puts "#sent_id = #{unique_id}; de-dem-dom_id = #{match_end-1}"
tokens = hit["tokens"]
#dword = ""
#dpos = ""
#ddeprel = ""
words = []
tokens.each.with_index do |token, tindex|
# id = token["ref"].to_i
#
word = token["word"]
words << word
# pos = token["pos"]
# msd = token["msd"]
# lemma = token["lemma"]
# deprel = token["deprel"]
# dephead = token["dephead"].to_i
# conll_file.puts "#{id}\t#{word}\t#{lemma}\t#{pos}\t#{msd}\t#{deprel}\t#{dephead}"
# if id == match_end - 1
# dword = word
# dpos = pos
# ddeprel = deprel
# end
end
#conll_file.puts ""
sample_file.puts "#{unique_id}\t#{match_end-1}\t#{words.join(" ")}\t#{maincorpus}\t#{corpus_from_json}\t#{year}"
end
else
STDERR.puts "Error! #{filename2}"
#errorlist.puts "#{corpus}\t#{searchyear}\t#{searchmonth}"
end
end #corpora
end
# STDERR.puts "Discarded:#{discarded}"
#end