-
-
Save tobynet/2273479 to your computer and use it in GitHub Desktop.
文を2つ入力して最初の文の名詞以外と2つめの文の名詞だけを使って文章を作るやつ
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
# -*- coding: utf-8 -*- | |
require 'open-uri' | |
require 'nokogiri' | |
require 'MeCab' | |
class WordsArray < Array | |
def nouns | |
select{ |w| | |
w.is_noun | |
} | |
end | |
end | |
class Word | |
attr_accessor :surface, :type | |
def self.parse text | |
list = WordsArray.new | |
tagger = MeCab::Tagger.new | |
node = tagger.parseToNode text | |
while node | |
if node.surface.length == 0 | |
node = node.next | |
next | |
end | |
surface = node.surface | |
surface.force_encoding 'utf-8' | |
feature = node.feature | |
feature.force_encoding 'utf-8' | |
word = Word.new | |
word.surface = surface | |
word.type = (feature.split /,/).first | |
list << word | |
node = node.next | |
end | |
list | |
end | |
def is_noun | |
type =~ /名詞/ | |
end | |
end | |
def random_text | |
(((Nokogiri open 'http://ja.wikipedia.org/wiki/Special:Randompage').search '#bodyContent p').map &:text).join "\n" | |
end | |
def create_nouns_table a, b | |
table = { } | |
nouns_b = b.nouns | |
a.nouns.each{ |n| | |
next if table[n.surface] | |
nb = nouns_b.shift | |
nouns_b << nb | |
table[n.surface] = nb.surface | |
} | |
table | |
end | |
text_a = ARGV.shift || random_text | |
text_b = ARGV.shift || random_text | |
words_a = Word.parse text_a | |
words_b = Word.parse text_b | |
nouns_table = create_nouns_table words_a, words_b | |
warn text_a | |
warn "----------------" | |
warn text_b | |
warn "----------------" | |
words_a.each{ |w| | |
if w.is_noun | |
print nouns_table[w.surface] | |
else | |
print w.surface | |
end | |
} | |
puts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment