2016-07-18 25 views
-3

を期待し、入力終了予期しないが、その私がそこにISN確信しています。この時点で、ライン78上に発生によってはずは、構文エラーを見つけることができません、誰かが私に構文エラーを見つけるのに役立つてもらえkeyword_end

#!/usr/bin/env ruby 

$LOAD_PATH.unshift "lib" 
require 'wordnet' 
require 'pp' 


print_synsets = false 



lex = WordNet::Lexicon.new 

puts "Hello! I'm Complainbot. To stop talking to enter \'cya\'. What would you like to complain about today?\n\n" 
user_response = gets.chomp 

while not user_response.include? "cya" do 
    puts 

    if is_command?(user_response) 
     handle_commands(user_response) 
    else 
     establish_context(user_response) 
    end 

    puts 

    user_response = gets.chomp 
end 



def establish_context(sentence) 
    nouns = get_nouns(sentence) 
    puts nouns 
end 


def get_nouns(sentence) 
    words = sentence.strip.split(' ') 
    nouns_in_sentence = [] 

    words.each do |word| 
     word_synsets = lex.lookup_synsets(word) 

     if print_synsets do PP.pp(word_synsets) end 

     word_synsets.each do |synset| 
      nouns = synset.nouns 
      if not nouns.empty? 
       nouns_in_sentence.append(nouns.first) 
       break 
      end 
     end 
    end 
    return nouns_in_sentence 
end 


def is_command?(sentence) 
    words = sentence.strip.split(' ') 

    if words.first[0].to_s == '\\' then 
     return true 
    else 
     return false 
    end 
end 


def handle_commands(commands) 
    if commands.include? 'PRINT_SYNSETS' 
     print_synsets = true 
    elsif commands.include? '~PRINT_SYNSETS' 
     print_synsets = false 
    end 
end 
Rubyで
+0

こんにちは、正しくフォーマットされるように、すべてのコードを4つのスペースにインデントする必要があります。 –

+0

ブロックのキーワードは 'do' _only_のままにすることをお勧めします。 – Aetherus

+0

1行ブロックの場合は、 'do ... end'の代わりに中かっこを使用します。 – Aetherus

答えて

0

if式はthen

if words.first[0].to_s == '\\' 
    return true 
else 
    return false 
end 

UPDATEを必要としない - を上記の事実ですが、@JörgWMittagが指摘したように、これが問題の原因ではありません。実際の問題はif print_synsets do PP.pp(word_synsets) endです。彼が述べたように、doの代わりにthenを使用することがここでの修正になります。

+0

これはどのように受け入れられましたか?それは技術的に間違っていませんが、 'if'式(その部分は*間違っています、' if'はRubyの式であり、文ではありません)は* '*'を必要としません。 ';'や改行代わりに、しかしそれは*どちらも傷つけません、そして、この構文エラーの原因ではありません。まさに逆ですが、実際には、 'do'を使って' do'を使ってエラーを引き起こしています。正しい修正は 'do'の代わりに' then'を使うことです。 –

+0

うん、私はあなたが正しいと思う@JörgWMittag。あなたの訂正をありがとう! – larz

関連する問題