Difference between revisions of "OpasnetUtils/Translate"
From Testiwiki
(raakile) |
(function added) |
||
Line 1: | Line 1: | ||
+ | [[Category:Opasnet]] | ||
+ | [[Category:R-tools]] | ||
+ | {{method|moderator=Jouni}} | ||
+ | |||
+ | == Question == | ||
+ | |||
+ | How to automatically translate texts in ovariables and graphs from one language to another? | ||
+ | |||
+ | == Answer == | ||
+ | |||
+ | Use the translate function (will be put as a part of OpasnetUtils package at some point). It goes through index names and locations and replaces terms in one language with those of another. The matching terms must be given in a data.frame called ''dictionary'', where each language has a column. If no dictionary exists, the t2b table from this page will be used. | ||
+ | |||
+ | == Rationale == | ||
+ | |||
+ | Data table for the default dictionary. | ||
+ | |||
<t2b name="Dictionary" index="Finnish,Swedish" obs="English" unit="-"> | <t2b name="Dictionary" index="Finnish,Swedish" obs="English" unit="-"> | ||
− | Builders| | + | Rakennuttaja||Builders |
− | Building owner| | + | Rakennuksen omistaja||Building owner |
− | + | Energiansäästöpolitiikka||EnergySavingPolicy | |
− | PlantPolicy | + | Voimalapolitiikka||PlantPolicy |
− | + | Tätä menoa||BAU | |
− | Energy saving moderate| | + | Kohtuullinen energiansäästö||Energy saving moderate |
− | Energy saving total| | + | Täysi energiansäästö||Energy saving total |
− | Apartment houses| | + | Kerrostalot||Apartment houses |
− | + | Kaupalliset||Commercial | |
− | Detached houses| | + | Omakotitalot||Detached houses |
− | + | Opetusala||Educational | |
− | Health and social sector| | + | Terveys- ja sosiaaliala||Health and social sector |
− | + | Teollisuus||Industrial | |
− | Leisure houses| | + | Mökki||Leisure houses |
− | + | Toimistot||Offices | |
− | + | Muu||Other | |
− | + | Julkinen||Public | |
− | Row houses| | + | Rivitalot||Row houses |
− | + | Urheilu||Sports | |
− | + | Perinteinen||Traditional | |
− | + | Vanha||Old | |
− | + | Uusi||New | |
− | Low-energy| | + | Matalaenerginen||Low-energy |
− | Passive | + | Passiivitalo||Passive |
− | + | Ei mitään||None | |
− | General| | + | Yleinen||General |
− | + | Ikkunat||Windows | |
− | Techical systems | + | Tekniset||Techical systems |
− | + | Seinät ja katto||Sheath reform | |
− | Biofuel heat plants| | + | Biolämpölaitokset||Biofuel heat plants |
− | CHP diesel generators| | + | CHP dieselgeneraattorit||CHP diesel generators |
− | Deep-drill heat | + | Syväporattu maalämpö||Deep-drill heat |
− | + | Omaan käyttöön||Domestic | |
− | Hanasaari|Hanasaari | + | Hanasaari||Hanasaari |
− | Hanasaari biofuel renovation| | + | Hanasaari bio||Hanasaari biofuel renovation |
− | Household air heat pumps| | + | Kotitalouden ilmalämpöpumppu||Household air heat pumps |
− | Household air conditioning| | + | Kotitalouden ilmastointi||Household air conditioning |
− | Household geothermal heat| | + | Kotitalouden maalämpö||Household geothermal heat |
− | Household solar | + | Kotitalouden aurinkovoima||Household solar |
− | Katri Vala | + | Katri Vala viilennys||Katri Vala cooling |
− | Katri Vala | + | Katri Vala lämpö||Katri Vala heat |
− | Kellosaari back-up plant| | + | Kellosaaren varavoimala||Kellosaari back-up plant |
− | Kymijoki River's plants| | + | Kymijoen vesivoimalat||Kymijoki River's plants |
− | Loviisa nuclear heat| | + | Loviisan ydinvoimalämpö||Loviisa nuclear heat |
− | Neste oil refinery heat| | + | Nesteen öljyjalostamolämpö||Neste oil refinery heat |
− | Other | + | Muu||Other |
− | Salmisaari A&B|Salmisaari A&B | + | Salmisaari A&B||Salmisaari A&B |
− | Salmisaari biofuel renovation| | + | Salmisaari biokorjaus||Salmisaari biofuel renovation |
− | Sea heat pump| | + | Merilämpö||Sea heat pump |
− | Sea heat pump for cooling| | + | Merilämpö viilennykseen||Sea heat pump for cooling |
− | Small-scale wood burning| | + | Puun pienpoltto||Small-scale wood burning |
− | Small fuel oil heat plants| | + | Pienet öljylämpölaitokset||Small fuel oil heat plants |
− | Small gas heat plants| | + | Pienet kaasulämpölaitokset||Small gas heat plants |
− | Suvilahti power storage| | + | Suvilahden voimavaraaja||Suvilahti power storage |
− | Suvilahti solar | + | Suvilahden aurinkovoima||Suvilahti solar |
− | Vuosaari A&B|Vuosaari A&B | + | Vuosaari A&B||Vuosaari A&B |
− | Vuosaari C | + | Vuosaari C bio||Vuosaari C biofuel |
− | Wind mills| | + | Tuulivoimalat||Wind mills |
− | + | Sähkö||Electricity | |
− | + | Lämpö||Heat | |
− | + | Biopolttoaine||Biofuel | |
− | + | Kivihiili||Coal | |
− | Fuel oil| | + | Polttoöljy||Fuel oil |
− | + | Maakaasu||Gas | |
− | Light oil| | + | Kevytöljy||Light oil |
− | + | Puu||Wood | |
− | + | Kaukolämpö||District | |
− | + | Sähkölämmitys||Electricity | |
− | + | Öljy||Oil | |
− | + | Muu||Other | |
− | + | Päätöksentekijä||Decision_maker | |
− | + | Päätös||Decision | |
− | + | Vaihtoehto||Option | |
− | + | Rakennus||Building | |
− | + | Tehokkuus||Efficiency | |
− | + | Korjaukset||Renovation | |
− | + | Voimala||Plant | |
− | + | Polttoaine||Fuel | |
− | Heating | + | Lämmitys||Heating |
</t2b> | </t2b> | ||
+ | |||
+ | <rcode name="translate" label="Initiate translate (developers only)"> | ||
+ | ### This is code Op_en7392/translate on page [[OpasnetUtils/Translate]]. | ||
+ | |||
+ | library(OpasnetUtils) | ||
+ | |||
+ | translate <- function( # Previously suomenna | ||
+ | x, # object to be translated (ovariable, data.frame, or vector) | ||
+ | lang = NULL, # language to translate to. | ||
+ | dict = NULL # t2b containing the dictionary | ||
+ | ) { | ||
+ | if(is.null(lang)) if(exists("language")) lang <- language else lang <- "English" | ||
+ | if(!is.null(dict) | !exists("dictionary")) { | ||
+ | if(is.null(dict)) dict <- "Op_en7392.dictionary" | ||
+ | dictionary <- tidy(opbase.data(dict)) | ||
+ | colnames(dictionary)[colnames(dictionary) == "Result"] <- "English" | ||
+ | } | ||
+ | require(reshape2) | ||
+ | dic <- melt(dictionary, id.vars = lang, value.name = "From") | ||
+ | |||
+ | tr <- function(a) { | ||
+ | if(is.numeric(a)) return(a) | ||
+ | if(is.factor(a)) temp <- levels(a) else temp <- a | ||
+ | out <- as.character(dic[[lang]][match(temp, dic$From)]) | ||
+ | out[is.na(out)] <- temp[is.na(out)] | ||
+ | if(is.factor(a)) { | ||
+ | out2 <- a; levels(out2) <- out | ||
+ | } else { | ||
+ | out2 <- out | ||
+ | } | ||
+ | return(out2) | ||
+ | } | ||
+ | |||
+ | if(class(x) == "ovariable") { | ||
+ | colnames(x@output) <- tr(colnames(x@output)) | ||
+ | for(i in colnames(x@output)) { | ||
+ | x@output[[i]] <- tr(x@output[[i]]) | ||
+ | } | ||
+ | } | ||
+ | if(is.data.frame(x)) { | ||
+ | colnames(x) <- tr(colnames(x)) | ||
+ | for(i in colnames(x)) { | ||
+ | x[[i]] <- tr(x[[i]]) | ||
+ | } | ||
+ | } | ||
+ | if(is.vector(x)) { | ||
+ | x <- tr(x) | ||
+ | } | ||
+ | return(x) | ||
+ | } | ||
+ | |||
+ | objects.store(translate) | ||
+ | cat("Function translate stored.\n") | ||
+ | </rcode> | ||
+ | |||
+ | == See also == | ||
+ | |||
+ | == Related files == |
Revision as of 13:05, 4 October 2015
This page is a method.
The page identifier is Op_en7392 |
---|
Moderator:Jouni (see all) |
Give your opinion to the peer rating of the content of this page. |
Upload data
|
Question
How to automatically translate texts in ovariables and graphs from one language to another?
Answer
Use the translate function (will be put as a part of OpasnetUtils package at some point). It goes through index names and locations and replaces terms in one language with those of another. The matching terms must be given in a data.frame called dictionary, where each language has a column. If no dictionary exists, the t2b table from this page will be used.
Rationale
Data table for the default dictionary.
Obs | Finnish | Swedish | English |
---|---|---|---|
1 | Rakennuttaja | Builders | |
2 | Rakennuksen omistaja | Building owner | |
3 | Energiansäästöpolitiikka | EnergySavingPolicy | |
4 | Voimalapolitiikka | PlantPolicy | |
5 | Tätä menoa | BAU | |
6 | Kohtuullinen energiansäästö | Energy saving moderate | |
7 | Täysi energiansäästö | Energy saving total | |
8 | Kerrostalot | Apartment houses | |
9 | Kaupalliset | Commercial | |
10 | Omakotitalot | Detached houses | |
11 | Opetusala | Educational | |
12 | Terveys- ja sosiaaliala | Health and social sector | |
13 | Teollisuus | Industrial | |
14 | Mökki | Leisure houses | |
15 | Toimistot | Offices | |
16 | Muu | Other | |
17 | Julkinen | Public | |
18 | Rivitalot | Row houses | |
19 | Urheilu | Sports | |
20 | Perinteinen | Traditional | |
21 | Vanha | Old | |
22 | Uusi | New | |
23 | Matalaenerginen | Low-energy | |
24 | Passiivitalo | Passive | |
25 | Ei mitään | None | |
26 | Yleinen | General | |
27 | Ikkunat | Windows | |
28 | Tekniset | Techical systems | |
29 | Seinät ja katto | Sheath reform | |
30 | Biolämpölaitokset | Biofuel heat plants | |
31 | CHP dieselgeneraattorit | CHP diesel generators | |
32 | Syväporattu maalämpö | Deep-drill heat | |
33 | Omaan käyttöön | Domestic | |
34 | Hanasaari | Hanasaari | |
35 | Hanasaari bio | Hanasaari biofuel renovation | |
36 | Kotitalouden ilmalämpöpumppu | Household air heat pumps | |
37 | Kotitalouden ilmastointi | Household air conditioning | |
38 | Kotitalouden maalämpö | Household geothermal heat | |
39 | Kotitalouden aurinkovoima | Household solar | |
40 | Katri Vala viilennys | Katri Vala cooling | |
41 | Katri Vala lämpö | Katri Vala heat | |
42 | Kellosaaren varavoimala | Kellosaari back-up plant | |
43 | Kymijoen vesivoimalat | Kymijoki River's plants | |
44 | Loviisan ydinvoimalämpö | Loviisa nuclear heat | |
45 | Nesteen öljyjalostamolämpö | Neste oil refinery heat | |
46 | Muu | Other | |
47 | Salmisaari A&B | Salmisaari A&B | |
48 | Salmisaari biokorjaus | Salmisaari biofuel renovation | |
49 | Merilämpö | Sea heat pump | |
50 | Merilämpö viilennykseen | Sea heat pump for cooling | |
51 | Puun pienpoltto | Small-scale wood burning | |
52 | Pienet öljylämpölaitokset | Small fuel oil heat plants | |
53 | Pienet kaasulämpölaitokset | Small gas heat plants | |
54 | Suvilahden voimavaraaja | Suvilahti power storage | |
55 | Suvilahden aurinkovoima | Suvilahti solar | |
56 | Vuosaari A&B | Vuosaari A&B | |
57 | Vuosaari C bio | Vuosaari C biofuel | |
58 | Tuulivoimalat | Wind mills | |
59 | Sähkö | Electricity | |
60 | Lämpö | Heat | |
61 | Biopolttoaine | Biofuel | |
62 | Kivihiili | Coal | |
63 | Polttoöljy | Fuel oil | |
64 | Maakaasu | Gas | |
65 | Kevytöljy | Light oil | |
66 | Puu | Wood | |
67 | Kaukolämpö | District | |
68 | Sähkölämmitys | Electricity | |
69 | Öljy | Oil | |
70 | Muu | Other | |
71 | Päätöksentekijä | Decision_maker | |
72 | Päätös | Decision | |
73 | Vaihtoehto | Option | |
74 | Rakennus | Building | |
75 | Tehokkuus | Efficiency | |
76 | Korjaukset | Renovation | |
77 | Voimala | Plant | |
78 | Polttoaine | Fuel | |
79 | Lämmitys | Heating |