Problem with Traditional and Simplified Chinese parsing in Pharo
Hi, I am currently parsing lua and JSON-like file in Pharo. They contain both Simplified and Traditional Chinese characters for comments and for strings displayed in the UI. Lua files are parsed correctly. However the JSON-like files aren't. In attachment I put one of the problematic file with Simplified chinese characters (I've also copied the file at the end of the mail). The problem can be shown as follow in Pharo: 'schinese.txt' asFileReference readStream contents The contents function sends a UTF8InvalidText 'Invalid utf8 input detected' error. However my text editor correctly displays the file and it is correctly parsed by the Lua runtime (The program parsed has been deployed in production for years and works fine). What can I do to parse this file correctly from Pharo ? Thanks, Below is the file content, non Chinese people may not have the font to display the characters, note that I have no idea what is written in Chinese (Please don't hold me responsible if there are offending contents): "lang" { "Language" "Schinese" "Tokens" { "text_store_cd" "è´å£³ååºçåå伿¯å¤©éæºå·æ°! 䏿¬¡å·æ°å·å´æ¶é´" "text_cannot_huidaoguoqu" "ç°å¨ä¸è½ä½¿ç¨åå°è¿å»~" "tips1" "å©ç¨é¼ æ æ»è½®å¯ä»¥è°èè§è§è·ç¦»ï¼æ¹ä¾¿ä½ æ¥çåºå°å ¨è²~" "tips2" "éè¿å ¨é¨50å ³ä»¥åä½ è¿å¯ä»¥ç»§ç»æææ´å æé¾åº¦çæ å°½è¯ç¼æ¨¡å¼ï¼" "tips3" "æ¯å¤©æ©æ¨è´å£³ååºä¼éæºå·æ°åéæºææï¼çå¿ä½ æ³è¦çååï¼" "tips4" "æè¡æ¦çå25åå¯ä»¥è·å¾çå å¥ç« ï¼è±¡å¾çä½ å¨å¡é²æ¸¸æä¸çåè¶æç»©ï¼" "tips5" "æ¯10æ³¢æäººä¼æä¸ä¸ªBOSSå ³å¡ï¼å®æ¯æ®éæäººæ´é¾å»æ~" "tips6" "é£è¡çæäººä¸ä¼è¢«ç³å¤´é»æ¡ï¼æä»¥ä¸è½å©ç¨è¿·å®«æ¥å¢é¿æªç©ç线路~" "tips7" "å¼å±çæ¶åç¹å»å·¦ä¸æ¹çè±é鿩徿 å¯ä»¥æ¥çå¹¶éæ©ä½ æ¥æçè±éï¼" "tips8" "é身çæäººå¿ é¡»éè¿èç½ç³»åå¡çç §æå ç¯æè½è¢«åç°ï¼" "tips9" "ç¹å»å³ä¾§çåæå ¬å¼æé®å¯ä»¥æå¼åæé¢æ¿ï¼äºè§£é«çº§å¡çåæä»¥åå½åé ä»¶ç¶æ~" "tips10" "é请好åä¸èµ·æ¸¸æï¼å¯ä»¥äºç¸åä½å ±å对æå¼ºå¤§çæäººï¼" "tips11" "ç³æ¿ä¼å¯¹è¸©ä¸å»çæäººäº§çææï¼æä»¥æå¥½æ¾ç½®å¨æäººå¿ ç»ç路线ä¸~" "tips12" "为è±éè´ä¹°ç¾ä¸½çç¹æï¼å½ä½ å¯ä»¥ä¸åååæçæ¶åææé ä»¶é½ä¼æç¹ææç¤ºï¼" "tips13" "æ¯åå伤害æé«çå¡å°ä¼è·å¾æå¤10å±çMVPå ç¯ï¼å¢å ç©çåéæ³è¾åºï¼" "tips14" "æ¯ä¸ªæçæåä¸å¤©æ¯èµå£ç»ç®æ¥ï¼å°ä¼æ ¹æ®ä½ è¿èµå£çæåé¢å丰åçè´å£³å¥å±ï¼" "tips15" "妿ä¸ç¥éææ ·é 迷宫ï¼ä½ å¯ä»¥ç¹å»å³ä¾§ç迷宫æåæé®æ¥çæè å享æ¨èç迷宫~" } } -- Clément Béra Pharo consortium engineer https://clementbera.wordpress.com/ Bâtiment B 40, avenue Halley 59650 Villeneuve d'Ascq
Are you sure that the file is encoded in utf8? Can you try stream := ZnCharacterReadStream on: (File named: '...') readStream encoding: 'utf8'. stream upToEnd. ? If that does not work, it could mean that the file is in another encoding... 2018-01-29 18:49 GMT+01:00 Clément Bera <bera.clement@gmail.com>:
Hi,
I am currently parsing lua and JSON-like file in Pharo. They contain both Simplified and Traditional Chinese characters for comments and for strings displayed in the UI. Lua files are parsed correctly. However the JSON-like files aren't.
In attachment I put one of the problematic file with Simplified chinese characters (I've also copied the file at the end of the mail). The problem can be shown as follow in Pharo:
'schinese.txt' asFileReference readStream contents
The contents function sends a UTF8InvalidText 'Invalid utf8 input detected' error. However my text editor correctly displays the file and it is correctly parsed by the Lua runtime (The program parsed has been deployed in production for years and works fine).
What can I do to parse this file correctly from Pharo ?
Thanks,
Below is the file content, non Chinese people may not have the font to display the characters, note that I have no idea what is written in Chinese (Please don't hold me responsible if there are offending contents):
"lang" { "Language" "Schinese" "Tokens" { "text_store_cd" "è´å£³ååºçåå伿¯å¤©éæºå·æ°! 䏿¬¡å·æ°å·å´æ¶é´" "text_cannot_huidaoguoqu" "ç°å¨ä¸è½ä½¿ç¨åå°è¿å»~" "tips1" "å©ç¨é¼ æ æ»è½®å¯ä»¥è°èè§è§è·ç¦»ï¼æ¹ä¾¿ä½ æ¥çåºå°å ¨è²~" "tips2" "éè¿å ¨é¨50å ³ä»¥åä½ è¿å¯ä»¥ç»§ç»æææ´å æé¾åº¦çæ å°½è¯ç¼æ¨¡å¼ï¼" "tips3" "æ¯å¤©æ©æ¨è´å£³ååºä¼éæºå·æ°åéæºææï¼çå¿ä½ æ³è¦çååï¼" "tips4" "æè¡æ¦çå25åå¯ä»¥è·å¾çå å¥ç« ï¼è±¡å¾çä½ å¨å¡é²æ¸¸æä¸çåè¶æç»©ï¼" "tips5" "æ¯10æ³¢æäººä¼æä¸ä¸ªBOSSå ³å¡ï¼å®æ¯æ®éæäººæ´é¾å»æ~" "tips6" "é£è¡çæäººä¸ä¼è¢«ç³å¤´é»æ¡ï¼æä»¥ä¸è½å©ç¨è¿·å®«æ¥å¢é¿æªç©ç线路~" "tips7" "å¼å±çæ¶åç¹å»å·¦ä¸æ¹çè±é鿩徿 å¯ä»¥æ¥çå¹¶éæ©ä½ æ¥æçè±éï¼" "tips8" "é身çæäººå¿ é¡»éè¿èç½ç³»åå¡çç §æå ç¯æè½è¢«åç°ï¼" "tips9" "ç¹å»å³ä¾§çåæå ¬å¼æé®å¯ä»¥æå¼åæé¢æ¿ï¼äºè§£é«çº§å¡çåæä»¥åå½åé ä»¶ç¶æ~" "tips10" "é请好åä¸èµ·æ¸¸æï¼å¯ä»¥äºç¸åä½å ±å对æå¼ºå¤§çæäººï¼" "tips11" "ç³æ¿ä¼å¯¹è¸©ä¸å»çæäººäº§çææï¼æä»¥æå¥½æ¾ç½®å¨æäººå¿ ç»ç路线ä¸~" "tips12" "为è±éè´ä¹°ç¾ä¸½çç¹æï¼å½ä½ å¯ä»¥ä¸åååæçæ¶åææé ä»¶é½ä¼æç¹ææç¤ºï¼" "tips13" "æ¯åå伤害æé«çå¡å°ä¼è·å¾æå¤10å±çMVPå ç¯ï¼å¢å ç©çåéæ³è¾åºï¼" "tips14" "æ¯ä¸ªæçæåä¸å¤©æ¯èµå£ç»ç®æ¥ï¼å°ä¼æ ¹æ®ä½ è¿èµå£çæåé¢å丰åçè´å£³å¥å±ï¼" "tips15" "妿ä¸ç¥éææ ·é 迷宫ï¼ä½ å¯ä»¥ç¹å»å³ä¾§ç迷宫æåæé®æ¥çæè å享æ¨èç迷宫~" } }
-- Clément Béra Pharo consortium engineer https://clementbera.wordpress.com/ Bâtiment B 40, avenue Halley 59650 Villeneuve d'Ascq
-- Guille Polito Research Engineer Centre de Recherche en Informatique, Signal et Automatique de Lille CRIStAL - UMR 9189 French National Center for Scientific Research - *http://www.cnrs.fr <http://www.cnrs.fr>* *Web:* *http://guillep.github.io* <http://guillep.github.io> *Phone: *+33 06 52 70 66 13
Your file is not in UTF-8 but in UTF-16 ! This will do: (FileLocator desktop / 'schinese.txt') readStreamDo: [ :in | (ZnCharacterReadStream on: in binary encoding: #utf16) upToEnd ]. BTW, this is not valid JSON.
From Windows, for sure ...
On 29 Jan 2018, at 18:49, Clément Bera <bera.clement@gmail.com> wrote:
Hi,
I am currently parsing lua and JSON-like file in Pharo. They contain both Simplified and Traditional Chinese characters for comments and for strings displayed in the UI. Lua files are parsed correctly. However the JSON-like files aren't.
In attachment I put one of the problematic file with Simplified chinese characters (I've also copied the file at the end of the mail). The problem can be shown as follow in Pharo:
'schinese.txt' asFileReference readStream contents
The contents function sends a UTF8InvalidText 'Invalid utf8 input detected' error. However my text editor correctly displays the file and it is correctly parsed by the Lua runtime (The program parsed has been deployed in production for years and works fine).
What can I do to parse this file correctly from Pharo ?
Thanks,
Below is the file content, non Chinese people may not have the font to display the characters, note that I have no idea what is written in Chinese (Please don't hold me responsible if there are offending contents):
"lang" { "Language" "Schinese" "Tokens" { "text_store_cd" "è´å£³ååºçåå伿¯å¤©éæºå·æ°! 䏿¬¡å·æ°å·å´æ¶é´" "text_cannot_huidaoguoqu" "ç°å¨ä¸è½ä½¿ç¨åå°è¿å»~" "tips1" "å©ç¨é¼ æ æ»è½®å¯ä»¥è°èè§è§è·ç¦»ï¼æ¹ä¾¿ä½ æ¥çåºå°å ¨è²~" "tips2" "éè¿å ¨é¨50å ³ä»¥åä½ è¿å¯ä»¥ç»§ç»æææ´å æé¾åº¦çæ å°½è¯ç¼æ¨¡å¼ï¼" "tips3" "æ¯å¤©æ©æ¨è´å£³ååºä¼éæºå·æ°åéæºææï¼çå¿ä½ æ³è¦çååï¼" "tips4" "æè¡æ¦çå25åå¯ä»¥è·å¾çå å¥ç« ï¼è±¡å¾çä½ å¨å¡é²æ¸¸æä¸çåè¶æç»©ï¼" "tips5" "æ¯10æ³¢æäººä¼æä¸ä¸ªBOSSå ³å¡ï¼å®æ¯æ®éæäººæ´é¾å»æ~" "tips6" "é£è¡çæäººä¸ä¼è¢«ç³å¤´é»æ¡ï¼æä»¥ä¸è½å©ç¨è¿·å®«æ¥å¢é¿æªç©ç线路~" "tips7" "å¼å±çæ¶åç¹å»å·¦ä¸æ¹çè±é鿩徿 å¯ä»¥æ¥çå¹¶éæ©ä½ æ¥æçè±éï¼" "tips8" "é身çæäººå¿ é¡»éè¿èç½ç³»åå¡çç §æå ç¯æè½è¢«åç°ï¼" "tips9" "ç¹å»å³ä¾§çåæå ¬å¼æé®å¯ä»¥æå¼åæé¢æ¿ï¼äºè§£é«çº§å¡çåæä»¥åå½åé ä»¶ç¶æ~" "tips10" "é请好åä¸èµ·æ¸¸æï¼å¯ä»¥äºç¸åä½å ±å对æå¼ºå¤§çæäººï¼" "tips11" "ç³æ¿ä¼å¯¹è¸©ä¸å»çæäººäº§çææï¼æä»¥æå¥½æ¾ç½®å¨æäººå¿ ç»ç路线ä¸~" "tips12" "为è±éè´ä¹°ç¾ä¸½çç¹æï¼å½ä½ å¯ä»¥ä¸åååæçæ¶åææé ä»¶é½ä¼æç¹ææç¤ºï¼" "tips13" "æ¯åå伤害æé«çå¡å°ä¼è·å¾æå¤10å±çMVPå ç¯ï¼å¢å ç©çåéæ³è¾åºï¼" "tips14" "æ¯ä¸ªæçæåä¸å¤©æ¯èµå£ç»ç®æ¥ï¼å°ä¼æ ¹æ®ä½ è¿èµå£çæåé¢å丰åçè´å£³å¥å±ï¼" "tips15" "妿ä¸ç¥éææ ·é 迷宫ï¼ä½ å¯ä»¥ç¹å»å³ä¾§ç迷宫æåæé®æ¥çæè å享æ¨èç迷宫~" } }
-- Clément Béra Pharo consortium engineer https://clementbera.wordpress.com/ Bâtiment B 40, avenue Halley 59650 Villeneuve d'Ascq <schinese.txt>
Thanks all, Sven solution works. Yes this is not JSON it's some kind of JSON-like format (Is it yml ? I don't know it might be proprietary). I was naive and thought there was some metadata in files precising the encoding used, and that #readStream on FileReference was able to pick automatically the correct decoder. Obviously it isn't the case. Thanks anyway. 2018-01-29 18:59 GMT+01:00 Sven Van Caekenberghe <sven@stfx.eu>:
Your file is not in UTF-8 but in UTF-16 !
This will do:
(FileLocator desktop / 'schinese.txt') readStreamDo: [ :in | (ZnCharacterReadStream on: in binary encoding: #utf16) upToEnd ].
BTW, this is not valid JSON.
From Windows, for sure ...
On 29 Jan 2018, at 18:49, Clément Bera <bera.clement@gmail.com> wrote:
Hi,
I am currently parsing lua and JSON-like file in Pharo. They contain both Simplified and Traditional Chinese characters for comments and for strings displayed in the UI. Lua files are parsed correctly. However the JSON-like files aren't.
In attachment I put one of the problematic file with Simplified chinese characters (I've also copied the file at the end of the mail). The problem can be shown as follow in Pharo:
'schinese.txt' asFileReference readStream contents
The contents function sends a UTF8InvalidText 'Invalid utf8 input detected' error. However my text editor correctly displays the file and it is correctly parsed by the Lua runtime (The program parsed has been deployed in production for years and works fine).
What can I do to parse this file correctly from Pharo ?
Thanks,
Below is the file content, non Chinese people may not have the font to display the characters, note that I have no idea what is written in Chinese (Please don't hold me responsible if there are offending contents):
"lang" { "Language" "Schinese" "Tokens" { "text_store_cd" "è´å£³ååºçåå伿¯å¤©éæºå·æ°! 䏿¬¡å·æ°å·å´æ¶é´" "text_cannot_huidaoguoqu" "ç°å¨ä¸è½ä½¿ç¨åå°è¿å»~" "tips1" "å©ç¨é¼ æ æ»è½®å¯ä»¥è°èè§è§è·ç¦»ï¼æ¹ä¾¿ä½ æ¥çåºå°å ¨è²~" "tips2" "éè¿å ¨é¨50å ³ä»¥åä½ è¿å¯ä»¥ç»§ç»æææ´å æé¾åº¦çæ å°½è¯ç¼æ¨¡å¼ï¼" "tips3" "æ¯å¤©æ©æ¨è´å£³ååºä¼éæºå·æ°åéæºææï¼çå¿ä½ æ³è¦çååï¼" "tips4" "æè¡æ¦çå25åå¯ä»¥è·å¾çå å¥ç« ï¼è±¡å¾çä½ å¨å¡é²æ¸¸æä¸çåè¶æç»©ï¼" "tips5" "æ¯10æ³¢æäººä¼æä¸ä¸ªBOSSå ³å¡ï¼å®æ¯æ®éæäººæ´é¾å»æ~" "tips6" "é£è¡çæäººä¸ä¼è¢«ç³å¤´é»æ¡ï¼æä»¥ä¸è½å©ç¨è¿·å®«æ¥å¢é¿æªç©ç线路~" "tips7" "å¼å±çæ¶åç¹å»å·¦ä¸æ¹çè±é鿩徿 å¯ä»¥æ¥çå¹¶éæ©ä½ æ¥æçè±éï¼" "tips8" "é身çæäººå¿ é¡»éè¿èç½ç³»åå¡çç §æå ç¯æè½è¢«åç°ï¼" "tips9" "ç¹å»å³ä¾§çåæå ¬å¼æé®å¯ä»¥æå¼åæé¢æ¿ï¼äºè§£é«çº§å¡çåæä»¥åå½åé ä»¶ç¶æ~" "tips10" "é请好åä¸èµ·æ¸¸æï¼å¯ä»¥äºç¸åä½å ±å对æå¼ºå¤§çæäººï¼" "tips11" "ç³æ¿ä¼å¯¹è¸©ä¸å»çæäººäº§çææï¼æä»¥æå¥½æ¾ç½®å¨æäººå¿ ç»ç路线ä¸~" "tips12" "为è±éè´ä¹°ç¾ä¸½çç¹æï¼å½ä½ å¯ä»¥ä¸åååæçæ¶åææé ä»¶é½ä¼æç¹ææç¤ºï¼" "tips13" "æ¯åå伤害æé«çå¡å°ä¼è·å¾æå¤10å±çMVPå ç¯ï¼å¢å ç©çåéæ³è¾åºï¼" "tips14" "æ¯ä¸ªæçæåä¸å¤©æ¯èµå£ç»ç®æ¥ï¼å°ä¼æ ¹æ®ä½ è¿èµå£çæåé¢å丰åçè´å£³å¥å±ï¼" "tips15" "妿ä¸ç¥éææ ·é 迷宫ï¼ä½ å¯ä»¥ç¹å»å³ä¾§ç迷宫æåæé®æ¥çæè å享æ¨èç迷宫~" } }
-- Clément Béra Pharo consortium engineer https://clementbera.wordpress.com/ Bâtiment B 40, avenue Halley 59650 Villeneuve d'Ascq <schinese.txt>
Le 29/01/2018 à 18:49, Clément Bera a écrit :
Hi,
I am currently parsing lua and JSON-like file in Pharo. They contain both Simplified and Traditional Chinese characters for comments and for strings displayed in the UI. Lua files are parsed correctly. However the JSON-like files aren't.
In attachment I put one of the problematic file with Simplified chinese characters (I've also copied the file at the end of the mail). The problem can be shown as follow in Pharo:
'schinese.txt' asFileReference readStream contents
The contents function sends a UTF8InvalidText 'Invalid utf8 input detected' error. However my text editor correctly displays the file and it is correctly parsed by the Lua runtime (The program parsed has been deployed in production for years and works fine).
What can I do to parse this file correctly from Pharo ?
Hi, Here is how we manage encoding in Moose following Sven's advices. We detect the encoding of a file with this code: [ self fileReference binaryReadStreamDo: [ :in | (ZnCharacterEncoder detectEncoding: in upToEnd) identifier ] ] on: ZnCharacterEncodingError do: [ nil ] It is not bullet proof but I never got a problem since we use it. Then to read a file we do this: self fileReference binaryReadStreamDo: [ :in | (ZnCharacterReadStream on: in encoding: self encoding) upToEnd ] Here, self encoding return the result of the previous snippet. Or you can use it this way: ZnCharacterEncoder detectEncoding: ((FileLocator desktop / 'some.data') binaryReadStreamDo: [ :in | in upToEnd ]). (FileLocator desktop / 'some.data') binaryReadStreamDo: [ :in | | bytes encoder | bytes := in upToEnd. encoder := ZnCharacterEncoder detectEncoding: bytes. encoder decodeBytes: bytes ].
Thanks,
Below is the file content, non Chinese people may not have the font to display the characters, note that I have no idea what is written in Chinese (Please don't hold me responsible if there are offending contents):
"lang" { "Language""Schinese" "Tokens" { "text_store_cd""è´å£³ååºçåå伿¯å¤©éæºå·æ°! 䏿¬¡å·æ°å·å´æ¶é´" "text_cannot_huidaoguoqu""ç°å¨ä¸è½ä½¿ç¨åå°è¿å»~" "tips1""å©ç¨é¼ æ æ»è½®å¯ä»¥è°èè§è§è·ç¦»ï¼æ¹ä¾¿ä½ æ¥çåºå°å ¨è²~" "tips2""éè¿å ¨é¨50å ³ä»¥åä½ è¿å¯ä»¥ç»§ç»æææ´å æé¾åº¦çæ å°½è¯ç¼æ¨¡å¼ï¼" "tips3""æ¯å¤©æ©æ¨è´å£³ååºä¼éæºå·æ°åéæºææï¼çå¿ä½ æ³è¦çååï¼" "tips4""æè¡æ¦çå25åå¯ä»¥è·å¾çå å¥ç« ï¼è±¡å¾çä½ å¨å¡é²æ¸¸æä¸çåè¶æç»©ï¼" "tips5""æ¯10æ³¢æäººä¼æä¸ä¸ªBOSSå ³å¡ï¼å®æ¯æ®éæäººæ´é¾å»æ~" "tips6""é£è¡çæäººä¸ä¼è¢«ç³å¤´é»æ¡ï¼æä»¥ä¸è½å©ç¨è¿·å®«æ¥å¢é¿æªç©ç线路~" "tips7""å¼å±çæ¶åç¹å»å·¦ä¸æ¹çè±é鿩徿 å¯ä»¥æ¥çå¹¶éæ©ä½ æ¥æçè±éï¼" "tips8""é身çæäººå¿ é¡»éè¿èç½ç³»åå¡çç §æå ç¯æè½è¢«åç°ï¼" "tips9""ç¹å»å³ä¾§çåæå ¬å¼æé®å¯ä»¥æå¼åæé¢æ¿ï¼äºè§£é«çº§å¡çåæä»¥åå½å é ä»¶ç¶æ~" "tips10""é请好åä¸èµ·æ¸¸æï¼å¯ä»¥äºç¸åä½å ±å对æå¼ºå¤§çæäººï¼" "tips11""ç³æ¿ä¼å¯¹è¸©ä¸å»çæäººäº§çææï¼æä»¥æå¥½æ¾ç½®å¨æäººå¿ ç»ç路线ä¸~" "tips12""为è±éè´ä¹°ç¾ä¸½çç¹æï¼å½ä½ å¯ä»¥ä¸åååæçæ¶åææé ä»¶é½ä¼æç¹ ææç¤ºï¼" "tips13""æ¯åå伤害æé«çå¡å°ä¼è·å¾æå¤10å±çMVPå ç¯ï¼å¢å ç©çåéæ³è¾åºï¼" "tips14""æ¯ä¸ªæçæåä¸å¤©æ¯èµå£ç»ç®æ¥ï¼å°ä¼æ ¹æ®ä½ è¿èµå£çæåé¢å丰åç è´å£³å¥å±ï¼" "tips15""妿ä¸ç¥éææ ·é 迷宫ï¼ä½ å¯ä»¥ç¹å»å³ä¾§ç迷宫æåæé®æ¥çæè å享 æ¨èç迷宫~" } }
-- Clément Béra Pharo consortium engineer https://clementbera.wordpress.com/ Bâtiment B 40, avenue Halley 59650 Villeneuve d'Ascq
-- Cyril Ferlicot https://ferlicot.fr
participants (5)
-
Clément Bera -
Clément Béra -
Cyril Ferlicot D. -
Guillermo Polito -
Sven Van Caekenberghe