Skip to content
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

Ngram Aligner Display #396

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions src/fragmentarium/application/FragmentService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const fragmentRepository = {
findInCorpus: jest.fn(),
query: jest.fn(),
listAllFragments: jest.fn(),
ngramScores: jest.fn(),
}

const imageRepository = {
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
6 changes: 6 additions & 0 deletions src/fragmentarium/domain/ngramMatching.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ChapterId } from 'transliteration/domain/chapter-id'

export type NgramScore = ChapterId & {
score: number
textName: string
}
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
85 changes: 85 additions & 0 deletions src/fragmentarium/ui/ngram-matching/NgramMatching.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
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 { chapterIdToString } from 'transliteration/domain/chapter-id'
import _ from 'lodash'
import { Markdown } from 'common/Markdown'
import { genreFromAbbr } from 'corpus/ui/Corpus'

function NgramMatchingHeadTags({ number }: { number: string }): JSX.Element {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

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={`Find Similar Chapters for ${number}`}
>
<SessionContext.Consumer>
{(session: Session): ReactNode =>
session.isAllowedToReadFragments() ? (
<>
<NgramMatchingHeadTags number={number} />
<Container>
{ngramScores.map((score, index) => (
<Row key={index}>
<Col xs={1}>{index + 1}.</Col>
<Col>
<Markdown text={genreFromAbbr(score.textId.genre)} />
{score.textName && (
<>
{' > '}
<Markdown text={score.textName} />
</>
)}
{' > '}
{chapterIdToString(_.omit(score, 'score', 'textName'))}
</Col>
<Col xs={1}>{score.score.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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar blocks of code found in 2 locations. Consider refactoring.

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
Loading