Commit eefb8d88 authored by nanahira's avatar nanahira

first

parent 7d548070
Pipeline #10204 passed with stages
in 4 minutes and 5 seconds
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
.git*
Dockerfile
.dockerignore
/tests
webpack.config.js
dist/*
build/*
*.js
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
stages:
- build
- combine
- deploy
variables:
GIT_DEPTH: "1"
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
CONTAINER_TEST_ARM_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-arm
CONTAINER_TEST_X86_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-x86
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build-x86:
stage: build
tags:
- docker
script:
- git submodule update --init --recursive
- TARGET_IMAGE=$CONTAINER_TEST_IMAGE
- docker build --pull -t $TARGET_IMAGE .
- docker push $TARGET_IMAGE
deploy_latest:
stage: deploy
tags:
- docker
script:
- TARGET_IMAGE=$CONTAINER_RELEASE_IMAGE
- SOURCE_IMAGE=$CONTAINER_TEST_IMAGE
- docker pull $SOURCE_IMAGE
- docker tag $SOURCE_IMAGE $TARGET_IMAGE
- docker push $TARGET_IMAGE
only:
- master
deploy_tag:
stage: deploy
tags:
- docker
script:
- TARGET_IMAGE=$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
- SOURCE_IMAGE=$CONTAINER_TEST_IMAGE
- docker pull $SOURCE_IMAGE
- docker tag $SOURCE_IMAGE $TARGET_IMAGE
- docker push $TARGET_IMAGE
only:
- tags
[submodule "chinese-poetry"]
path = chinese-poetry
url = https://github.com/chinese-poetry/chinese-poetry
/install-npm.sh
.git*
/data
/output
/config.yaml
.idea
.dockerignore
Dockerfile
/src
/coverage
/tests
/dist/tests
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
FROM node:bullseye-slim as base
LABEL Author="Nanahira <nanahira@momobako.com>"
RUN apt update && apt -y install python3 build-essential && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/log/*
WORKDIR /usr/src/app
COPY ./package*.json ./
FROM base as builder
RUN npm ci && npm cache clean --force
COPY . ./
RUN npm run build
FROM base
ENV NODE_ENV production
RUN npm ci && npm cache clean --force
COPY --from=builder /usr/src/app/dist ./dist
CMD [ "npm", "start" ]
The MIT License (MIT)
Copyright (c) 2021 Nanahira
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Subproject commit dfad57f3945be1d2bf5d68055144f134718f0baa
import readline from 'readline';
import { search } from './src/main';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '> ',
});
rl.on('line', async (line) => {
if (line.length && !line.match(/^\s*$/)) {
const result = await search(line);
}
rl.prompt();
}).on('close', () => {
process.exit(0);
});
console.log(`Poet killer by Nanahira.`);
rl.prompt();
#!/bin/bash
npm install --save-dev \
@types/node \
typescript \
'@typescript-eslint/eslint-plugin@^4.28.2' \
'@typescript-eslint/parser@^4.28.2 '\
'eslint@^7.30.0' \
'eslint-config-prettier@^8.3.0' \
'eslint-plugin-prettier@^3.4.0' \
prettier \
jest \
@types/jest \
ts-jest \
rimraf
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "poem-killer",
"description": "灰流丽的猜古诗答题器。",
"private": true,
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint --fix .",
"build": "rimraf dist && tsc",
"test": "jest --passWithNoTests",
"start": "node dist/index.js"
},
"repository": {
"type": "git",
"url": "https://code.mycard.moe/3rdeye/poem-killer.git"
},
"author": "Nanahira <nanahira@momobako.com>",
"license": "MIT",
"keywords": [],
"bugs": {
"url": "https://code.mycard.moe/3rdeye/poem-killer/issues"
},
"homepage": "https://code.mycard.moe/3rdeye/poem-killer",
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "tests",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/lodash": "^4.14.179",
"@types/node": "^17.0.21",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.4.0",
"eslint-plugin-prettier": "^3.4.1",
"jest": "^27.5.1",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.3",
"typescript": "^4.6.2"
},
"dependencies": {
"chinese-simple2traditional": "^1.0.1",
"load-json-file": "^6.2.0",
"lodash": "^4.17.21",
"moment": "^2.29.1"
}
}
import { getQueryRegex, matchPoem } from './utils/match';
import { readPoems } from './utils/reader';
import _ from 'lodash';
import { readFilelist } from './utils/filelist';
import { MatchResult } from './utils/def';
import moment from 'moment';
async function searchInFile(
regex: RegExp,
file: string,
): Promise<MatchResult[]> {
// console.log(`Searching in ${file} ...`);
const poems = await readPoems(file);
const matches = _.compact(
poems.map((poem) => {
const result = matchPoem(poem, regex);
if (result) {
console.log(
`Found line:\n${result.matching}\n${poem.title || poem.rhythmic} - ${
poem.author
}\n${poem.paragraphs.join('')}\n\n`,
);
}
return result;
}),
);
return matches;
}
export async function search(query: string) {
const regex = getQueryRegex(query);
console.log(`Searching for ${regex.toString()} .`);
const startTime = moment();
const fileList = await readFilelist();
const results = _.flatten(
await Promise.all(fileList.map((file) => searchInFile(regex, file))),
);
console.log(
`Found ${results.length} matches. Time used: ${
moment().diff(startTime, 'ms') / 1000
} s.`,
);
return results;
}
import { Poem } from './def';
import { traditionToSimple } from 'chinese-simple2traditional';
export const convertPoem = (poem: Poem): Poem => {
poem.author = traditionToSimple(poem.author);
poem.paragraphs = poem.paragraphs.map((p) => traditionToSimple(p));
if (poem.rhythmic) {
poem.rhythmic = traditionToSimple(poem.rhythmic);
}
if (poem.title) {
poem.title = traditionToSimple(poem.title);
}
return poem;
};
export interface Poem {
author: string;
paragraphs: string[];
rhythmic?: string;
title?: string;
}
export interface MatchResult {
found: boolean;
poem?: Poem;
matching?: string;
}
import * as fs from 'fs';
import path from 'path';
import _ from 'lodash';
export const readFilelist = async () =>
_.flatten(
await Promise.all(
[
{ dir: 'json', prefix: 'poet' },
{ dir: 'ci', prefix: 'ci' },
].map(async (entry) =>
(await fs.promises.readdir(path.join('chinese-poetry', entry.dir)))
.filter(
(file) => file.endsWith('json') && file.startsWith(entry.prefix),
)
.map((file) => path.join('chinese-poetry', entry.dir, file)),
),
),
);
import { MatchResult, Poem } from './def';
const replacements = [
'\uFF0C\u3002\uFF1F\uFF01\uFF1B\u3001\\,\\.\\?\\!\\;',
'一壹',
'二贰',
'三叁',
'四肆',
'五伍',
'六陆',
'七柒',
'八捌',
'九玖',
'十拾',
'百佰',
'千仟',
'万萬',
];
const replacementsEntry = replacements.map((r) => {
const wordWithSym = `[${r}]`;
return {
word: wordWithSym,
regex: new RegExp(wordWithSym, 'g'),
};
});
const nonSym = '[^\uFF0C\u3002\uFF1F\uFF01\uFF1B\u3001\\,\\.\\?\\!\\;]';
export const getQueryRegex = (query: string) => {
let word = query.trim();
for (const entry of replacementsEntry) {
word = word.replace(entry.regex, entry.word);
}
word = word
.replace(/\s*\(\s*\)\s*/g, nonSym)
.replace(/\s*\uff08\s*\uff09\s*/g, nonSym);
return new RegExp(word);
};
export const matchPoem = (poem: Poem, regex: RegExp): MatchResult => {
/*const suffix = 'js';
const result = await new Promise<MatchResult>((resolve, reject) => {
const worker = new Worker(
path.resolve(process.cwd(), 'worker', `match.${suffix}`),
{
workerData: {
poem,
regex,
},
},
);
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
});*/
const wholeParagraph = poem.paragraphs.join('');
const matching = wholeParagraph.match(regex)?.[0];
if (!matching) return;
return { matching, poem, found: true };
};
import { Poem } from './def';
import loadJsonFile from 'load-json-file';
import { convertPoem } from './convert';
export const readPoems = async (file: string) => {
const poems: Poem[] = await loadJsonFile(file);
return poems.map(convertPoem);
};
import { readFilelist } from '../src/utils/filelist';
describe('filelist', () => {
it('should return an array of files', async () => {
const filelist = await readFilelist();
console.log(filelist);
expect(filelist).toBeInstanceOf(Array);
expect(filelist[0].startsWith('chinese-poetry')).toBe(true);
});
});
import { Poem } from '../src/utils/def';
import { getQueryRegex, matchPoem } from '../src/utils/match';
describe('match', () => {
const poem: Poem = {
author: '孟浩然',
title: '春晓',
paragraphs: ['春眠不觉晓,处处闻啼鸟。', '夜来风雨声,花落知多少。'],
};
it('should match a poem', () => {
const regex = getQueryRegex(
'() ( ) ()觉()。()处()()(),',
);
console.log(regex.toString());
const match = matchPoem(poem, regex);
expect(match).toBeTruthy();
expect(match.poem).toEqual(poem);
expect(match.matching).toEqual('春眠不觉晓,处处闻啼鸟。');
});
it('it should not match invalid', () => {
expect(
matchPoem(
poem,
getQueryRegex('()()()叫()。()处()()(),'),
),
).toBeFalsy();
});
});
import { readPoems } from '../src/utils/reader';
import { getQueryRegex, matchPoem } from '../src/utils/match';
describe('reader', () => {
it('should read a file', async () => {
const poems = await readPoems('chinese-poetry/json/poet.song.173000.json');
expect(poems).toBeInstanceOf(Array);
expect(poems.length).toBeGreaterThan(0);
const poem = poems[0];
console.log(poem);
expect(poem.author).toBeDefined();
expect(poem.paragraphs).toBeInstanceOf(Array);
expect(poem.paragraphs[0]).toBeDefined();
const regex = getQueryRegex(
'一()()()()()骨,曲身()()()()()。',
);
console.log(regex.toString());
const match = matchPoem(poem, regex);
expect(match).toBeDefined();
expect(match.poem).toEqual(poem);
expect(match.matching).toBe('一朝入火纵粉骨,曲身得煖龙蛇伸。');
});
});
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es2021",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts",
"src/**/*.ts",
"test/**/*.ts",
"tests/**/*.ts"
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment