2017-02-16 53 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