Git Product home page Git Product logo

Comments (2)

loretoparisi avatar loretoparisi commented on August 21, 2024

I'm not sure if it would work (like fs access), but I was trying Opal so using at least

http://cdn.opalrb.org/opal/current/opal.min.js
http://cdn.opalrb.org/opal/current/opal-parser.min.js

getting (from http://opalrb.org/try/#)

/* Generated by Opal 0.9.0 */
(function(Opal) {
    Opal.dynamic_require_severity = "error";
    var OPAL_CONFIG = {
        method_missing: true,
        arity_check: false,
        freezing: true,
        tainting: true
    };

    function $rb_plus(lhs, rhs) {
        return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs + rhs : lhs['$+'](rhs);
    }

    function $rb_minus(lhs, rhs) {
        return (typeof(lhs) === 'number' && typeof(rhs) === 'number') ? lhs - rhs : lhs['$-'](rhs);
    }
    var self = Opal.top,
        $scope = Opal,
        nil = Opal.nil,
        $breaker = Opal.breaker,
        $slice = Opal.slice,
        $klass = Opal.klass,
        $hash2 = Opal.hash2,
        $range = Opal.range;

    Opal.add_stubs(['$attr_accessor', '$open', '$gets', '$split', '$[]=', '$+', '$dirname', '$[]', '$sub', '$fetch', '$init_word_table', '$map!', '$to_proc', '$include?', '$ord', '$chop!', '$join', '$init_table', '$each', '$sprintf', '$first', '$unpack', '$<<', '$chomp', '$downcase!', '$capitalize!', '$to_i', '$tr!', '$to_sym', '$-', '$size', '$=~', '$!=', '$last', '$scan', '$alias_method']);
    return (function($base, $super) {
        function $Pinyin() {};
        var self = $Pinyin = $klass($base, $super, 'Pinyin', $Pinyin);

        var def = self.$$proto,
            $scope = self.$$scope;

        Opal.cdecl($scope, 'TONE_MARK', $hash2(["a", "o", "e", "i", "u", "v"], {
            "a": ["ā", "á", "ǎ", "à", "a"],
            "o": ["ō", "ó", "ǒ", "ò", "o"],
            "e": ["ē", "é", "ě", "è", "e"],
            "i": ["ī", "í", "ǐ", "ì", "i"],
            "u": ["ū", "ú", "ǔ", "ù", "u"],
            "v": ["ǖ", "ǘ", "ǚ", "ǜ", "ü"]
        }));

        return (function(self) {
            var $scope = self.$$scope,
                def = self.$$proto,
                TMP_6;

            self.$attr_accessor("table");
            self.$attr_accessor("ruby2");
            Opal.defn(self, '$init_table', function() {
                var $a, $b, TMP_1, self = this,
                    datfile = nil;
                if (self.table == null) self.table = nil;

                if ((($a = self.table) !== nil && (!$a.$$is_boolean || $a == true))) {
                    return nil
                };
                datfile = "pinyin-utf8.dat";
                self.table = $hash2([], {});
                return ($a = ($b = $scope.get('File')).$open, $a.$$p = (TMP_1 = function(file) {
                    var self = TMP_1.$$s || this,
                        $a, $b, $c, line = nil,
                        key = nil,
                        value = nil;
                    if (self.table == null) self.table = nil;
                    if (file == null) file = nil;
                    while ((($b = line = file.$gets()) !== nil && (!$b.$$is_boolean || $b == true))) {
                        $c = line.$split(" ", 2), $b = Opal.to_ary($c), key = ($b[0] == null ? nil : $b[0]), value = ($b[1] == null ? nil : $b[1]), $c;
                        self.table['$[]='](key, value);
                    }
                }, TMP_1.$$s = self, TMP_1), $a).call($b, $rb_plus($scope.get('File').$dirname("(file)"), "/../data/" + (datfile)));
            });
            Opal.defn(self, '$init_word_table', function() {
                var $a, $b, TMP_2, self = this;
                if (self.words_table == null) self.words_table = nil;

                if ((($a = self.words_table) !== nil && (!$a.$$is_boolean || $a == true))) {
                    return nil
                };
                self.words_table = $hash2([], {});
                if ((($a = $scope.get('ENV')['$[]']("WORDS_FILE")) !== nil && (!$a.$$is_boolean || $a == true))) {
                    return ($a = ($b = $scope.get('File')).$open, $a.$$p = (TMP_2 = function(file) {
                        var self = TMP_2.$$s || this,
                            $a, $b, $c, line = nil,
                            key = nil,
                            value = nil;
                        if (self.words_table == null) self.words_table = nil;
                        if (file == null) file = nil;
                        while ((($b = line = file.$gets()) !== nil && (!$b.$$is_boolean || $b == true))) {
                            $c = line.$sub("\n", "").$split("|", 2), $b = Opal.to_ary($c), key = ($b[0] == null ? nil : $b[0]), value = ($b[1] == null ? nil : $b[1]), $c;
                            self.words_table['$[]='](key, value);
                        }
                    }, TMP_2.$$s = self, TMP_2), $a).call($b, $scope.get('ENV')['$[]']("WORDS_FILE"))
                } else {
                    return nil
                };
            });
            Opal.defn(self, '$translate', TMP_6 = function(chars, options) {
                var $a, $b, $c, $d, TMP_3, $e, TMP_4, self = this,
                    $iter = TMP_6.$$p,
                    $yield = $iter || nil,
                    splitter = nil,
                    tonemarks = nil,
                    tone = nil,
                    camel = nil,
                    results = nil,
                    is_english = nil;
                if (self.words_table == null) self.words_table = nil;

                if (options == null) {
                    options = $hash2([], {})
                }
                TMP_6.$$p = null;
                splitter = options.$fetch("splitter", " ");
                tonemarks = options.$fetch("tonemarks", false);
                tone = options.$fetch("tone", ((($a = false) !== false && $a !== nil) ? $a : tonemarks));
                camel = options.$fetch("camelcase", false);
                self.$init_word_table();
                results = self.words_table['$[]'](chars);
                if (results !== false && results !== nil) {
                    results = results.$split();
                    ($a = ($b = results)['$map!'], $a.$$p = "downcase".$to_proc(), $a).call($b);
                    if (camel !== false && camel !== nil) {
                        ($a = ($c = results)['$map!'], $a.$$p = "capitalize".$to_proc(), $a).call($c)
                    };
                    if (tone !== false && tone !== nil) {} else {
                        ($a = ($d = results)['$map!'], $a.$$p = (TMP_3 = function(x) {
                            var self = TMP_3.$$s || this,
                                $a;
                            if (x == null) x = nil;
                            if ((($a = ($range(48, 57, false))['$include?'](x['$[]'](-1).$ord())) !== nil && (!$a.$$is_boolean || $a == true))) {
                                return x['$chop!']()
                            } else {
                                return x
                            }
                        }, TMP_3.$$s = self, TMP_3), $a).call($d)
                    };
                    return results.$join(splitter);
                };
                self.$init_table();
                results = [];
                is_english = false;
                ($a = ($e = chars.$scan(/./)).$each, $a.$$p = (TMP_4 = function(char$) {
                    var self = TMP_4.$$s || this,
                        $a, $b, TMP_5, key = nil,
                        pinyin = nil,
                        tone_index = nil;
                    if (self.ruby2 == null) self.ruby2 = nil;
                    if (self.table == null) self.table = nil;
                    if (char$ == null) char$ = nil;
                    key = (function() {
                        if ((($a = self.ruby2) !== nil && (!$a.$$is_boolean || $a == true))) {
                            return char$
                        } else {
                            return self.$sprintf("%X", char$.$unpack("U").$first())
                        };
                        return nil;
                    })();
                    if ((($a = self.table['$[]'](key)) !== nil && (!$a.$$is_boolean || $a == true))) {
                        if (is_english !== false && is_english !== nil) {
                            results['$<<'](splitter)
                        };
                        is_english = false;
                        pinyin = self.table['$[]'](key).$chomp().$split(" ", 2)['$[]'](0);
                        if ((($a = self.ruby2) !== nil && (!$a.$$is_boolean || $a == true))) {} else {
                            pinyin['$downcase!']()
                        };
                        if (tone !== false && tone !== nil) {} else {
                            pinyin['$chop!']()
                        };
                        if (camel !== false && camel !== nil) {
                            pinyin['$capitalize!']()
                        };
                        if (tonemarks !== false && tonemarks !== nil) {
                            tone_index = pinyin['$[]'](-1).$to_i();
                            pinyin = pinyin['$[]']($range(0, -1, true));
                            ($a = ($b = ["a", "o", "e", "i", "u", "v"]).$each, $a.$$p = (TMP_5 = function(v) {
                                var self = TMP_5.$$s || this,
                                    $a;
                                if (v == null) v = nil;
                                if ((($a = pinyin['$tr!'](v, $scope.get('TONE_MARK')['$[]'](v.$to_sym())['$[]']($rb_minus(tone_index, 1)))) !== nil && (!$a.$$is_boolean || $a == true))) {
                                    return ($breaker.$v = nil, $breaker)
                                } else {
                                    return nil
                                }
                            }, TMP_5.$$s = self, TMP_5), $a).call($b);
                        };
                        if (($yield !== nil)) {
                            return results['$<<']((((($a = Opal.yieldX($yield, [pinyin, results.$size()])) === $breaker) ? $breaker.$v : $a)))
                        } else {
                            results['$<<'](pinyin);
                            return results['$<<'](splitter);
                        };
                    } else {
                        if ((($a = char$['$=~'](/[a-zA-Z0-9]/)) !== nil && (!$a.$$is_boolean || $a == true))) {
                            results['$<<'](char$)
                        } else if ((($a = results.$last()['$!='](splitter)) !== nil && (!$a.$$is_boolean || $a == true))) {
                            results['$<<'](splitter)
                        };
                        return is_english = true;
                    };
                }, TMP_4.$$s = self, TMP_4), $a).call($e);
                return results.$join("").$chomp(splitter);
            });
            return self.$alias_method("t", "translate");
        })(Opal.get_singleton_class(self));
    })($scope.base, null)
})(Opal);

The source was a bit worst, so I have used a JavaScript Beautifier to make it more readable.

Also I'm not sure how Opal handles the file access here: datfile = "pinyin-utf8.dat";
The file compiles and run in JavaScript, so it seems to be formally correct. I have tried it in this JSFiddle

from chinese_pinyin.

loretoparisi avatar loretoparisi commented on August 21, 2024

So I removed the dictionary, just to make a test and replacing with some key value lines so that in the init_table method

def init_table
      return if @table

      @table  = {}

      @table["鼸"] = "xian3"
      @table["鼺"] = "yan3"
      @table["鼻"] = "lei2"
      @table["鼼"] = "yao3"

    end

But when I try to compile in Opal I get some errors now:

unpack: undefined method `unpack' for "中":String
unpack: undefined method `unpack' for "":String
    at $.$new (http://opalrb.org/javascripts/try.js:6:8830)
    at String.Opal.defn.TMP_5 (http://opalrb.org/javascripts/try.js:5:20896)
    at String.n [as $unpack] (http://opalrb.org/javascripts/try.js:4:24433)
    at eval (eval at <anonymous> (http://opalrb.org/javascripts/try.js:24:19155), <anonymous>:95:46)
    at $a.$$p.TMP_3 (eval at <anonymous> (http://opalrb.org/javascripts/try.js:24:19155), <anonymous>:96:28)
    at E.yield1 (http://opalrb.org/javascripts/try.js:4:29046)
    at Array.e.defn.y [as $each] (http://opalrb.org/javascripts/try.js:8:28187)
    at $.Opal.defn.TMP_5 (eval at <anonymous> (http://opalrb.org/javascripts/try.js:24:19155), <anonymous>:134:46)
    at eval (eval at <anonymous> (http://opalrb.org/javascripts/try.js:24:19155), <anonymous>:140:42)
    at eval (eval at <anonymous> (http://opalrb.org/javascripts/try.js:24:19155), <anonymous>:141:3)

This error unpack: undefined methodunpack'seems to be related to theOpal/Ruby` versions.
Reported in opal/opal#1413

from chinese_pinyin.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.