【问题标题】:Unable to read temp file using r programming, file encoding error无法使用 r 编程读取临时文件,文件编码错误
【发布时间】:2018-03-05 21:04:30
【问题描述】:

使用 R 编程

我有两组数据(securityj 和 securityc)。我想找到它们之间的cosine相似度值

我通过lsa library使用了这段代码

databasfile = tempfile()
dir.create(databasfile)
write( databasej, file=paste(databasfile, "D1", sep="/"))
write( databasec, file=paste(databasfile, "D2", sep="/"))
myMatrix = textmatrix(databasfile)

databaseRes <- lsa::cosine(myMatrix[,1], myMatrix[,2])

securityfile = tempfile()
dir.create(securityfile)

write( securityj, file=paste(securityfile, "D1", sep="/"))
write( securityc, file=paste(securityfile, "D2", sep="/"))
securityMatrix = textmatrix(securityfile)

securityRes <- lsa::cosine(securityMatrix[,1], securityMatrix[,2])

运行(textmatrix(securityfile))时出现此错误

FUN(X[[i]], ...) 中的错误: [lsa] - 由于文件的编码问题,无法打开文件 C:\Users\AAA\AppData\Local\Temp\RtmpIDmcl7\file1898438fde2/D1。

在处理数据库文件时它运行得非常好,但是使用安全文件时我有错误,并且数据是从同一个原始文件中获取的。 问题是我创建了文件然后立即读取它。 我尝试更改原始文件编码并确保它是 UTF-8 但没有任何改变

textmatrixlsa library中的一个函数。我的数据是从干净的招聘广告中提取的两个二元组列表, (databasej ,databasec) 和 (securityj,securityc) 都来自同一个文本文件,它在第一个文件中有效,但在第二个文件中出现错误。 对于分隔符 sep="/" ,它与文档中想要的函数相同。

securityj 中的示例输入

 [333] "risk assessment"               "beginning darkmatter"         
 [335] "best practices"                "create dream"                 
 [337] "darkmatter agile"              "darkmatter bring"             
 [339] "darkmatter impossible"         "darkmatter place"             
 [341] "drive lead"                    "education drive"              
 [343] "experience education"          "forensic analysis"            
 [345] "freedom create"                "knowledge network"            
 [347] "lead missing"                  "missing freedom"              
 [349] "offers personal"               "perl python"                  
 [351] "related security"              "security risks"               
 [353] "standard operating"            "windows linux"                
 [355] "security controls"             "systems security"             
 [357] "advice guidance"               "application penetration"      
 [359] "certified information"         "forensics malware"            
 [361] "guidance areas"                "networks applications"        
 [363] "new era"                       "practice advice"              
 [365] "provisioning best"             "security certified"           
 [367] "web application"               "government oil"               
 [369] "kill chain"                    "network based"                
 [371] "risk assessments"              "technical experience"         
 [373] "audit compliance"              "business units"               

【问题讨论】:

  • 函数textmatrix从何而来?寻求帮助时,您应该包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出。
  • 我编辑了问题

标签: r cosine-similarity file-encodings


【解决方案1】:

如果没有reproducible example,很难评估这个问题,包括看起来像用户定义函数textmatrix 的源代码。

唯一让我大吃一惊的是,您创建的文件非常奇怪。您正在创建一个有效但随机的目录,然后看起来您正在尝试使用错误的分隔符将两个文件放在该目录中(您的文件分隔符是反斜杠,并且您正在尝试使用在目录中添加文件正斜杠)。根据 testmatrix 是什么(它对传递给它的字符向量参数的作用)以及 databasejdatabasec 的结构是什么,它可能能够理解数据库案例中的文件,但是不是安全案例。但这是没有可重复示例的猜测。您可以尝试使用带有内置变量.Platform$file.sep 的独立于平台的文件分隔符,或者如果您只是在本地运行它,请将其与您的文件分隔符匹配,即\ 而不是/。如果这有效,那么万岁。如果没有,试着写一个可复现的例子,你可能会得到更好的帮助~

【讨论】:

  • 我说我使用 lsa 库来计算余弦相似度,textmatrix 是这个库中的一个函数。
  • 能否请您附上reproducible example
  • 所以看起来 textmatrix 需要一个包含文件的目录。我不能告诉你为什么它能够解析数据库文件,因为我不知道它们是什么,但我可以告诉你它不能解析安全文件并不奇怪,因为它们没有使用正确的文件分隔器。您是否尝试过为您的平台使用正确的文件分隔符?
  • 我说我正在使用 lsa 库来计算余弦相似度,textmatrix是这个库中的一个函数。我的数据是从清理过的招聘广告中提取的两个二元组列表,奇怪的是 (databasej ,databasec) 和 (securityj,securityc) 都来自同一个文本文件,它在第一个文件中有效,但在第二个文件中出现错误.对于分隔符,它与文档中所需的功能相同。还需要哪些细节?
【解决方案2】:

我将文件编码更改为 ANSI,它可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-30
    • 2014-12-06
    • 2021-06-26
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多