From 4239b44e34641eba8172a8d5cfa0531fb3e657a5 Mon Sep 17 00:00:00 2001 From: fsimonjetz Date: Mon, 25 Sep 2023 15:00:12 +0000 Subject: [PATCH] add NgramMatching prototype --- src/corpus/ui/search/CorpusSearchResult.tsx | 2 +- .../application/FragmentService.ts | 6 ++ src/fragmentarium/domain/ngramMatching.ts | 5 ++ .../infrastructure/FragmentRepository.ts | 5 ++ .../ui/ngram-matching/NgramMatching.tsx | 79 +++++++++++++++++++ src/router/fragmentariumRoutes.tsx | 11 +++ 6 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 src/fragmentarium/domain/ngramMatching.ts create mode 100644 src/fragmentarium/ui/ngram-matching/NgramMatching.tsx diff --git a/src/corpus/ui/search/CorpusSearchResult.tsx b/src/corpus/ui/search/CorpusSearchResult.tsx index 68c9b757c..9d570c9c1 100644 --- a/src/corpus/ui/search/CorpusSearchResult.tsx +++ b/src/corpus/ui/search/CorpusSearchResult.tsx @@ -16,7 +16,7 @@ import TranslationContext, { import { Markdown } from 'common/Markdown' import { genreFromAbbr } from '../Corpus' -function GenreInfoRow({ +export function GenreInfoRow({ chapterId, textName, }: { diff --git a/src/fragmentarium/application/FragmentService.ts b/src/fragmentarium/application/FragmentService.ts index 386de4596..c7c75858f 100644 --- a/src/fragmentarium/application/FragmentService.ts +++ b/src/fragmentarium/application/FragmentService.ts @@ -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') { @@ -87,6 +88,7 @@ export interface FragmentRepository { findLemmas(lemma: string, isNormalized: boolean): Bluebird fetchCdliInfo(cdliNumber: string): Bluebird lineToVecRanking(number: string): Bluebird + ngramScores(number: string): Bluebird query(fragmentQuery: FragmentQuery): Bluebird listAllFragments(): Bluebird } @@ -333,6 +335,10 @@ export class FragmentService { return this.fragmentRepository.query(fragmentQuery) } + ngramScores(number: string): Bluebird { + return this.fragmentRepository.ngramScores(number) + } + private injectReferences(fragment: Fragment): Bluebird { return this.referenceInjector .injectReferencesToText(fragment.text) diff --git a/src/fragmentarium/domain/ngramMatching.ts b/src/fragmentarium/domain/ngramMatching.ts new file mode 100644 index 000000000..34cf8a9f3 --- /dev/null +++ b/src/fragmentarium/domain/ngramMatching.ts @@ -0,0 +1,5 @@ +import { ChapterId } from 'transliteration/domain/chapter-id' + +export type NgramScore = ChapterId & { + overlap: number +} diff --git a/src/fragmentarium/infrastructure/FragmentRepository.ts b/src/fragmentarium/infrastructure/FragmentRepository.ts index 891a7df9e..3a6dd56b2 100644 --- a/src/fragmentarium/infrastructure/FragmentRepository.ts +++ b/src/fragmentarium/infrastructure/FragmentRepository.ts @@ -47,6 +47,7 @@ import { ArchaeologyDto, createArchaeology, } from 'fragmentarium/domain/archaeology' +import { NgramScore } from 'fragmentarium/domain/ngramMatching' export function createScript(dto: ScriptDto): Script { return { @@ -388,6 +389,10 @@ class ApiFragmentRepository listAllFragments(): Promise { return this.apiClient.fetchJson(`/fragments/all`, false) } + + ngramScores(number: string): Promise { + return this.apiClient.fetchJson(createFragmentPath(number, 'ngrams'), false) + } } export default ApiFragmentRepository diff --git a/src/fragmentarium/ui/ngram-matching/NgramMatching.tsx b/src/fragmentarium/ui/ngram-matching/NgramMatching.tsx new file mode 100644 index 000000000..430a6bf6f --- /dev/null +++ b/src/fragmentarium/ui/ngram-matching/NgramMatching.tsx @@ -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 ( + + ) +} + +function NgramMatching({ + number, + ngramScores, +}: { + number: string + ngramScores: readonly NgramScore[] +}): JSX.Element { + return ( + + + {(session: Session): ReactNode => + session.isAllowedToReadFragments() ? ( + <> + + + {ngramScores.map((score, index) => ( + + + { + + } + + {score.overlap.toFixed(4)} + + ))} + + + ) : ( + 'Please log in to search for matching chapters' + ) + } + + + ) +} + +export default withData< + { fragmentService: FragmentService; number: string }, + { number: string }, + readonly NgramScore[] +>( + ({ data, ...props }) => ( + + ), + (props) => props.fragmentService.ngramScores(props.number) +) diff --git a/src/router/fragmentariumRoutes.tsx b/src/router/fragmentariumRoutes.tsx index dc3049add..b713d4ba4 100644 --- a/src/router/fragmentariumRoutes.tsx +++ b/src/router/fragmentariumRoutes.tsx @@ -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] @@ -120,6 +121,16 @@ export default function FragmentariumRoutes({ /> )} />, + ( + + )} + />,