-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 Google Natural Language API を実装 (#102)
- Loading branch information
1 parent
ae1580d
commit c3ece3a
Showing
22 changed files
with
245 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/lib/google_natural_language_api/pickup_character_names.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# https://googleapis.dev/ruby/google-cloud-language/latest/file.MIGRATING.html | ||
# https://googleapis.dev/ruby/google-cloud-language-v1/latest/Google/Cloud/Language/V1/AnalyzeSyntaxResponse.html | ||
# https://cloud.google.com/natural-language/#natural-language-api-demo | ||
# GoogleNaturalLanguageApi::PickupCharacterNames.new.foobar | ||
|
||
require "google/cloud/language" | ||
|
||
module GoogleNaturalLanguageApi | ||
class PickupCharacterNames | ||
attr_reader :client | ||
|
||
def initialize | ||
@language = Google::Cloud::Language.language_service | ||
@client = Google::Cloud::Language.language_service | ||
end | ||
|
||
def create_analyze_syntax_object(tweet) | ||
response = analyze_tweet_syntax_by_api(tweet) | ||
|
||
ActiveRecord::Base.transaction do | ||
analyze_syntax = AnalyzeSyntax.new( | ||
language: response.language, | ||
sentences: response.sentences.map(&:to_json), | ||
tokens: response.tokens.map(&:to_json), | ||
tweet_id: tweet.id | ||
) | ||
|
||
analyze_syntax.save! | ||
end | ||
end | ||
|
||
def analyze_tweet_syntax_by_api(tweet) | ||
content = tweet.full_text | ||
document = { type: :PLAIN_TEXT, content: content } | ||
|
||
@language.analyze_syntax(document: document) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class AnalyzeSyntax < ApplicationRecord | ||
belongs_to :tweet | ||
|
||
def convert_analyze_syntax_response_sentence_objects | ||
hashed_sentences.map do |hashed_sentence| | ||
hashed_sentence.merge!(analyze_syntax_id: id) | ||
|
||
AnalyzeSyntaxResponse::Sentence.new(hashed_sentence) | ||
end | ||
end | ||
|
||
def convert_analyze_syntax_response_token_objects | ||
hashed_tokens.map do |hashed_token| | ||
hashed_token.merge!(analyze_syntax_id: id) | ||
|
||
AnalyzeSyntaxResponse::Token.new(hashed_token) | ||
end | ||
end | ||
|
||
def hashed_tokens | ||
tokens.map { |token| JSON.parse(token) } | ||
end | ||
|
||
def hashed_sentences | ||
sentences.map { |sentence| JSON.parse(sentence) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module AnalyzeSyntaxResponse | ||
class Sentence | ||
include ActiveModel::Model | ||
|
||
attr_accessor :text, :analyze_syntax_id | ||
|
||
def begin_offset | ||
text['beginOffset'] | ||
end | ||
|
||
def content | ||
text['content'] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module AnalyzeSyntaxResponse | ||
class Token | ||
include ActiveModel::Model | ||
|
||
# rubocop:disable Style/SymbolLiteral, Naming/MethodName, Layout/EmptyLinesAroundAttributeAccessor | ||
attr_accessor :text, :'partOfSpeech', :'dependencyEdge', :lemma, :analyze_syntax_id | ||
# rubocop:enable Style/SymbolLiteral, Naming/MethodName, Layout/EmptyLinesAroundAttributeAccessor | ||
|
||
def tag | ||
# 戻り値は Google::Cloud::Language::V1::AnalyzeSyntaxResponse では Symbol だが、これは String である | ||
part_of_speech['tag'] | ||
end | ||
|
||
def part_of_speech | ||
partOfSpeech | ||
end | ||
|
||
def dependency_edge | ||
dependencyEdge | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class CreateAnalyzeSyntaxes < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :analyze_syntaxes do |t| | ||
t.string :language | ||
|
||
# https://googleapis.dev/ruby/google-cloud-language-v1/latest/Google/Cloud/Language/V1/AnalyzeSyntaxResponse.html | ||
t.text :sentences, array: true # レスポンスの生ログを保存する目的 | ||
t.text :tokens, array: true # レスポンスの生ログを保存する目的 | ||
|
||
t.references :tweet | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
FactoryBot.define do | ||
factory :analyze_syntax do | ||
language { 'ja' } | ||
sentences { | ||
[ | ||
"{\"text\":{\"content\":\"RT @foobar: オデッサを応援しています \\n#幻水総選挙運動\\n #幻水総選挙2021 https://t.co/3njNheDvPk\",\"beginOffset\":-1}}" | ||
] | ||
} | ||
tokens { | ||
[ | ||
"{\"text\":{\"content\":\"オデッサ\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"NOUN\",\"proper\":\"PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":8,\"label\":\"DOBJ\"},\"lemma\":\"オデッサ\"}", | ||
"{\"text\":{\"content\":\"を\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"PRT\",\"case\":\"ACCUSATIVE\",\"proper\":\"NOT_PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":6,\"label\":\"PRT\"},\"lemma\":\"を\"}", | ||
"{\"text\":{\"content\":\"応援\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"NOUN\",\"proper\":\"NOT_PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":8,\"label\":\"ROOT\"},\"lemma\":\"応援\"}", | ||
"{\"text\":{\"content\":\"し\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"VERB\",\"form\":\"GERUND\",\"proper\":\"NOT_PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":8,\"label\":\"MWV\"},\"lemma\":\"する\"}", | ||
"{\"text\":{\"content\":\"て\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"PRT\",\"proper\":\"NOT_PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":8,\"label\":\"PRT\"},\"lemma\":\"て\"}", | ||
"{\"text\":{\"content\":\"い\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"VERB\",\"form\":\"GERUND\",\"proper\":\"NOT_PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":8,\"label\":\"AUXVV\"},\"lemma\":\"い\"}", | ||
"{\"text\":{\"content\":\"ます\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"VERB\",\"form\":\"ADNOMIAL\",\"proper\":\"NOT_PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":8,\"label\":\"AUX\"},\"lemma\":\"ます\"}", | ||
"{\"text\":{\"content\":\"#\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"X\",\"proper\":\"NOT_PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":17,\"label\":\"NN\"},\"lemma\":\"#\"}", | ||
"{\"text\":{\"content\":\"幻\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"NOUN\",\"proper\":\"PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":15,\"label\":\"NN\"},\"lemma\":\"幻\"}", | ||
"{\"text\":{\"content\":\"水\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"NOUN\",\"proper\":\"PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":17,\"label\":\"NN\"},\"lemma\":\"水\"}", | ||
"{\"text\":{\"content\":\"総\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"AFFIX\",\"proper\":\"PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":17,\"label\":\"PREF\"},\"lemma\":\"総\"}", | ||
"{\"text\":{\"content\":\"選挙\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"NOUN\",\"proper\":\"NOT_PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":18,\"label\":\"NN\"},\"lemma\":\"選挙\"}", | ||
"{\"text\":{\"content\":\"運動\",\"beginOffset\":-1},\"partOfSpeech\":{\"tag\":\"NOUN\",\"proper\":\"NOT_PROPER\"},\"dependencyEdge\":{\"headTokenIndex\":23,\"label\":\"NN\"},\"lemma\":\"運動\"}", | ||
] | ||
} | ||
# TODO: tweet_id { Tweet の Factory を作る } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe AnalyzeSyntax, type: :model do | ||
let(:analyze_syntax) { build(:analyze_syntax) } | ||
|
||
let(:hashed_sentences) { analyze_syntax.hashed_sentences } | ||
let(:hashed_tokens) { analyze_syntax.hashed_tokens } | ||
|
||
let(:tokens) { analyze_syntax.convert_analyze_syntax_response_token_objects } | ||
let(:sentences) { analyze_syntax.convert_analyze_syntax_response_sentence_objects } | ||
|
||
describe "#hashed_tokens" do | ||
it 'tokens が Array in Hash で戻ってくること' do | ||
expect(hashed_tokens.instance_of?(Array)).to be_truthy | ||
expect(hashed_tokens.map(&:class).all? { |klass| klass == Hash }).to be_truthy | ||
end | ||
end | ||
|
||
describe "#hashed_sentences" do | ||
it 'sentences が Array in Hash で戻ってくること' do | ||
expect(hashed_sentences.instance_of?(Array)).to be_truthy | ||
expect(hashed_sentences.map(&:class).all? { |klass| klass == Hash }).to be_truthy | ||
end | ||
end | ||
|
||
describe "#convert_analyze_syntax_response_token_objects" do | ||
it 'tokens が Array in AnalyzeSyntaxResponse::Token で戻ってくること' do | ||
expect(tokens.instance_of?(Array)).to be_truthy | ||
expect(tokens.map(&:class).all? { |klass| klass == AnalyzeSyntaxResponse::Token }).to be_truthy | ||
end | ||
end | ||
|
||
describe "#convert_analyze_syntax_response_sentence_objects" do | ||
it 'sentences が Array in AnalyzeSyntaxResponse::Sentence で戻ってくること' do | ||
expect(sentences.instance_of?(Array)).to be_truthy | ||
expect(sentences.map(&:class).all? { |klass| klass == AnalyzeSyntaxResponse::Sentence }).to be_truthy | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Asset, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe DirectMessage, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Hashtag, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe InTweetUrl, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Mention, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Tweet, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe User, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |
Oops, something went wrong.