-
Notifications
You must be signed in to change notification settings - Fork 284
[RSM] Smart bookmarks API layer #5283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sztomek
wants to merge
4
commits into
feat/smart-bookmarks-model
Choose a base branch
from
feat/smart-bookmarks-api
base: feat/smart-bookmarks-model
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+311
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
91 changes: 91 additions & 0 deletions
91
.../java/au/com/shiftyjelly/pocketcasts/repositories/transcript/TranscriptWindowExtractor.kt
This file contains hidden or 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,91 @@ | ||
| package au.com.shiftyjelly.pocketcasts.repositories.transcript | ||
|
|
||
| import au.com.shiftyjelly.pocketcasts.models.db.dao.TranscriptDao | ||
| import au.com.shiftyjelly.pocketcasts.servers.podcast.TranscriptService | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
| import kotlin.coroutines.cancellation.CancellationException | ||
| import kotlin.time.Duration.Companion.minutes | ||
| import kotlinx.coroutines.flow.filter | ||
| import kotlinx.coroutines.flow.firstOrNull | ||
| import kotlinx.coroutines.withTimeoutOrNull | ||
| import okhttp3.CacheControl | ||
| import timber.log.Timber | ||
|
|
||
| @Singleton | ||
| class TranscriptWindowExtractor @Inject constructor( | ||
| private val transcriptDao: TranscriptDao, | ||
| private val transcriptService: TranscriptService, | ||
| ) { | ||
| suspend fun extractWindow(episodeUuid: String, timeSecs: Int, windowSecs: Int = 30): String? { | ||
| return try { | ||
| val transcripts = withTimeoutOrNull(1.minutes) { | ||
| transcriptDao.observeTranscripts(episodeUuid) | ||
| .filter { it.isNotEmpty() } | ||
| .firstOrNull() | ||
| } | ||
| val generated = transcripts?.firstOrNull { it.isGenerated } ?: return null | ||
|
|
||
| val body = runCatching { transcriptService.getTranscriptOrThrow(generated.url, CacheControl.FORCE_CACHE) } | ||
| .getOrNull() ?: return null | ||
|
|
||
| val vttContent = body.use { it.string() } | ||
| parseVttWindow(vttContent, timeSecs, windowSecs) | ||
| } catch (e: CancellationException) { | ||
| throw e | ||
| } catch (e: Exception) { | ||
| Timber.e(e, "Failed to extract transcript window for episode $episodeUuid") | ||
| null | ||
|
sztomek marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private val TIMESTAMP_REGEX = | ||
| """(?:(\d{2}):)?(\d{2}):(\d{2})\.\d{3}\s*-->\s*(?:(\d{2}):)?(\d{2}):(\d{2})\.\d{3}""".toRegex() | ||
| private val HTML_TAG_REGEX = """<[^>]+>""".toRegex() | ||
|
|
||
|
sztomek marked this conversation as resolved.
|
||
| internal fun parseVttWindow(content: String, timeSecs: Int, windowSecs: Int): String? { | ||
| val windowStart = (timeSecs - windowSecs).coerceAtLeast(0) | ||
| val windowEnd = timeSecs + windowSecs | ||
| val lines = content.lines() | ||
| val texts = mutableListOf<String>() | ||
| var i = 0 | ||
|
|
||
| while (i < lines.size) { | ||
| val match = TIMESTAMP_REGEX.find(lines[i]) | ||
| if (match != null) { | ||
| val sh = match.groupValues[1].takeIf { it.isNotEmpty() }?.toInt() ?: 0 | ||
| val sm = match.groupValues[2].toInt() | ||
|
sztomek marked this conversation as resolved.
|
||
| val ss = match.groupValues[3].toInt() | ||
| val eh = match.groupValues[4].takeIf { it.isNotEmpty() }?.toInt() ?: 0 | ||
| val em = match.groupValues[5].toInt() | ||
| val es = match.groupValues[6].toInt() | ||
| val start = sh * 3600 + sm * 60 + ss | ||
| val end = eh * 3600 + em * 60 + es | ||
|
|
||
| if (start < windowEnd && end > windowStart) { | ||
| i++ | ||
| val cueLines = mutableListOf<String>() | ||
| while (i < lines.size && lines[i].isNotBlank()) { | ||
| cueLines.add(lines[i].replace(HTML_TAG_REGEX, "").trim()) | ||
| i++ | ||
|
sztomek marked this conversation as resolved.
|
||
| } | ||
| val text = cueLines.joinToString(" ").trim() | ||
| if (text.isNotEmpty()) { | ||
| texts.add(text) | ||
| } | ||
| } else { | ||
| i++ | ||
| while (i < lines.size && lines[i].isNotBlank()) { | ||
| i++ | ||
| } | ||
| } | ||
| } | ||
| i++ | ||
| } | ||
|
|
||
| val result = texts.joinToString(" ") | ||
| return result.takeIf { it.split("\\s+".toRegex()).size >= 10 } | ||
| } | ||
| } | ||
| } | ||
124 changes: 124 additions & 0 deletions
124
...a/au/com/shiftyjelly/pocketcasts/repositories/transcript/TranscriptWindowExtractorTest.kt
This file contains hidden or 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,124 @@ | ||
| package au.com.shiftyjelly.pocketcasts.repositories.transcript | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNull | ||
| import org.junit.Test | ||
|
|
||
| class TranscriptWindowExtractorTest { | ||
|
|
||
| private val sampleVtt = """ | ||
| |WEBVTT | ||
| | | ||
| |00:00:00.000 --> 00:00:05.000 | ||
| |Welcome to the show everyone. | ||
| | | ||
| |00:00:05.000 --> 00:00:10.000 | ||
| |Today we are going to discuss artificial intelligence. | ||
| | | ||
| |00:00:10.000 --> 00:00:20.000 | ||
| |Let me start by defining what AI actually means in practice. | ||
| | | ||
| |00:00:20.000 --> 00:00:30.000 | ||
| |AI is a broad field that includes machine learning, deep learning, and more. | ||
| | | ||
| |00:00:30.000 --> 00:00:40.000 | ||
| |The recent advances have been truly remarkable for the industry. | ||
| | | ||
| |00:00:40.000 --> 00:00:50.000 | ||
| |Companies are investing billions of dollars into AI research. | ||
| | | ||
| |00:01:00.000 --> 00:01:10.000 | ||
| |This is a much later segment about totally different things. | ||
| """.trimMargin() | ||
|
sztomek marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| fun `extract window around middle of transcript`() { | ||
| val result = TranscriptWindowExtractor.parseVttWindow(sampleVtt, timeSecs = 25, windowSecs = 15) | ||
|
|
||
| assertEquals( | ||
| "Let me start by defining what AI actually means in practice. " + | ||
| "AI is a broad field that includes machine learning, deep learning, and more. " + | ||
| "The recent advances have been truly remarkable for the industry.", | ||
| result, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `extract window at start of transcript`() { | ||
| val result = TranscriptWindowExtractor.parseVttWindow(sampleVtt, timeSecs = 0, windowSecs = 10) | ||
|
|
||
| assertEquals( | ||
| "Welcome to the show everyone. Today we are going to discuss artificial intelligence.", | ||
| result, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `return null when window has too few words`() { | ||
| val shortVtt = """ | ||
| |WEBVTT | ||
| | | ||
| |00:00:00.000 --> 00:00:05.000 | ||
| |Just a few words. | ||
| """.trimMargin() | ||
|
|
||
| val result = TranscriptWindowExtractor.parseVttWindow(shortVtt, timeSecs = 2, windowSecs = 30) | ||
|
|
||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `return null when no cues in window`() { | ||
| val result = TranscriptWindowExtractor.parseVttWindow(sampleVtt, timeSecs = 300, windowSecs = 10) | ||
|
|
||
| assertNull(result) | ||
| } | ||
|
|
||
| @Test | ||
| fun `extract window from mm-ss-mmm timestamps`() { | ||
| val vtt = """ | ||
| |WEBVTT | ||
| | | ||
| |00:00.000 --> 00:05.000 | ||
| |Welcome to the show everyone. | ||
| | | ||
| |00:05.000 --> 00:10.000 | ||
| |Today we are going to discuss artificial intelligence. | ||
| | | ||
| |00:10.000 --> 00:20.000 | ||
| |Let me start by defining what AI actually means in practice. | ||
| | | ||
| |00:20.000 --> 00:30.000 | ||
| |AI is a broad field that includes machine learning, deep learning, and more. | ||
| | | ||
| |00:30.000 --> 00:40.000 | ||
| |The recent advances have been truly remarkable for the industry. | ||
| """.trimMargin() | ||
|
|
||
| val result = TranscriptWindowExtractor.parseVttWindow(vtt, timeSecs = 15, windowSecs = 10) | ||
|
|
||
| assertEquals( | ||
| "Today we are going to discuss artificial intelligence. " + | ||
| "Let me start by defining what AI actually means in practice. " + | ||
| "AI is a broad field that includes machine learning, deep learning, and more.", | ||
| result, | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `strip html tags from cue text`() { | ||
| val vttWithTags = """ | ||
| |WEBVTT | ||
| | | ||
| |00:00:00.000 --> 00:00:10.000 | ||
| |<v Alice>This is a sentence with enough words to pass the minimum threshold for extraction. | ||
| """.trimMargin() | ||
|
|
||
| val result = TranscriptWindowExtractor.parseVttWindow(vttWithTags, timeSecs = 5, windowSecs = 10) | ||
|
|
||
| assertEquals( | ||
| "This is a sentence with enough words to pass the minimum threshold for extraction.", | ||
| result, | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.