Skip to content

Commit

Permalink
add NgramMatching prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
fsimonjetz committed Sep 25, 2023
1 parent 06aed0c commit 4239b44
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/corpus/ui/search/CorpusSearchResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import TranslationContext, {
import { Markdown } from 'common/Markdown'
import { genreFromAbbr } from '../Corpus'

function GenreInfoRow({
export function GenreInfoRow({
chapterId,
textName,
}: {
Expand Down
6 changes: 6 additions & 0 deletions src/fragmentarium/application/FragmentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { FragmentQuery } from 'query/FragmentQuery'
import { QueryResult } from 'query/QueryResult'
import { MesopotamianDate } from 'fragmentarium/domain/Date'
import { ArchaeologyDto } from 'fragmentarium/domain/archaeology'
import { NgramScore } from 'fragmentarium/domain/ngramMatching'

export const onError = (error) => {
if (error.message === '403 Forbidden') {
Expand Down Expand Up @@ -87,6 +88,7 @@ export interface FragmentRepository {
findLemmas(lemma: string, isNormalized: boolean): Bluebird<Word[][]>
fetchCdliInfo(cdliNumber: string): Bluebird<CdliInfo>
lineToVecRanking(number: string): Bluebird<LineToVecRanking>
ngramScores(number: string): Bluebird<NgramScore[]>
query(fragmentQuery: FragmentQuery): Bluebird<QueryResult>
listAllFragments(): Bluebird<string[]>
}
Expand Down Expand Up @@ -333,6 +335,10 @@ export class FragmentService {
return this.fragmentRepository.query(fragmentQuery)
}

ngramScores(number: string): Bluebird<NgramScore[]> {
return this.fragmentRepository.ngramScores(number)
}

private injectReferences(fragment: Fragment): Bluebird<Fragment> {
return this.referenceInjector
.injectReferencesToText(fragment.text)
Expand Down
5 changes: 5 additions & 0 deletions src/fragmentarium/domain/ngramMatching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ChapterId } from 'transliteration/domain/chapter-id'

export type NgramScore = ChapterId & {
overlap: number
}
5 changes: 5 additions & 0 deletions src/fragmentarium/infrastructure/FragmentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import {
ArchaeologyDto,
createArchaeology,
} from 'fragmentarium/domain/archaeology'
import { NgramScore } from 'fragmentarium/domain/ngramMatching'

export function createScript(dto: ScriptDto): Script {
return {
Expand Down Expand Up @@ -388,6 +389,10 @@ class ApiFragmentRepository
listAllFragments(): Promise<string[]> {
return this.apiClient.fetchJson(`/fragments/all`, false)
}

ngramScores(number: string): Promise<NgramScore[]> {
return this.apiClient.fetchJson(createFragmentPath(number, 'ngrams'), false)
}
}

export default ApiFragmentRepository
79 changes: 79 additions & 0 deletions src/fragmentarium/ui/ngram-matching/NgramMatching.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React, { ReactNode } from 'react'
import AppContent from 'common/AppContent'
import { SectionCrumb, TextCrumb } from 'common/Breadcrumbs'
import FragmentCrumb from 'fragmentarium/ui/FragmentCrumb'
import SessionContext from 'auth/SessionContext'
import { Session } from 'auth/Session'
import withData from 'http/withData'
import { HeadTags } from 'router/head'
import FragmentService from 'fragmentarium/application/FragmentService'
import { NgramScore } from 'fragmentarium/domain/ngramMatching'
import { Col, Container, Row } from 'react-bootstrap'
import { GenreInfoRow } from 'corpus/ui/search/CorpusSearchResult'
import _ from 'lodash'

function NgramMatchingHeadTags({ number }: { number: string }): JSX.Element {
return (
<HeadTags
title={`Fragment ${number} N-gram Matching: eBL`}
description={`Fragment ${number} n-gram matching in the electronic Babylonian Library (eBL) Fragmentarium.`}
/>
)
}

function NgramMatching({
number,
ngramScores,
}: {
number: string
ngramScores: readonly NgramScore[]
}): JSX.Element {
return (
<AppContent
crumbs={[
new SectionCrumb('Fragmentarium'),
new FragmentCrumb(number),
new TextCrumb('N-Gram Matching'),
]}
title={`N-Gram Matching for ${number}`}
>
<SessionContext.Consumer>
{(session: Session): ReactNode =>
session.isAllowedToReadFragments() ? (
<>
<NgramMatchingHeadTags number={number} />
<Container>
{ngramScores.map((score, index) => (
<Row key={index}>
<Col>
{
<GenreInfoRow
chapterId={_.omit(score, 'overlap')}
textName={''}
/>
}
</Col>
<Col>{score.overlap.toFixed(4)}</Col>
</Row>
))}
</Container>
</>
) : (
'Please log in to search for matching chapters'
)
}
</SessionContext.Consumer>
</AppContent>
)
}

export default withData<
{ fragmentService: FragmentService; number: string },
{ number: string },
readonly NgramScore[]
>(
({ data, ...props }) => (
<NgramMatching ngramScores={data} number={props.number} />
),
(props) => props.fragmentService.ngramScores(props.number)
)
11 changes: 11 additions & 0 deletions src/router/fragmentariumRoutes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import SignService from 'signs/application/SignService'
import { FragmentSlugs, sitemapDefaults } from 'router/sitemap'
import { HeadTagsService } from 'router/head'
import BibliographyService from 'bibliography/application/BibliographyService'
import NgramMatching from 'fragmentarium/ui/ngram-matching/NgramMatching'

function parseStringParam(location: Location, param: string): string | null {
const value = parse(location.search)[param]
Expand Down Expand Up @@ -120,6 +121,16 @@ export default function FragmentariumRoutes({
/>
)}
/>,
<Route
key="NgramScore"
path="/fragmentarium/:id/ngrams"
render={({ match }): ReactNode => (
<NgramMatching
fragmentService={fragmentService}
number={decodeURIComponent(match.params.id)}
/>
)}
/>,
<Route
key="FragmentView"
path="/fragmentarium/:id"
Expand Down

0 comments on commit 4239b44

Please sign in to comment.