2017-02-16 35 views
1

我目前正在尝试使用Stanford CoreNLP管道来运行情感分析。我需要它遍历个别文本文件(其中包含电影评论)的文件夹,以确定每个评论的情感。我试图创建一个批处理脚本,以遍历包含评论的3个不同文件夹。通过壳浇道程序收到以下错误运行脚本时:“f此时意外” - bash脚本

f为在这个时候意外

脚本如下:

dir=C:\stanford-corenlp-full-2016-10-31 
for f in "$dir\train\neg" && 
    for f in "$dir\train\pos" && 
     for f in "$dir\train\unsup" ; do 
    echo $f >> filelist.txt 
    java -mx8g edu.stanford.nlp.sentiment.SentimentTraining -numHid 25 -trainPath filelist.txt -devPath dev.txt -train -model model.ser.gz 
done 

这是第一bash脚本我曾经写过,所以我假设我的语法可能不正确的地方?

我也是使用的是Windows 10

任何意见将是惊人的,

千恩万谢

嗨你的建议一直是非常有用的。试图让我的生活变得更轻松我已经尝试将我的脚本转换为批处理脚本,以便它不应该在Windows上运行时遇到任何问题。我的新脚本如下所示:

 @echo off 
dir=C:\stanford-corenlp-full-2016-10-31 
for %%f in "%dir\train\neg" & "%dir\train\pos" & "%dir\train\unsup" do 
    ECHO %%f >> filelist.txt 
    java -mx8g edu.stanford.nlp.sentiment.SentimentTraining -numHid 25 -trainPath filelist.txt -devPath dev.txt -train -model model.ser.gz 
done 
pause 

这将导致以下错误: “%DIR \火车站\ POS”在这个时候

人明白我做错了意外?我假设这是某种语法问题,我只是看不到它。

+1

我确定那不是批处理文件。批处理变量由['set'](https://ss64.com/nt/set.html)命令设置,由'%var%'使用,而不是'$ var',就像在bash中一样。 ['for'](https://ss64.com/nt/for.html)不会以'done'结尾。在批处理中没有';'(实际上是,但不会结束语句) –

+0

因此,您正在使用Linux Windows子系统? – Squashman

+0

对不起,我应该更好地解释一下,我正在使用Windows 10,但我已经安装了cygwin,以便能够使用linux命令更灵活地使用我的bash脚本。 – user7575479

回答

0

你不需要多对的,你可以把所有的项目的第一个这样的后:

for f in "$dir\train\neg" "$dir\train\pos" "$dir\train\unsup" ; do 

甚至:

for f in "$dir\train\"{neg,pos,unsup} 
do 
    .... 
done 

另外,我想你会需要更换所有单斜杠“\”与两个“\\”。但不能肯定地说,因为我没有Windows。

所以它可能是

dir=C:\\stanford-corenlp-full-2016-10-31 

for f in "$dir\\train\\"{neg,pos,unsup} 
do 
    ... 
done 

它目前还不清楚是否:你

  • 需要调用Java 3次(一次为每个目录)

或者:

  • 你需要把所有然后在末尾调用Java

在filelist中只用一个目录调用Java 3次的示例。每次TXT:

for f in "$dir\\train\\"{neg,pos,unsup} 
do 
    #note this ">" will over-write the file each time 
    echo "$f" > filelist.txt 
    java .... filelist.txt 
done 

创建3个目录列表文件,然后调用Java一次:

# note this ">" will overwrite the file with nothing 
# so it will then be empty 
# so that it doesn't have what's left over from last time 
# that you ran the script 
> filelist.txt 

for f in "$dir\\train\\"{neg,pos,unsup} 
do 
     #note this ">>" will add a line to the file 
     echo "$f" >> filelist.txt 
done 

# call Java after you've finished creating the file 
java ..... filelist.txt 

,你需要

但是,你甚至不需要环路(假设你在这样的环境提供给您的“LS”命令)

dir=C:\\stanford-corenlp-full-2016-10-31 

ls -1 -d "$dir\\train\\"{neg,pos,unsup} > filelist.txt 

java -mx8g edu.stanford.nlp.sentiment.SentimentTraining -numHid 25 -trainPath filelist.txt -devPath dev.txt -train -model model.ser.gz 

注意 {neg,pos,unsup} 语法将在bash中工作,但不能在windows bat文件中工作

+0

感谢您的意见,我已采​​取了所有的建议,但无济于事。我认为我可能有可能做了其他事情错误,或者这可能不是我的系统:( – user7575479

+0

除非你安装了“cygwin”,我认为这不是!'Bash'是Linux和Unix和Mac的脚本语言。安装Cygwin在Windows上提供)你可以在Windows中使用“bat”脚本来做你需要的东西,但是你是否认为你可以用文本编辑器(notepad.exe)创建filelist.txt文件夹并将其放入文件夹中然后你可以在你的Java命令里面有一个.bat文件,然后双击那个 – Chunko

+0

哦,你有cygwin.just看到上面的内容。你现在得到了什么错误信息? – Chunko