-
Notifications
You must be signed in to change notification settings - Fork 6
/
trials.R
183 lines (122 loc) · 4.35 KB
/
trials.R
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# translating into chinese or russian
library(translateVTT)
# https://stackoverflow.com/questions/18789330/r-on-windows-character-encoding-hell
translateVTT("L0.vtt", destLang = "zh-CN", apikey = k)
library(translateR)
getGoogleLanguages()
ret <- translate(content.vec = "This is my text",google.api.key = k,source.lang = "en", target.lang = "zh-CN")
# load library
library(translateR)
# return chinese tranlation
result_chinese <- translate(content.vec = "This is my Text",
google.api.key = api_key,
source.lang = "en",
target.lang = "zh-CN")
print(result_chinese)
# writing
write.table(result_chinese, "translation.txt")
iconv(x = "<U+8FD9>", from = "windows-1252", to = "UTF-8")
txt <- ret
rty <- file("test.txt",encoding="UTF-8")
writeLines(text = txt, con=rty, useBytes = T)
close(rty)
rty <- file("test.txt",encoding="UTF-8")
inp <- scan(rty,what=character())
#Read 1 item
close(rty)
inp
### BRUTEFORCE Solution!
# writing all encodings to vector
encodings <- iconvlist()
# prepare vector for results (never grow vector in R)
resultings <- vector(mode = "character", length = length(encodings))
# run the for loop to write all outcomes!
for (ENC in encodings) {
resultings[ENC] <- iconv(x = "<U+8FD9>", from = "ASCII", to = ENC)
}
be <- "<U+8FD9>"
txt <- "在"
writeLines(txt, "test.txt", useBytes=T)
cvb <- readLines("test.txt", encoding="UTF-8")
cvb
write.table(cvb, "test1.txt") # does not work :)
# trial
Sys.getlocale() # to get current locale
Sys.setlocale() # to set up current locale to be default of the system
Sys.setlocale(locale = "Chinese") # set locale to Chinese
# make trial now
write.table(cvb, "test2.txt") #
# Set locale back to English
Sys.setlocale() # set original system settings
Sys.setlocale(locale = "Hindi")
resultLocale <- Sys.setlocale(locale = "Hindi")
resultOK <- Sys.setlocale(locale = "Chinese")
Sys.getlocale()
Sys.setlocale(locale = "russian")
### Trial harvest and translate websites
## inspiration from: https://www.datacamp.com/community/tutorials/r-web-scraping-rvest
library(rvest)
# this url for the jobs website
# https://www.jobs.ch/en/vacancies/?region=13&term=
url <- "https://www.jobs.ch/en/vacancies/?region=13&term="
d1 <- url %>% read_html() %>% html_nodes("h2") %>% html_text()
d2 <- url %>% read_html() %>% html_nodes(".serp-item-head-3") %>% html_text()
### ===============================================
# General-purpose data wrangling
library(tidyverse)
# Parsing of HTML/XML files
library(rvest)
# String manipulation
library(stringr)
# Verbose regular expressions
library(rebus)
# Eases DateTime manipulation
library(lubridate)
# this url for the jobs website
# https://www.jobs.ch/en/vacancies/?region=13&term=
url <- "https://www.jobs.ch/en/vacancies/?region=13&term="
# first page
"https://www.jobs.ch/en/vacancies/?page=1®ion=13&term="
# last page
"https://www.jobs.ch/en/vacancies/?page=100®ion=13&term="
url1 <- "https://www.jobs.ch/en/vacancies/?page=2®ion=24&term="
# job title
job_data1 <- url1 %>%
read_html() %>%
html_nodes(".t--job-link") %>%
# Extract the raw text as a list
html_text()
# job company
job_data2 <- url1 %>%
read_html() %>%
html_nodes(".serp-item-head-2") %>% #.x--company-link
# Extract the raw text as a list
html_text()
# job description
job_data3 <- url1 %>%
read_html() %>%
html_nodes(".serp-item-head-3") %>%
# Extract the raw text as a list
html_text()
job_table <- data.frame(job_title = job_data1,
job_firma = job_data2,
job_descr = job_data3)
# function to ge the last page as a number
get_last_page <- function(url, class_html = 'x--paginator row'){
#url <- "https://www.jobs.ch/en/vacancies/?page=2®ion=24&term="
#class_html <- 'page hidden-xs'
pages_data <- url %>%
read_html() %>%
# The '.' indicates the class
html_nodes(class_html) %>%
# Extract the raw text as a list
html_text()
# The second to last of the buttons is the one
pages_data[(length(pages_data)-1)] %>%
# Take the raw string
unname() %>%
# Convert to number
as.numeric()
}
first_page <- read_html(url)
latest_page_number <- get_last_page(first_page)