首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将kanji数据库kradfile读入R:编码问题

将kanji数据库kradfile读入R:编码问题
EN

Stack Overflow用户
提问于 2022-03-18 02:41:49
回答 1查看 75关注 0票数 1

我试图像这样将kradfile加载到R中:

代码语言:javascript
复制
krad<-readLines("kradfile2",encoding = "UTF-8")
krad<-readLines("kradfile2",encoding = "EUC-JP")

但在正确阅读内容方面,两者似乎都不起作用。我不知道该如何处理编码才能正确读取它。该文件有以下说明:

代码语言:javascript
复制
[20] "# Two fonts were used in decomposition so as to include as many glyphs as"     
[21] "# possible.  One apparently based on the JIS X 0212 standard itself, and"      
[22] "# one based on Unicode.  Each JIS X 0212 kanji is represented by 3 bytes"      
[23] "# in EUC-JP encoding, as opposed to the two bytes used in the JIS X 0208"      
[24] "# range, so adjust your software accordingly if necessary."                  

编码的文本如下所示:

代码语言:javascript
复制
 : °ì Ò± °¢ : °ì ¡Ã °£ : °ì ¡Ã °¤ : °ì ¡Ã ¥Î °¥ : °ì Ц ¥Î Ò± °¦ : ÒÓ ²¦ °§ : °ì ¡Ã ¥Î ¸ý °¨ : °ì ¿Í ÑÄ °© : ¡Ã °ª : ¡Ã Ц ¥Î Öõ °« : ¡Ã Æó °¬ : ¡Ã ×Ä °­ : °ì ¡Ã Æó °® : °ì Ц ¥Î Öõ ½½ ´³ °¯ : Ц °° : °ì Ц °± : Ц ¥Î °² : °ì ¥Î ²µ °³ : ¡Ã Ц ¥Î °´ : ²µ °µ : ²µ Ò¸ °¶ : ²µ Öö °· : ²µ ÒÓ ¸ý °¸ : ²µ ËÎ ¸ý °¹ : °ì ¥Î ²µ Э ¶Ô °º : ²µ ½½ Ω ¿É °» : ²µ ½½ Æü °¼ :

帮助被接受了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-18 10:00:54

这个文件看起来有点奇怪,它是日本字符的JISX0208-1997编码和描述性文本和空格的普通的一个字节的ASCII的混合,所以看起来您需要检测和解码每个日本字符的字节对,而不是文件的其余部分。

例如,文件的第200行如下所示:

代码语言:javascript
复制
"\xb1\xa4 : \xb2\xbb \xb3\xad \xcc\xdc \xa5\xcf \xb8\xfd \xc6\xfc Ω"

我不知道JIS 0208编码,但我很确定这是不合法的:它是每个字符编码的2字节,而不仅仅是日本部分。因此,您需要将文件读入字节,并自己解析它们。

幸运的是,已经有人这样做了。您可以在这里获得UTF-8版本的文件:https://github.com/irrwahn/jiten-pai/blob/master/kradfile.utf8。这里还有另一个版本:https://github.com/jmettraux/kensaku/blob/master/data/kradfile-u

编辑后添加:

在注释中,您要求进行此转换的代码。正如我在这里所说的:方法是将文件读取为一个字节序列,并查看每个字节,将其描述为分隔符(空格、冒号、换行符)或JISX0208-1997字符。然后将这些JISX0208-1997字符转换为您可以使用的编码。第一部分相对容易;我不知道如何进行翻译,因为编码不存在于iconvlist()中。

下面是执行第一部分的代码:

代码语言:javascript
复制
bytes <- readBin("~/temp/kradfile", "int", size=1, signed=FALSE, n=1e6)

# The lines are separated by newline 0x0a values, and the main records
# have 2 byte characters followed by " : ", i.e. 0x20, 0x3a, 0x20.

newline <- 0x0a
space <- 0x20
colon <- 0x3a

newlines <- which(bytes == newline)

# Look through all the lines until we find the main records

for (start in newlines) {
  if (all(bytes[start + 3:5] == c(space, colon, space)))
    break
} 

# Only keep the newlines that start the main records

newlines <- newlines[newlines >= start]

# Now extract the data

result <- vector("list", length = length(newlines))

for (i in seq_along(newlines)) {
  if (i < length(newlines)) {
    kanji <- bytes[newlines[i] + 1:2]
    eol <- newlines[i+1]
    elements <- bytes[(newlines[i] + 6):(eol - 1)]
    # drop the spaces
    elements <- elements[elements != space]
    result[[i]] <- list(kanji = kanji, elements = elements)
  }
}

这最后是一个长列表,开头如下所示:

代码语言:javascript
复制
head(result, 2)
#> [[1]]
#> [[1]]$kanji
#> [1] 176 161
#> 
#> [[1]]$elements
#> [1] 161 195 176 236 184 253
#> 
#> 
#> [[2]]
#> [[2]]$kanji
#> [1] 176 162
#> 
#> [[2]]$elements
#> [1] 161 195 176 236 184 253

第一个汉字有176161字节,有三个元素: 161 195,176 236,184 253。将这些字符转换为可读字符需要一个JISX0208-1997编码表,而我没有这种编码。

第二版编辑:

经过一番搜索,我发现了以下内容。我们需要从kradfile中的字符中减去0x80,以获得实际的JISX0208编码。iconv()不支持该编码的1997年修订版,但它确实支持1990年版本,称之为"JIS_X0208-1990"。一个棘手的问题是,要处理多字节字符,每个字符的字节需要作为原始向量出现在列表中的条目中。下面是从原始文件中提取字符的最后一段代码:

代码语言:javascript
复制
for (i in seq_along(result)) {
  code <- as.raw(result[[i]]$kanji - 0x80)
  result[[i]]$kanji <- iconv(list(code), from = "JIS_X0208-1990", to="UTF-8")
  code <- as.raw(result[[i]]$elements - 0x80)
  result[[i]]$elements <- iconv(list(code), from = "JIS_X0208-1990", to="UTF-8")
}
head(result, 2)
#> [[1]]
#> [[1]]$kanji
#> [1] "亜"
#> 
#> [[1]]$elements
#> [1] "|一口"
#> 
#> 
#> [[2]]
#> [[2]]$kanji
#> [1] "唖"
#> 
#> [[2]]$elements
#> [1] "|一口"
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71521918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档