Debian Codegraph @

java/src/main/org/numbertext/Numbertext.java

raw
1 /* See numbertext.org
2 * 2009-2010 (c) László Németh
3 * License: LGPL/BSD dual license */
4
5 package org.numbertext;
6
7 import static org.numbertext.MenuState.LANGUAGE;
8 import static org.numbertext.MenuState.PARAM;
9 import static org.numbertext.MenuState.PREFIX;
10
11 import java.io.BufferedReader;
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 public class Numbertext {
20
21 private static final Pattern LANG_PATTERN_NO = Pattern.compile("n[bn]([-_]NO)?");
22
23 private static Map<String, Soros> modules = new HashMap<String, Soros>();
24
25 private static Soros load(String langfile, String langcode) {
26 final Soros s;
27 try (InputStream input = Numbertext.class.getResourceAsStream("data/" + langfile + ".sor");
28 BufferedReader f = new BufferedReader(new InputStreamReader(input, "UTF-8"));) {
29 final StringBuilder builder = new StringBuilder();
30 String line = null;
31 while ((line = f.readLine()) != null) {
32 builder.append(line);
33 builder.append("\n");
34 }
35
36 s = new Soros(builder.toString(), langcode);
37 if (modules != null && langfile != null) {
38 modules.put(langcode, s);
39 }
40 } catch (Exception e) {
41 return null;
42 }
43 return s;
44 }
45
46 public static String numbertext(String input, String lang) {
47 Soros s = (Soros) modules.get(lang);
48 if (s == null) {
49 s = load(lang.replace('-', '_'), lang);
50 }
51 if (s == null) {
52 s = load(lang.replaceFirst("[-_].*", ""), lang);
53 }
54 if (s == null) {
55 // some exceptional language codes
56 // Norwegian....
57 Matcher m = LANG_PATTERN_NO.matcher(lang);
58 if (m.find()) {
59 s = load(m.replaceAll("no"), lang);
60 }
61 }
62 if (s == null) {
63 System.out.println("Missing language module: " + lang);
64 return null;
65 }
66 return s.run(input);
67 }
68
69 public static String moneytext(String input, String money, String lang) {
70 return numbertext(money + " " + input, lang);
71 }
72
73 private static void printHelp() {
74 System.out.println("Usage: java soros [-l lang] [-p prefix_function] [par1 [par2...]]");
75 System.out.println("Parameter: n: number; n-m: range; n-m~s: range with step");
76 System.out.println("Example: java -jar numbertext.jar -l en_US 99 # spell out number 99 in English");
77 System.out.println(" # spell out different ordinal numbers and number ranges");
78 System.out.println(" java -jar numbertext.jar -l en_US -p ordinal 1-10 500 1000-10000~1000");
79 System.out.println(" java -jar numbertext.jar -l en_US # print prefix functions of the language module");
80 System.out.println("License: GNU LGPL/BSD dual-license");
81 }
82
83 public static void main(String[] args) {
84 String lang = "en_US";
85 if (args.length == 0) {
86 printHelp();
87 return;
88 }
89
90 MenuState state = PARAM;
91 boolean missingNumbers = true;
92 String prefix = "";
93 for (int i = 0; i < args.length; i++) {
94 switch (state) {
95 case PARAM:
96 if (args[i].equals("-l")) {
97 state = LANGUAGE;
98 break;
99 } else if (args[i].equals("-p")) {
100 state = PREFIX;
101 break;
102 } else {
103 missingNumbers = false;
104 int idx = args[i].indexOf('-', 1);
105 if (idx > -1) {
106 int b = Integer.parseInt(args[i].substring(0, idx));
107 String e = args[i].substring(idx + 1);
108 int step = e.indexOf('~', idx);
109 int end;
110 if (step > -1) {
111 end = Integer.parseInt(e.substring(0, step));
112 step = Integer.parseInt(e.substring(step + 1));
113 } else {
114 step = 1;
115 end = Integer.parseInt(e);
116 }
117 for (int j = b; j <= end; j = j + step) {
118 System.out.println(numbertext(prefix + j, lang));
119 }
120 } else {
121 System.out.println(numbertext(prefix + args[i], lang));
122 }
123 }
124 break;
125 case LANGUAGE:
126 lang = args[i];
127 if (numbertext("1", lang) == null) {
128 System.exit(1);
129 }
130 state= PARAM;
131 break;
132 case PREFIX:
133 prefix = args[i] + " ";
134 state = PARAM;
135 break;
136 }
137 }
138 if (missingNumbers) {
139 System.out.println(numbertext("help", lang));
140 }
141 }
142 }