无法找到语法错​​误,意外的输入结束,期望keyword_end

时间:2016-07-18 23:29:23

标签: ruby syntax-error

有人可以帮我找到语法错​​误,它应该发生在第78行。此时我确信没有

#!/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

1 个答案:

答案 0 :(得分:0)

Ruby中的

if表达式不需要then

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

更新 - 虽然上述情况属实,但正如@JörgWMittag指出的那样,这不是问题的原因。实际问题在于if print_synsets do PP.pp(word_synsets) end。如他所述,使用then代替do将是此处的修复。