Hi,

Some time ago I asked for help on coding J in Chinese :), that, since I have
very little time it seems hard for me to learn J dictionary by heart. I'm
also a Smalltalker so I finally decide to utilize Smalltalk in helping me
get into J paradise.

I wrote a short translator today for fun, and now I'm able to type Chinese-J
in Smalltalk IDE and translate them to whatever J code... that I know :(

Here is an example, which also gerenrated from a former topic here I asked
for help.
The result has whole original ChineseJ in front of translated codes and
looks like:

NB.     NB.讀入數據文件
NB.     rddt =: 3 : 0
NB.     試讀四八數據檔 <y NB.從y中讀入4byte int數據再分成八列
NB.     )
NB.     tr =: 3 : ' 均增首行 y ' NB.取首行增一求均值
NB.     tc =: 3 : ' 求均 末列 y ' NB.取末列求均值
NB.     m=. rddt 'D:\jcb_gfzq\vipdoc\sh\lday\sh600000.day'
NB.     tr m
NB.     tc m
NB.     

NB.----------------------
NB.讀入數據文件
rddt =: 3 : 0 
(_8]\ _2(3!:4) 1!:1) <y NB.從y中讀入4byte int數據再分成八列 
) 
tr =: 3 : ' (+/%#)>:{. y ' NB.取首行增一求均值 
tc =: 3 : ' (+/%#) {:"1 y ' NB.取末列求均值 
m=. rddt 'D:\jcb_gfzq\vipdoc\sh\lday\sh600000.day' 
tr m 
tc m 


In fact, I wish I could code the thing in J. However, when I learn how to do
it some day, I'll not need that. Anyway, here is the source:

From VisualWorks® NonCommercial, 7.6 of March 3, 2008 on September 14, 2009
at 8:39:31 pm

 Package ACJ(0.1, emptist)= 
Namespace Smalltalk.中文J

        Private: false
        Imports: 
                        private Smalltalk.*
                        
        Attributes: #(#(#package 'ACJ'))

Class 中文J.中譯J

        Superclass: Core.Object
        Type: none
        Instance variables: 原文 譯文 
        Class instance variables: 字典 
        Attributes: #(#(#package 'ACJ'))

中文J.中譯J class methods for 'instance creation'

保存字典
        
        self 字典存為: self 字典文名

查閱修改字典
        
        ^字典 inspect

程序文件: aString
        "aString是完整路徑"
        
        ^self new 程序文件: aString

讀入字典: filename
        
        | 全文 詞義 |
        全文 := (filename asFilename withEncoding: #UTF8) contentsOfEntireFile
                readStream.
        [全文 atEnd]
                whileFalse:
                        [詞義 := 全文 nextLine tokensBasedOn: Character tab.
                        self 字典
                                at: 詞義 first asSymbol
                                put: 詞義 last]

字典文名
        
        ^'cj.dict'

預設字典
        
        字典
                at: #均 put: '(+/%#)';
                at: #加 put: '+'

程序: aString
        
        ^self new 翻譯: aString

字典存為: aString
        
        | 文 |
        文 := (aString asFilename withEncoding: #UTF8) writeStream.
        [字典
                keysAndValuesDo:
                        [:詞 :義 | 
                        文
                                nextPutAll: 詞 asString;
                                tab;
                                nextPutAll: 義;
                                cr]] ensure: [文 close]

字典
        
        ^字典
                ifNil:
                        [字典 := Dictionary new.
                        [self 讀入字典: self 字典文名]
                                on: Error
                                do: [self 預設字典].
                        字典]

"
self 字典 
"

查: 字
        "此處可增加保存字典動作"   

        ^self 字典
                at: 字
                ifAbsentPut: [Dialog request: '''' , 字 , '''J語言代碼是什麼?' 
initialAnswer:字]

中文J.中譯J class methods for '舉例'

例1
        "輸入中文詞語翻譯成J詞語,注意這是指動詞連詞副詞部分"
        
        self 試譯:'NB.讀入通達信數據文件
rddt =: 3 : 0
試讀四八數據檔 <y NB.從y中讀入4byte int數據再分成八列
)
tr =: 3 : '' 均增首行 y '' NB.取首行增一求均值
tc =: 3 : '' 求均 末列 y '' NB.取末列求均值
m=. rddt ''D:\jcb_gfzq\vipdoc\sh\lday\sh600000.day''
tr m
tc m
'

試譯: aString
        "輸入中文詞語翻譯成J詞語,注意這是指動詞連詞副詞部分"
        
        (self 程序: aString) 對照 inspect

中文J.中譯J methods for 'accessing'

對照
        
        ^String
                streamContents:
                        [:s | 
                        (原文 tokensBasedOn: Character cr)
                                do:
                                        [:行 | 
                                        s
                                                nextPutAll: 'NB.';
                                                tab;
                                                nextPutAll: 行;
                                                cr].
                        s
                                cr;
                                nextPutAll: 'NB.----------------------';
                                cr;
                                nextPutAll: 譯文;
                                cr]

譯文
        ^譯文

原文
        ^原文

中文J.中譯J methods for '翻譯'

翻譯: 中文
        "輸入中文詞語翻譯成J詞語,注意這是指動詞連詞副詞部分"
        "括號前後須空格"
        
        | 句 兩半 中文Stream |
        原文 := 中文.
        中文Stream := 中文 readStream.
        譯文 := String
                streamContents:
                        [:j | 
                        [中文Stream atEnd]
                                whileFalse:
                                        [句 := 中文Stream nextLine.
                                        (句 isEmpty or: [(句 first: 3) = 'NB.'])
                                                ifTrue: [j nextPutAll: 句]
                                                ifFalse:
                                                        ["j
                                                                nextPutAll: 
'NB.' , 句;
                                                                cr."
                                                        兩半 := 句 tokensBasedOn: 
'NB.'.
                                                        (兩半 first 
tokensBasedOn: Character space)
                                                                do:
                                                                        [:詞 | 
                                                                        j
                                                                                
nextPutAll:
                                                                                
                (詞 first value < 128
                                                                                
                                ifFalse: [詞 asJ]
                                                                                
                                ifTrue: [詞]);
                                                                                
space].
                                                        兩半 size = 2 ifTrue: [j 
nextPutAll: 'NB. ' , 兩半 last]].
                                        j cr]].
        ^self

程序文件: aString
        "aString是完整路徑"
        
        ^self 翻譯: (aString asFilename withEncoding: #UTF8) contentsOfEntireFile

Core.String methods for '*中文J'

eachJ
        "逐字翻譯"
        
        ^String
                streamContents: [:s | self do: [:字 | s nextPutAll: 字 asSymbol 
j]]

j
        
        ^中文J.中譯J 查: self

加括號
        "對phrases加括號,若不用此功能,註釋此行:"
        
        ^'(' , self , ')'

asJ
        "視設定字詞為整體,餘則拆分"
        
        ^(self prefixMatchesRegex: '試|求|擬|將|且|欲')
                ifTrue: [self asSymbol j 加括號]
                ifFalse: [self eachJ trimBlanks]        "
'擬是求' prefixMatchesRegex: '求|擬|欲|將|且'

"


-- 
View this message in context: 
http://www.nabble.com/finally%2C-I%27ve-made-tiny-Smalltalk-program-for-ChineseJ-tp25435672s24193p25435672.html
Sent from the J Chat mailing list archive at Nabble.com.

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to