Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何用谷歌&x27;使用ruby的NLAPI?_Ruby On Rails_Ruby_Count_Google Cloud Nl - Fatal编程技术网

Ruby on rails 如何用谷歌&x27;使用ruby的NLAPI?

Ruby on rails 如何用谷歌&x27;使用ruby的NLAPI?,ruby-on-rails,ruby,count,google-cloud-nl,Ruby On Rails,Ruby,Count,Google Cloud Nl,尝试使用ruby计算Google NL api在段落中出现的名词数量 正在查找文档,找不到如何执行此操作 昨晚想出一个解决办法 text = 'xxxxxxxxxx' response = language.analyze_syntax content: text, type: :PLAIN_TEXT sentences = response.sentences tokens = response.tokens x= tokens.count a = Array.new(x-1) f

尝试使用ruby计算Google NL api在段落中出现的名词数量

正在查找文档,找不到如何执行此操作

昨晚想出一个解决办法

text = 'xxxxxxxxxx'
response = language.analyze_syntax content: text, type: :PLAIN_TEXT

sentences = response.sentences
tokens    = response.tokens
x= tokens.count

a = Array.new(x-1)

for i in 1..x
    a[i-1] = tokens[i-1].part_of_speech.tag.to_s
end
for i in 1..x
    if a[i-1] == 'NOUN' 

    num= num+1
    end
end

仍然想知道NLAPI中是否存在类似(tokens.noon.count?)的东西

以您的示例为基础,您可以这样计算
名词的数量:

text = 'xxxxxxxxxx'
response = language.analyze_syntax content: text, type: :PLAIN_TEXT

tokens = response.tokens

tokens.count { |t| t.part_of_speech.tag.to_s == 'NOUN' }
请注意,在ruby中,像这样使用迭代器是非常常见的样式,而不是定义临时变量和使用
for
循环。(事实上,)


这种简洁明了的语法是ruby语言最大的吸引力之一。通常,您可以在一行这样的代码中获得所需的结果,而不是所有这些临时数组和索引计数器声明。

只是试图找到一个解决方案。到目前为止,您做了哪些尝试?StackOverflow不是一个免费的代码编写服务(这就是为什么你会被否决);你需要表现出更多的努力。你在用什么图书馆?您引用的是什么文档?你有部分解决方案吗?不正确的结果?错误消息?有什么事吗?好的。我只是想出了一个局部的解决方案。我会把它贴在原籍上。一看
tokens.select{| t | t.part_of_speech.tag.to_s=='NOUN'}.count
?只需要代码的前4行;其余的可以删除。(我假设你是在用
句子
做其他事情,再往下看?)它是有效的。谢谢。是的,我还需要一些句子来表达另一个意思:谢谢。这项研究真的很有帮助。