グカネータ改良 (#371) (#375)

Reviewed-on: #375
Co-authored-by: miteruzo <miteruzo@naver.com>
Co-committed-by: miteruzo <miteruzo@naver.com>
このコミットはPull リクエスト #375 でマージされました.
このコミットが含まれているのは:
2026-06-17 01:04:57 +09:00
committed by みてるぞ
コミット a54ca72244
21個のファイルの変更1699行の追加882行の削除
+58
ファイルの表示
@@ -125,6 +125,64 @@ npm run preview
- TypeScript and TSX use 4-space logical indentation. - TypeScript and TSX use 4-space logical indentation.
- In TypeScript and TSX only, replace every leading run of 8 spaces with a tab. - In TypeScript and TSX only, replace every leading run of 8 spaces with a tab.
- Tabs are only for leading indentation, never for spaces after non-space text. - Tabs are only for leading indentation, never for spaces after non-space text.
- TypeScript and TSX imports may stay on one line if they remain within the
line limit; do not expand short type-only imports mechanically.
- In TypeScript and TSX, when a function takes one destructured object
argument plus an inline type, prefer this shape when it fits locally:
```ts
const helper = (
{ value, flag }: { value: string
flag: boolean },
): Result => {
// ...
}
```
- In TypeScript and TSX, put `switch` case block braces on their own lines
when a case needs a lexical block:
```ts
case 'yes':
case 'no':
{
const expected = valueFor (item)
return expected == null || expected === answer
}
```
- In TypeScript and TSX, use `value == null` and `value != null` as the
default nullish checks. Do not use `=== null`, `=== undefined`,
`!== null`, or `!== undefined`.
- If code appears to need a distinction between `null` and `undefined`, treat
that as a design smell and revise the logic to avoid the distinction.
External library APIs that explicitly require distinguishing the two are the
only exception.
- In TypeScript and TSX, keep short arrays on one line when they fit under the
line limit; break arrays only when readability or line length requires it.
- In TypeScript and TSX, when a ternary expression is split across multiple
lines, align `?` and `:` with the condition expression. Do not indent `?` and
`:` one extra level under the condition.
```ts
const value =
condition
? consequent
: alternate
```
- In TypeScript and TSX, keep short ternary expressions on one line when they
fit cleanly under the line limit.
- In TypeScript and TSX, prefer ternary expressions for simple conditional
value selection. Do not replace a clear ternary with `if` statements, and do
not introduce immediately invoked functions just to avoid or reformat a
ternary expression.
- In TypeScript and TSX, do not write `let` followed by later `if` assignments
when the value can be expressed as a single `const` initializer. Prefer
`const` because it prevents accidental later reassignment.
- When fixing formatting, change formatting only. Do not change expression
structure, control flow, or variable mutability unless the requested style
explicitly requires it.
- Do not add production dependencies without explicit approval. - Do not add production dependencies without explicit approval.
- Do not create, modify, or run tests unless the user explicitly asks for - Do not create, modify, or run tests unless the user explicitly asks for
test work. When the user asks for tests, keep working and rerun them until test work. When the user asks for tests, keep working and rerun them until
+131 -7
ファイルの表示
@@ -14,11 +14,24 @@ class GekanatorGamesController < ApplicationController
question_count: answers.length, question_count: answers.length,
answers:) answers:)
if game.save if game.invalid?
render json: { id: game.id }, status: :created
else
render json: { errors: game.errors.full_messages }, status: :unprocessable_entity render json: { errors: game.errors.full_messages }, status: :unprocessable_entity
return
end end
learned_example_count = 0
ActiveRecord::Base.transaction do
game.save!
learned_example_count = learn_answers_from_game!(game)
end
render json: {
id: game.id,
learned_example_count:
}, status: :created
rescue ActiveRecord::RecordInvalid => e
render json: { errors: e.record.errors.full_messages }, status: :unprocessable_entity
end end
def extra_questions def extra_questions
@@ -32,10 +45,12 @@ class GekanatorGamesController < ApplicationController
.where(kind: 'post_similarity', source: 'user_suggested') .where(kind: 'post_similarity', source: 'user_suggested')
.to_a .to_a
selected = weighted_sample_questions( selected =
questions, prioritized_extra_questions(
post_id: game.correct_post_id, questions,
limit: 2) post_id: game.correct_post_id,
user: current_user,
limit: 2)
render json: { render json: {
questions: selected.map { |question| extra_question_json(question) } questions: selected.map { |question| extra_question_json(question) }
@@ -96,6 +111,23 @@ class GekanatorGamesController < ApplicationController
} }
end end
def prioritized_extra_questions questions, post_id:, user:, limit:
answered_question_ids =
GekanatorQuestionExample
.where(user:, gekanator_question_id: questions.map(&:id))
.distinct
.pluck(:gekanator_question_id)
unanswered, answered =
questions.partition { |question| !answered_question_ids.include?(question.id) }
selected = weighted_sample_questions(unanswered, post_id:, limit:)
return selected if selected.length >= limit
selected + weighted_sample_questions(
answered.reject { |question| selected.any? { _1.id == question.id } },
post_id:,
limit: limit - selected.length)
end
def weighted_sample_questions questions, post_id:, limit: def weighted_sample_questions questions, post_id:, limit:
remaining = questions.uniq(&:id) remaining = questions.uniq(&:id)
selected = [] selected = []
@@ -145,4 +177,96 @@ class GekanatorGamesController < ApplicationController
game game
end end
def learn_answers_from_game! game
correct_post = game.correct_post
return 0 if correct_post.blank?
accepted_questions =
GekanatorQuestion
.accepted
.index_by { |question| public_question_id_for(question) }
learned_count = 0
Array(game.answers).each do |answer|
answer_value = answer['answer'].to_s
next if answer_value.blank? || answer_value == 'unknown'
question_id = game_answer_question_id(answer)
next if question_id.blank?
question = accepted_questions[question_id.to_s]
next unless learnable_game_answer_question?(question)
example =
GekanatorQuestionExample.find_or_initialize_by(
gekanator_question: question,
post: correct_post,
user: current_user)
example.record_answer!(
answer: answer_value,
source: 'post_game_answer',
gekanator_game: game)
example.save!
learned_count += 1
end
learned_count
end
def public_question_id_for question
condition = normalize_condition(question.condition)
case condition[:type]
when 'tag'
"tag:#{condition[:key]}"
when 'source'
"source:#{condition[:host]}"
when 'original-year'
"original-year:#{condition[:year]}"
when 'original-month'
"original-month:#{condition[:month]}"
when 'original-month-day'
"original-month-day:#{condition[:monthDay] || condition[:month_day]}"
when 'title-length-at-least'
"title:length-at-least:#{condition[:length]}"
when 'title-length-greater-than'
"title:length-at-least:#{condition[:length].to_i + 1}"
when 'title-has-ascii'
'title:ascii'
when 'title-contains'
"title:contains:#{condition[:text]}"
when 'post-similarity'
"post-similarity:#{question.id}"
else
"catalog:#{question.id}"
end
end
def normalize_condition condition
json = condition.deep_dup.as_json
if json['type'] == 'original-month-day' && json['monthDay'].blank?
json['monthDay'] = json.delete('month_day')
end
json.deep_symbolize_keys
end
def learnable_game_answer_question? question
return false if question.nil?
return true if question.kind == 'post_similarity'
return false unless question.kind == 'tag'
condition = normalize_condition(question.condition)
key = condition[:key].to_s
!key.start_with?('nico:')
end
def game_answer_question_id answer
answer['question_id'] ||
answer[:question_id] ||
answer['questionId'] ||
answer[:questionId]
end
end end
+7 -1
ファイルの表示
@@ -2,7 +2,7 @@ class GekanatorPostsController < ApplicationController
def index def index
posts = posts =
Post Post
.preload(tags: :tag_name) .preload(:post_similarities, tags: :tag_name)
.with_attached_thumbnail .with_attached_thumbnail
.order(Arel.sql( .order(Arel.sql(
'COALESCE(posts.original_created_before - INTERVAL 1 MINUTE, ' \ 'COALESCE(posts.original_created_before - INTERVAL 1 MINUTE, ' \
@@ -22,6 +22,12 @@ class GekanatorPostsController < ApplicationController
thumbnail_base: post.thumbnail_base, thumbnail_base: post.thumbnail_base,
original_created_from: post.original_created_from, original_created_from: post.original_created_from,
original_created_before: post.original_created_before, original_created_before: post.original_created_before,
post_similarity_edges: post.post_similarities.map { |similarity|
{
target_post_id: similarity.target_post_id,
cos: similarity.cos.to_f
}
},
tags: post.tags.map { |tag| tag_json(tag) } tags: post.tags.map { |tag| tag_json(tag) }
} }
end end
+39
ファイルの表示
@@ -8,6 +8,35 @@ class GekanatorQuestionSuggestionsController < ApplicationController
return head :not_found return head :not_found
end end
existing_question_id = params[:existing_question_id].presence
if existing_question_id
question = GekanatorQuestion.accepted.find_by(id: existing_question_id)
return head :not_found unless question
unless learnable_existing_question?(question)
return render_validation_error fields: { existing_question_id: ['質問が不正です.'] }
end
example =
GekanatorQuestionExample.find_or_initialize_by(
gekanator_question: question,
post: game.correct_post,
user: current_user)
example.record_answer!(
answer: params.require(:answer),
source: 'post_game_extra',
gekanator_game: game)
if example.save
render json: {
id: question.id,
count: game.question_suggestions.count
}, status: :created
else
render_validation_error example
end
return
end
suggestion = GekanatorQuestionSuggestion.new( suggestion = GekanatorQuestionSuggestion.new(
gekanator_game: game, gekanator_game: game,
user: current_user, user: current_user,
@@ -53,4 +82,14 @@ class GekanatorQuestionSuggestionsController < ApplicationController
rescue NotImplementedError rescue NotImplementedError
head :not_implemented head :not_implemented
end end
private
def learnable_existing_question? question
return true if question.kind == 'post_similarity'
return false unless question.kind == 'tag'
key = question.condition.as_json['key'].to_s
!key.start_with?('nico:')
end
end end
+2 -1
ファイルの表示
@@ -16,6 +16,7 @@ class GekanatorQuestionsController < ApplicationController
def question_json question def question_json question
condition = condition_json(question.condition).deep_symbolize_keys condition = condition_json(question.condition).deep_symbolize_keys
json = { json = {
record_id: question.id,
id: question_id_for(question, condition), id: question_id_for(question, condition),
text: question_text_for(question, condition), text: question_text_for(question, condition),
kind: question.kind, kind: question.kind,
@@ -23,7 +24,7 @@ class GekanatorQuestionsController < ApplicationController
source: question.source, source: question.source,
priority_weight: question.priority_weight priority_weight: question.priority_weight
} }
if question.kind == 'post_similarity' if question.kind == 'post_similarity' || question.kind == 'tag'
json[:example_answers] = example_answers_json(question) json[:example_answers] = example_answers_json(question)
end end
json json
+2 -2
ファイルの表示
@@ -1,7 +1,7 @@
class GekanatorQuestionExample < ApplicationRecord class GekanatorQuestionExample < ApplicationRecord
ANSWERS = GekanatorQuestionSuggestion::ANSWERS ANSWERS = GekanatorQuestionSuggestion::ANSWERS
NON_UNKNOWN_ANSWERS = ANSWERS - ['unknown'] NON_UNKNOWN_ANSWERS = ANSWERS - ['unknown']
SOURCES = ['initial_suggestion', 'post_game_extra'].freeze SOURCES = ['initial_suggestion', 'post_game_answer', 'post_game_extra'].freeze
belongs_to :gekanator_question belongs_to :gekanator_question
belongs_to :post belongs_to :post
@@ -35,7 +35,7 @@ class GekanatorQuestionExample < ApplicationRecord
self.answer_counts = counts self.answer_counts = counts
self.sample_count = counts.values.sum self.sample_count = counts.values.sum
self.gekanator_game = gekanator_game if gekanator_game.present? self.gekanator_game = gekanator_game if gekanator_game.present?
self.source = source if new_record? self.source = source
apply_aggregated_answer!(preferred_answer: answer) apply_aggregated_answer!(preferred_answer: answer)
self self
-13
ファイルの表示
@@ -1,5 +1,4 @@
class GekanatorQuestionSuggestion < ApplicationRecord class GekanatorQuestionSuggestion < ApplicationRecord
MAX_QUESTIONS_PER_GAME = 3
ANSWERS = ['yes', 'no', 'partial', 'probably_no', 'unknown'].freeze ANSWERS = ['yes', 'no', 'partial', 'probably_no', 'unknown'].freeze
belongs_to :gekanator_game belongs_to :gekanator_game
@@ -10,16 +9,4 @@ class GekanatorQuestionSuggestion < ApplicationRecord
validates :question_text, presence: true, length: { maximum: 1000 } validates :question_text, presence: true, length: { maximum: 1000 }
validates :answer, presence: true, inclusion: { in: ANSWERS } validates :answer, presence: true, inclusion: { in: ANSWERS }
validates :processed, inclusion: { in: [true, false] } validates :processed, inclusion: { in: [true, false] }
validate :question_suggestion_limit_per_game, on: :create
private
def question_suggestion_limit_per_game
return if gekanator_game_id.blank?
count = GekanatorQuestionSuggestion.where(gekanator_game_id:).count
if count >= MAX_QUESTIONS_PER_GAME
errors.add(:base, '質問追加数を超えてゐます.')
end
end
end end
+152 -3
ファイルの表示
@@ -151,6 +151,154 @@ RSpec.describe 'Gekanator learning API', type: :request do
expect(response).to have_http_status(:unauthorized) expect(response).to have_http_status(:unauthorized)
end end
it 'learns accepted non-nico tag answers from camelCase main game logs' do
sign_in_as admin
tag_question = GekanatorQuestion.create!(
text: 'MAD 要素がある?',
kind: 'tag',
source: 'admin_curated',
status: 'accepted',
priority_weight: 1.0,
condition: { type: 'tag', key: 'meme:MAD' },
created_by: admin
)
expect {
post '/gekanator/games', params: {
guessed_post_id: guessed_post.id,
correct_post_id: correct_post.id,
answers: [
{
questionId: 'tag:meme:MAD',
question_text: 'MAD 要素がある?',
answer: 'yes',
original_answer: 'yes'
},
{
questionId: 'tag:meme:missing',
question_text: '存在しない質問?',
answer: 'yes',
original_answer: 'yes'
},
{
questionId: 'tag:meme:MAD',
question_text: 'MAD 要素がある?',
answer: 'unknown',
original_answer: 'unknown'
}
]
}
}.to change { GekanatorQuestionExample.count }.by(1)
expect(response).to have_http_status(:created)
expect(json['learned_example_count']).to eq(1)
example = GekanatorQuestionExample.last
expect(example).to have_attributes(
gekanator_question_id: tag_question.id,
post_id: correct_post.id,
user_id: admin.id,
answer: 'yes',
source: 'post_game_answer'
)
expect(example.gekanator_game_id).to eq(json['id'])
end
it 'does not learn fact questions or nico tag questions from main game logs' do
sign_in_as admin
[
{
text: 'example.com 由来?',
kind: 'source',
condition: { type: 'source', host: 'example.com' }
},
{
text: '題名に結束バンドを含む?',
kind: 'title',
condition: { type: 'title-contains', text: '結束バンド' }
},
{
text: '2024 年投稿?',
kind: 'original_date',
condition: { type: 'original-year', year: 2024 }
},
{
text: 'ニコニコにぼっちタグ?',
kind: 'tag',
condition: { type: 'tag', key: 'nico:ぼっち' }
}
].each do |attributes|
GekanatorQuestion.create!(
text: attributes[:text],
kind: attributes[:kind],
source: 'admin_curated',
status: 'accepted',
priority_weight: 1.0,
condition: attributes[:condition],
created_by: admin
)
end
expect {
post '/gekanator/games', params: {
guessed_post_id: guessed_post.id,
correct_post_id: correct_post.id,
answers: [
{ question_id: 'source:example.com', answer: 'yes' },
{ question_id: 'title:contains:結束バンド', answer: 'yes' },
{ question_id: 'original-year:2024', answer: 'yes' },
{ question_id: 'tag:nico:ぼっち', answer: 'yes' }
]
}
}.not_to change { GekanatorQuestionExample.count }
expect(response).to have_http_status(:created)
expect(json['learned_example_count']).to eq(0)
end
it 'updates an existing main game example instead of duplicating it' do
sign_in_as admin
tag_question = GekanatorQuestion.create!(
text: '喜多ちゃんが関係してる?',
kind: 'tag',
source: 'admin_curated',
status: 'accepted',
priority_weight: 1.0,
condition: { type: 'tag', key: 'character:喜多郁代' },
created_by: admin
)
existing = GekanatorQuestionExample.create!(
gekanator_question: tag_question,
post: correct_post,
user: admin,
answer: 'no',
source: 'post_game_answer',
weight: 1.0
)
expect {
post '/gekanator/games', params: {
guessed_post_id: guessed_post.id,
correct_post_id: correct_post.id,
answers: [
{
question_id: 'tag:character:喜多郁代',
answer: 'yes',
original_answer: 'yes'
}
]
}
}.not_to change { GekanatorQuestionExample.count }
expect(response).to have_http_status(:created)
expect(json['learned_example_count']).to eq(1)
expect(existing.reload.answer).to eq('yes')
expect(existing.sample_count).to eq(2)
end
end end
describe 'POST /gekanator/question_suggestions' do describe 'POST /gekanator/question_suggestions' do
@@ -249,7 +397,7 @@ RSpec.describe 'Gekanator learning API', type: :request do
expect(GekanatorQuestionSuggestion.last.processed).to eq(false) expect(GekanatorQuestionSuggestion.last.processed).to eq(false)
end end
it 'limits suggestions to three per game' do it 'allows more than three suggestions per game' do
sign_in_as admin sign_in_as admin
3.times do |i| 3.times do |i|
@@ -267,9 +415,10 @@ RSpec.describe 'Gekanator learning API', type: :request do
question_text: 'fourth question?', question_text: 'fourth question?',
answer: 'yes' answer: 'yes'
} }
}.not_to change { GekanatorQuestionSuggestion.count } }.to change { GekanatorQuestionSuggestion.count }.by(1)
expect(response).to have_http_status(:unprocessable_entity) expect(response).to have_http_status(:created)
expect(json['count']).to eq(4)
end end
it 'allows a non-admin user to suggest a question for their own game' do it 'allows a non-admin user to suggest a question for their own game' do
バイナリファイルは表示されません.

