Project Japanesehelp

Study Japanese with reading comprehension in focus. Made with Angular, Node.js and Basebase.

Regex for Japanese Keywords

Recently I came over a great Regex expression for sorting out Japanese keywords. I have never seen this on StackOverflow and it is really something I’ve been searching for. I must say I was really surprised finding this, because I didn’t think it existed!

Regex Expression below

([\((「『]+.*?[\))」』]|\ |<br>|[a-zA-Z0-9]+\.[a-z]{2,}|[一-龠々〆ヵヶゝ]+|[ぁ-んゝ]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+)

Same code as above but without the br tag and non-breakable space character entity reference.

/([\((「『]+.*?[\))」』]|[a-zA-Z0-9]+\.[a-z]{2,}|[一-龠々〆ヵヶゝ]+|[ぁ-んゝ]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+)/g;

Example JavaScipt Code with the Regex in action

const regex = /([\((「『]+.*?[\))」』]|\ |<br>|[a-zA-Z0-9]+\.[a-z]{2,}|[一-龠々〆ヵヶゝ]+|[ぁ-んゝ]+|[ァ-ヴー]+|[a-zA-Z0-9]+|[a-zA-Z0-9]+)/g;
const str = `【AFP=時事】新型コロナウイルスのパンデミック(世界的な大流行)が7月に入って急加速していることが、各国・機関のデータを基にしたAFPの集計で明らかになった。

`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

Source

Found this on qiita.com posted by ykkey.

Great way to map up objects in TypeScript to keep objects initialized

Available in TypeScript 2.1 there is now the possibility to set partial properties values of objects, making it very easy to initialize new objects.

I had the problem before that whenever I wanted to create a new object and also initilize a new object, I had to set my values in the constructor, all my values.

export class Example{

    text: string;
    constructor(init?: Partial<Example>) {

        // This will set values for each properties that can be found
        Object.assign(this, init);

    }
}

Now with the above code I can run a map function on an array and use the object itself as a constructor parameter. Like below.

// array filled with data is coming from firestore in this example
const ExamplesObs = <Observable<Example>>this.db.doc(`example/mycoolexampleDoc`).valueChanges().pipe(
            map((fbl: Example) => {
                let ex: Example = new Example(ex);
                return ex
})

// Subscribe to the Observable etc...

The Observable will return an initialized object of the Example class. Now I am using firestore to get the data (in json format) and what will come back is simply just the json structure so this would be a great way to put the data into the real object with the help of a constructor with Partial.