変更前

幅:  |  高さ:  |  サイズ: 559 KiB

バイナリファイルは表示されません.

変更前

幅:  |  高さ:  |  サイズ: 146 KiB

バイナリファイルは表示されません.

変更前

幅:  |  高さ:  |  サイズ: 1.2 MiB

バイナリファイルは表示されません.

変更前

幅:  |  高さ:  |  サイズ: 188 KiB

バイナリファイルは表示されません.

変更前

幅:  |  高さ:  |  サイズ: 201 KiB

バイナリファイルは表示されません.

変更前

幅:  |  高さ:  |  サイズ: 196 KiB

バイナリファイルは表示されません.

変更前

幅:  |  高さ:  |  サイズ: 179 KiB

+10 -3
ファイルの表示
@@ -62,6 +62,7 @@ export type GekanatorExtraQuestion = {
priorityWeight: number } priorityWeight: number }
export type StoredGekanatorQuestion = { export type StoredGekanatorQuestion = {
recordId?: number
id: string id: string
text: string text: string
kind: GekanatorQuestionKind kind: GekanatorQuestionKind
@@ -71,6 +72,7 @@ export type StoredGekanatorQuestion = {
exampleAnswers?: Record<`${ number }`, GekanatorAnswerValue> } exampleAnswers?: Record<`${ number }`, GekanatorAnswerValue> }
export type GekanatorQuestion = { export type GekanatorQuestion = {
recordId?: number
id: string id: string
text: string text: string
kind: GekanatorQuestionKind kind: GekanatorQuestionKind
@@ -148,7 +150,7 @@ const directExampleAnswerFor = (
question: StoredGekanatorQuestion, question: StoredGekanatorQuestion,
post: Post, post: Post,
): GekanatorAnswerValue | null => { ): GekanatorAnswerValue | null => {
if (question.kind !== 'post_similarity') if (question.kind !== 'post_similarity' && question.kind !== 'tag')
return null return null
const direct = question.exampleAnswers?.[String (post.id) as `${ number }`] const direct = question.exampleAnswers?.[String (post.id) as `${ number }`]
@@ -348,6 +350,7 @@ export const restoreGekanatorQuestion = (
const normalizedCondition = normalizeTitleLengthCondition (question.condition) const normalizedCondition = normalizeTitleLengthCondition (question.condition)
const normalizedQuestion = { const normalizedQuestion = {
...question, ...question,
recordId: question.recordId,
id: normalizedCondition.type === 'title-length-at-least' id: normalizedCondition.type === 'title-length-at-least'
? `title:length-at-least:${ normalizedCondition.length }` ? `title:length-at-least:${ normalizedCondition.length }`
: question.id, : question.id,
@@ -367,6 +370,7 @@ export const storeGekanatorQuestion = (
id: question.condition.type === 'title-length-greater-than' id: question.condition.type === 'title-length-greater-than'
? `title:length-at-least:${ question.condition.length + 1 }` ? `title:length-at-least:${ question.condition.length + 1 }`
: question.id, : question.id,
recordId: question.recordId,
text: question.text, text: question.text,
kind: question.kind, kind: question.kind,
condition: normalizeTitleLengthCondition (question.condition), condition: normalizeTitleLengthCondition (question.condition),
@@ -581,7 +585,7 @@ export const saveGekanatorGame = async ({
guessedPostId: number guessedPostId: number
correctPostId: number correctPostId: number
answers: GekanatorAnswerLog[] answers: GekanatorAnswerLog[]
}): Promise<{ id: number }> => }): Promise<{ id: number; learnedExampleCount: number }> =>
await apiPost ('/gekanator/games', { await apiPost ('/gekanator/games', {
guessed_post_id: guessedPostId, guessed_post_id: guessedPostId,
correct_post_id: correctPostId, correct_post_id: correctPostId,
@@ -595,15 +599,18 @@ export const saveGekanatorGame = async ({
export const saveGekanatorQuestionSuggestion = async ({ export const saveGekanatorQuestionSuggestion = async ({
gekanatorGameId, gekanatorGameId,
existingQuestionId,
questionText, questionText,
answer, answer,
}: { }: {
gekanatorGameId: number gekanatorGameId: number
questionText: string existingQuestionId?: number
questionText?: string
answer: GekanatorAnswerValue answer: GekanatorAnswerValue
}): Promise<{ id: number; count: number }> => }): Promise<{ id: number; count: number }> =>
await apiPost ('/gekanator/question_suggestions', { await apiPost ('/gekanator/question_suggestions', {
gekanator_game_id: gekanatorGameId, gekanator_game_id: gekanatorGameId,
existing_question_id: existingQuestionId,
question_text: questionText, question_text: questionText,
answer }) answer })
+75 -3
ファイルの表示
@@ -51,6 +51,21 @@ const postSimilarityQuestion = (
}) })
const sourceQuestion = (
host: string,
): GekanatorQuestion => ({
id: `source:${ host }`,
text: `${ host }?`,
kind: 'source',
condition: {
type: 'source',
host },
source: 'default',
priorityWeight: 1,
test: candidate => new URL (candidate.url).hostname === host,
})
const answer = ( const answer = (
question: GekanatorQuestion, question: GekanatorQuestion,
value: GekanatorAnswerValue, value: GekanatorAnswerValue,
@@ -64,7 +79,7 @@ const answer = (
describe('candidatePostsFor', () => { describe('candidatePostsFor', () => {
it('lets recovered candidates ignore old answers but not later answers', () => { it('does not hard-filter semantic post_similarity answers', () => {
const posts = [post (1), post (2), post (3)] const posts = [post (1), post (2), post (3)]
const oldQuestion = postSimilarityQuestion ('old', { const oldQuestion = postSimilarityQuestion ('old', {
1: 'no', 1: 'no',
@@ -77,6 +92,29 @@ describe('candidatePostsFor', () => {
3: 'yes', 3: 'yes',
}) })
const candidates = candidatePostsFor ({
posts,
questions: [oldQuestion, laterQuestion],
answers: [answer (oldQuestion, 'yes'), answer (laterQuestion, 'yes')],
softenedQuestionIds: new Set (),
rejectedPostIds: new Set (),
recoveredCandidatePosts: new Map ([
[1, 1],
[3, 1],
]) })
expect(candidates.map (candidate => candidate.id)).toEqual ([1, 2, 3])
})
it('lets recovered candidates ignore old fact answers but not later fact answers', () => {
const posts = [
{ ...post (1), url: 'https://other.example/posts/1' },
post (2),
{ ...post (3), url: 'https://example.com/posts/3' },
]
const oldQuestion = sourceQuestion ('old.example.com')
const laterQuestion = sourceQuestion ('example.com')
const candidates = candidatePostsFor ({ const candidates = candidatePostsFor ({
posts, posts,
questions: [oldQuestion, laterQuestion], questions: [oldQuestion, laterQuestion],
@@ -112,7 +150,7 @@ describe('candidatePostsFor', () => {
describe('hardFilteredPostsForAnswer', () => { describe('hardFilteredPostsForAnswer', () => {
it('returns zero candidates without falling back to the original pool', () => { it('keeps the original pool for semantic post_similarity answers', () => {
const posts = [post (1), post (2)] const posts = [post (1), post (2)]
const question = postSimilarityQuestion ('question', { const question = postSimilarityQuestion ('question', {
1: 'yes', 1: 'yes',
@@ -123,7 +161,41 @@ describe('hardFilteredPostsForAnswer', () => {
posts, posts,
question, question,
answer: 'no', answer: 'no',
})).toEqual ([]) })).toEqual (posts)
})
it('hard-filters fact answers only for yes and no', () => {
const posts = [
{ ...post (1), url: 'https://example.com/posts/1' },
{ ...post (2), url: 'https://other.example/posts/2' },
]
const question = sourceQuestion ('example.com')
expect(hardFilteredPostsForAnswer ({
posts,
question,
answer: 'yes',
}).map (candidate => candidate.id)).toEqual ([1])
expect(hardFilteredPostsForAnswer ({
posts,
question,
answer: 'no',
}).map (candidate => candidate.id)).toEqual ([2])
expect(hardFilteredPostsForAnswer ({
posts,
question,
answer: 'partial',
})).toEqual (posts)
expect(hardFilteredPostsForAnswer ({
posts,
question,
answer: 'probably_no',
})).toEqual (posts)
expect(hardFilteredPostsForAnswer ({
posts,
question,
answer: 'unknown',
})).toEqual (posts)
}) })
}) })
+70 -79
ファイルの表示
@@ -1,33 +1,33 @@
import { expectedAnswerForQuestion } from '@/lib/gekanator' import { expectedAnswerForQuestion } from '@/lib/gekanator'
import type { import type { GekanatorAnswerLog, GekanatorAnswerValue, GekanatorQuestion } from '@/lib/gekanator'
GekanatorAnswerLog,
GekanatorAnswerValue,
GekanatorQuestion,
} from '@/lib/gekanator'
import type { Post } from '@/types' import type { Post } from '@/types'
export type RecoveredCandidatePost = { export type RecoveredCandidatePost = {
postId: number postId: number
answerCountAtRecovery: number } answerCountAtRecovery: number }
export const candidatePostsFor = ({ const questionIsFactLikeForHardFiltering = (question: GekanatorQuestion): boolean =>
posts, !(question.kind === 'post_similarity'
questions, || (question.kind === 'tag'
answers, && question.condition.type === 'tag'
softenedQuestionIds, && !(question.condition.key.startsWith ('nico:'))))
rejectedPostIds,
recoveredCandidatePosts,
}: { export const candidatePostsFor = (
posts: Post[] { posts,
questions: GekanatorQuestion[] questions,
answers: GekanatorAnswerLog[] answers,
softenedQuestionIds: Set<string> softenedQuestionIds,
rejectedPostIds: Set<number> rejectedPostIds,
recoveredCandidatePosts: Map<number, number> recoveredCandidatePosts }: { posts: Post[]
}): Post[] => { questions: GekanatorQuestion[]
answers: GekanatorAnswerLog[]
softenedQuestionIds: Set<string>
rejectedPostIds: Set<number>
recoveredCandidatePosts: Map<number, number> },
): Post[] => {
const questionById = new Map (questions.map (question => [question.id, question])) const questionById = new Map (questions.map (question => [question.id, question]))
return posts.filter (post => { return posts.filter (post => {
@@ -37,7 +37,7 @@ export const candidatePostsFor = ({
const answerCountAtRecovery = recoveredCandidatePosts.get (post.id) const answerCountAtRecovery = recoveredCandidatePosts.get (post.id)
return answers.every ((answer, index) => { return answers.every ((answer, index) => {
if (answerCountAtRecovery !== undefined && index < answerCountAtRecovery) if (answerCountAtRecovery != null && index < answerCountAtRecovery)
return true return true
if (softenedQuestionIds.has (answer.questionId)) if (softenedQuestionIds.has (answer.questionId))
@@ -46,14 +46,17 @@ export const candidatePostsFor = ({
const question = questionById.get (answer.questionId) const question = questionById.get (answer.questionId)
if (!(question)) if (!(question))
return true return true
if (!(questionIsFactLikeForHardFiltering (question)))
return true
switch (answer.answer) switch (answer.answer)
{ {
case 'yes': case 'yes':
case 'no': { case 'no':
const expected = expectedAnswerForQuestion (question, post) {
return expected === null || expected === 'unknown' || expected === answer.answer const expected = expectedAnswerForQuestion (question, post)
} return expected === null || expected === 'unknown' || expected === answer.answer
}
default: default:
return true return true
} }
@@ -62,30 +65,25 @@ export const candidatePostsFor = ({
} }
export const hardFilteredPostsForAnswer = ({ export const hardFilteredPostsForAnswer = (
posts, { posts, question, answer }: { posts: Post[]
question, question: GekanatorQuestion
answer, answer: GekanatorAnswerValue },
}: { ): Post[] => {
posts: Post[] if (!(questionIsFactLikeForHardFiltering (question)))
question: GekanatorQuestion return posts
answer: GekanatorAnswerValue
}): Post[] => { if (!(answer === 'yes' || answer === 'no'))
if (answer === 'unknown')
return posts return posts
return posts.filter (post => { return posts.filter (post => {
const expected = expectedAnswerForQuestion (question, post) const expected = expectedAnswerForQuestion (question, post)
return expected === null || expected === 'unknown' || expected === answer return expected == null || expected === 'unknown' || expected === answer
}) })
} }
const concreteAnswerOptions: GekanatorAnswerValue[] = [ const concreteAnswerOptions: GekanatorAnswerValue[] = ['yes', 'no', 'partial', 'probably_no']
'yes',
'no',
'partial',
'probably_no']
export const allConcreteAnswerOptionsExhausted = ( export const allConcreteAnswerOptionsExhausted = (
@@ -104,45 +102,39 @@ const nextRecoveryTargetSize = (recoveryStepCount: number): number =>
6 * (2 ** recoveryStepCount) 6 * (2 ** recoveryStepCount)
export const recoverCandidatePosts = ({ export const recoverCandidatePosts = (
posts, { posts,
scores, scores,
rejectedPostIds, rejectedPostIds,
recoveredCandidatePosts, recoveredCandidatePosts,
eligiblePostIds, eligiblePostIds,
answerCountAtRecovery, answerCountAtRecovery,
recoveryStepCount, recoveryStepCount }: { posts: Post[]
}: { scores: Map<number, number>
posts: Post[] rejectedPostIds: Set<number>
scores: Map<number, number> recoveredCandidatePosts: Map<number, number>
rejectedPostIds: Set<number> eligiblePostIds: Set<number>
recoveredCandidatePosts: Map<number, number> answerCountAtRecovery: number
eligiblePostIds: Set<number> recoveryStepCount: number },
answerCountAtRecovery: number ): { recoveredCandidatePosts: Map<number, number>
recoveryStepCount: number recoveryStepCount: number } | null => {
}): {
recoveredCandidatePosts: Map<number, number>
recoveryStepCount: number
} | null => {
const recovered = new Map (recoveredCandidatePosts) const recovered = new Map (recoveredCandidatePosts)
const targetSize = nextRecoveryTargetSize (recoveryStepCount) const targetSize = nextRecoveryTargetSize (recoveryStepCount)
const countedPostIds = new Set ([ const countedPostIds = new Set ([...eligiblePostIds, ...recovered.keys ()])
...eligiblePostIds,
...recovered.keys ()])
const addCount = targetSize - countedPostIds.size const addCount = targetSize - countedPostIds.size
if (addCount <= 0) if (addCount <= 0)
return { {
recoveredCandidatePosts: recovered, return { recoveredCandidatePosts: recovered,
recoveryStepCount: recoveryStepCount + 1 } recoveryStepCount: recoveryStepCount + 1 }
}
const candidates = posts const candidates =
.filter (post => posts
!(rejectedPostIds.has (post.id)) .filter (post => (!(rejectedPostIds.has (post.id))
&& !(eligiblePostIds.has (post.id)) && !(eligiblePostIds.has (post.id))
&& !(recovered.has (post.id))) && !(recovered.has (post.id))))
.sort ((a, b) => .sort ((a, b) => ((scores.get (b.id) ?? Number.NEGATIVE_INFINITY)
(scores.get (b.id) ?? Number.NEGATIVE_INFINITY) - (scores.get (a.id) ?? Number.NEGATIVE_INFINITY)))
- (scores.get (a.id) ?? Number.NEGATIVE_INFINITY))
.slice (0, addCount) .slice (0, addCount)
if (candidates.length === 0) if (candidates.length === 0)
@@ -150,7 +142,6 @@ export const recoverCandidatePosts = ({
candidates.forEach (post => recovered.set (post.id, answerCountAtRecovery)) candidates.forEach (post => recovered.set (post.id, answerCountAtRecovery))
return { return { recoveredCandidatePosts: recovered,
recoveredCandidatePosts: recovered, recoveryStepCount: recoveryStepCount + 1 }
recoveryStepCount: recoveryStepCount + 1 }
} }
+54
ファイルの表示
@@ -1,3 +1,6 @@
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
import { isQuestionHardFilteredAfterAnswers } from '@/lib/gekanatorQuestionFilters' import { isQuestionHardFilteredAfterAnswers } from '@/lib/gekanatorQuestionFilters'
@@ -49,6 +52,57 @@ const blocked = (
isQuestionHardFilteredAfterAnswers (question (candidate), [answer (previous, value)]) isQuestionHardFilteredAfterAnswers (question (candidate), [answer (previous, value)])
const gekanatorPageSource = readFileSync (
resolve (process.cwd (), 'src/pages/GekanatorPage.tsx'),
'utf8')
const gekanatorBackdropSource = gekanatorPageSource.slice (
gekanatorPageSource.indexOf ('const GekanatorBackdrop'),
gekanatorPageSource.indexOf ('const expectedAnswerFor'))
describe('GekanatorBackdrop regression structure', () => {
it('keeps displayedBackdropMode as the render-time source of truth', () => {
expect(gekanatorBackdropSource).not.toContain ('isLeavingGuessBackdrop')
expect(gekanatorBackdropSource).not.toContain ('renderBackdropMode')
expect(gekanatorBackdropSource).not.toContain ('renderWinningRunCount')
expect(gekanatorBackdropSource).not.toContain ('renderThumbnails')
expect(gekanatorBackdropSource).not.toContain ('renderIsCrossfading')
expect(gekanatorBackdropSource).toContain (
"const renderedSettings = settingsForMode (displayedBackdropMode)")
expect(gekanatorBackdropSource).toContain (
'scaleForMode (displayedBackdropMode, displayedWinningRunCount)')
expect(gekanatorBackdropSource).toContain (
"backdropMode === 'guess' || displayedBackdropMode === 'guess'")
})
it('does not split guess into a separate renderer or force a remount', () => {
expect(gekanatorBackdropSource).not.toContain ('renderStaticGuessBackdrop')
expect(gekanatorBackdropSource).not.toContain ('guessZoomAnimationKey')
expect(gekanatorBackdropSource).not.toContain ('shouldAnimateGuessZoomIn')
expect(gekanatorBackdropSource).not.toContain ('previousBackdropModeRef')
expect(gekanatorBackdropSource).not.toContain (
'if (isGuessPresentation && guessThumbnail)')
})
it('keeps tile keys independent from backdrop mode', () => {
expect(gekanatorBackdropSource).toContain ('key={duplicate}')
expect(gekanatorBackdropSource).toContain ('key={`${ duplicate }:${ index }`}')
expect(gekanatorBackdropSource).not.toMatch (/key=\{`\$\{\s*mode/)
expect(gekanatorBackdropSource).not.toMatch (/key=\{`\$\{\s*displayedBackdropMode/)
})
it('keeps guess on the shared scale, x, and y animation path', () => {
expect(gekanatorBackdropSource).toContain ('animate={{ scale: renderedScale')
expect(gekanatorBackdropSource).toContain (
"x: displayedBackdropMode === 'guess' ? guessFocusOffset.x : '0%'")
expect(gekanatorBackdropSource).toContain (
"y: displayedBackdropMode === 'guess' ? guessFocusOffset.y : '0%'")
})
})
describe('isQuestionHardFilteredAfterAnswers', () => { describe('isQuestionHardFilteredAfterAnswers', () => {
it('blocks only contradictory or redundant month questions after a yes answer', () => { it('blocks only contradictory or redundant month questions after a yes answer', () => {
const previous: GekanatorQuestionCondition = { type: 'original-month', month: 12 } const previous: GekanatorQuestionCondition = { type: 'original-month', month: 12 }
ファイル差分が大きすぎるため省略します 差分を読込み
+4
ファイルの表示
@@ -139,6 +139,10 @@ export type Post = {
title: string | null title: string | null
thumbnail: string | null thumbnail: string | null
thumbnailBase: string | null thumbnailBase: string | null
postSimilarityEdges?: {
targetPostId: number
cos: number
}[]
tags: Tag[] tags: Tag[]
parentPosts?: Post[] parentPosts?: Post[]
childPosts?: Post[] childPosts?: Post[]