Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import BaseParser from "../../BaseParser";
- import { getRegexPosition, fuzzyMatching } from "../../util";
- import { orderBy } from "lodash";
- import { DISTRICT_KEYWORDS } from "../../keywords/const-district";
- import { PROVINCE_KEYWORDS } from "../../keywords/const-province";
- import {
- SOURCE_KEYWORDS,
- DESTINATION_KEYWORDS
- } from "../../keywords/const-direction";
- let DICT_PROVINCE_KEYWORDS = [];
- export default class LocationRuleMultiTask extends BaseParser {
- constructor() {
- super();
- }
- PreProcess(train) {}
- Parse(message) {
- const { origin, dest } = LocationRuleMultiTask.getProvinceSourceDestination(message);
- return {
- origin_province: !origin ? null : origin.province,
- dest_province: !dest ? null : dest.province,
- origin: !origin ? null : origin,
- dest: !dest ? null : dest
- };
- }
- // TODO:
- static getProvinceSourceDestination(msg) {
- // Search District Name in message
- if (!msg) {
- return { origin: null, dest: null };
- }
- let msg2 = msg.split(' ').join('#');
- let message = msg2.replace(/[-\(\)##\:]/g, "");//let message = msg.replace(/[-\(\)##\:]/g, "");
- // console.log("message", message);
- let result = []; //[Simple/Complex][workID][Direction][Location]
- let CSSKeyResult = [];
- let CSDKeyResult = [];
- let SKeyResult = [];
- let DKeyResult = [];
- let SnDKeyResult = [];
- //let sourceWord = [];
- //let destinationWord = [];
- let breakLineIndex = getRegexPosition(/\n/g, message);
- // console.log("breakLineIndex", breakLineIndex);
- let ii = 0;
- let foundCSSKey = false;
- let foundCSDKey = false;
- let foundCSSKeyNearDSKey = false;
- let foundCSDKeyNearDSKey = false;
- let workNum = 0;
- for (let keyword of COMPLEX_SENTENCE_SOURCE_KEYWORDS) {
- let index = [];
- index = getRegexPositionMultiTask(keyword, message,0);
- for (let re of index)
- {
- CSSKeyResult.push(re);
- }
- if (CSSKeyResult[0] && CSSKeyResult.length > 0) {
- foundCSSKey = true;
- }
- ii++;
- }
- ii=0;
- for (let keyword of COMPLEX_SENTENCE_DESTINATION_KEYWORDS) {
- let index = [];
- index = getRegexPositionMultiTask(keyword, message,1);
- for (let re of index)
- {
- CSDKeyResult.push(re);
- }
- if (CSDKeyResult[0] && CSDKeyResult.length > 0) {
- foundCSDKey = true;
- }
- ii++;
- }
- //console.log(foundCSSKey,foundCSDKey);
- let sourceWord = [];
- let destinationWord = [];
- let result2 = [];
- for (let keyword of DISTRICT_KEYWORDS) {
- let index = [];
- ii++;
- if (keyword.district && keyword.district.length <= 3) {
- index = getRegexPosition(
- "(อ.|อำเภอ|ลูกศร|ขึ้น|ลง|up|down| )" + keyword.keyword + "( |\n)",
- message
- );
- } else {
- index = getRegexPosition(keyword.keyword, message);
- }
- if (index && index.length > 0) {
- result2.push(
- ...index.map(idx =>
- Object.assign({}, keyword, {
- index: idx
- })
- )
- );
- }
- }
- sourceWord = findPrefixWord(message, SOURCE_KEYWORDS);
- destinationWord = findPrefixWord(message, DESTINATION_KEYWORDS);
- sourceWord = removePrefixContainInResult(sourceWord, result2);
- destinationWord = removePrefixContainInResult(destinationWord, result2);
- for (let sw of sourceWord) {
- let inputSW = {Index:sw.index, Flag:2, Keyword:sw.keyword}
- SKeyResult.push(inputSW);
- }
- for (let dw of destinationWord) {
- let inputDW = {Index:dw.index, Flag:3, Keyword:dw.keyword}
- DKeyResult.push(inputDW);
- }
- SnDKeyResult = SKeyResult.concat(DKeyResult);
- SnDKeyResult.sort(function(a,b){return a.Index - b.Index});
- let IsUndefined = false;
- let IsSimpleSingleTask = false; //ขึ้น-ลง
- let IsSimpleMultiTask = false; //n ขึ้น ,n ลง
- let IsSimpleMultiPairTask = false; //ขึ้น-ลง, ขึ้น-ลง, ...
- let IsSimpleMultiNotPairTask = false; //ขึ้น, ขึ้น, ...- ลง, ลง ...
- let IsSimpleMulti_SingleNotPairMultiTask = false; //ขึ้น- ลง, ลง ...
- let IsSimpleMulti_MultiNotPairSingleTask = false; //ขึ้น, ขึ้น, ...- ลง
- let IsSimpleMulti_MultiNotPairMultiTask = false; //ขึ้น, ขึ้น, ...- ลง, ลง ...
- let IsComplexSingleSingle_MultiTask = false; //ไป - กลับ
- let IsComplexSingleMulti_MultiTask = false; //ไป - กลับ
- let IsComplexMultiSingle_MultiTask = false; //ไป - กลับ
- let IsComplexMultiMulti_MultiTask = false; //ไป - กลับ
- let numFlag0 = SnDKeyResult.filter(item => item.Flag === 0).length;
- let numFlag1 = SnDKeyResult.filter(item => item.Flag === 1).length;
- let numFlag2 = SnDKeyResult.filter(item => item.Flag === 2).length;
- let numFlag3 = SnDKeyResult.filter(item => item.Flag === 3).length;
- let flagChangeIndex = [];
- ii=0;
- if(numFlag2 === 1 && numFlag3 ===1)
- {
- //ขึ้น-ลง
- IsSimpleSingleTask = true; //ขึ้น-ลง
- for(let res of SnDKeyResult)
- {
- let inputResult = {Index:res.Index, WorkIndex:0 ,Flag:res.Flag,Keyword:res.Keyword};
- result.push(inputResult);
- }
- }
- else if(numFlag2 === 0 && numFlag3 >=1)
- {
- IsUndefined = true;
- }
- else if(numFlag2 >= 1 && numFlag3 ===0)
- {
- IsUndefined = true;
- }
- else
- {
- //n ขึ้น ,n ลง
- //|-ขึ้น-ลง, ขึ้น-ลง, ...
- //'-ขึ้น, ขึ้น, ...- ลง, ลง ...
- // |-ขึ้น- ลง, ลง ...
- // |-ขึ้น, ขึ้น, ...- ลง
- // '-ขึ้น, ขึ้น, ...- ลง, ลง ...
- let tempFlag = -1;
- for(let res of SnDKeyResult)
- {
- if(res.Flag != tempFlag && tempFlag != -1)
- {
- flagChangeIndex.push(res);
- }
- tempFlag = res.Flag;
- }
- if(flagChangeIndex.length === 0)
- {
- IsUndefined = true;
- }
- else if(flagChangeIndex.length === 1)
- {
- //n ขึ้น ,n ลง
- //'-ขึ้น, ขึ้น, ...- ลง, ลง ...
- // |-ขึ้น- ลง, ลง ...
- // |-ขึ้น, ขึ้น, ...- ลง
- // '-ขึ้น, ขึ้น, ...- ลง, ลง ...
- if(numFlag2 === 1 && numFlag3 > 1)
- {
- let workIndex = 0;
- let tempFlag2Result = {Index:-1, WorkIndex:-1 ,Flag:-1,Keyword:""};
- for(let res of SnDKeyResult)
- {
- if(res.Flag === 2)
- {
- tempFlag2Result.Index= res.Index;
- tempFlag2Result.WorkIndex = workIndex;
- tempFlag2Result.Flag = res.Flag;
- tempFlag2Result.Keyword = res.Keyword;
- }
- }
- for(let res of SnDKeyResult)
- {
- if(res.Flag === 3)
- {
- let inputResultFlag2 = {Index:tempFlag2Result.Index, WorkIndex:workIndex ,Flag:tempFlag2Result.Flag,Keyword:tempFlag2Result.Keyword};
- result.push(inputResultFlag2);
- let inputResult = {Index:res.Index, WorkIndex:workIndex ,Flag:res.Flag,Keyword:res.Keyword};
- result.push(inputResult);
- workIndex++;
- }
- }
- IsSimpleMultiNotPairTask = true; //ขึ้น, ขึ้น, ...- ลง, ลง ...
- IsSimpleMulti_SingleNotPairMultiTask = true; //ขึ้น- ลง, ลง ...
- }
- else if(numFlag2 > 1 && numFlag3 === 1)
- {
- let workIndex = 0;
- let tempFlag3Result = {Index:-1, WorkIndex:-1 ,Flag:-1,Keyword:""};
- for(let res of SnDKeyResult)
- {
- if(res.Flag === 3)
- {
- tempFlag3Result.Index= res.Index;
- tempFlag3Result.WorkIndex = workIndex;
- tempFlag3Result.Flag = res.Flag;
- tempFlag3Result.Keyword = res.Keyword;
- }
- }
- for(let res of SnDKeyResult)
- {
- if(res.Flag === 2)
- {
- let inputResult = {Index:res.Index, WorkIndex:workIndex ,Flag:res.Flag,Keyword:res.Keyword};
- result.push(inputResult);
- let inputResultFlag3 = {Index:tempFlag3Result.Index, WorkIndex:workIndex ,Flag:tempFlag3Result.Flag,Keyword:tempFlag3Result.Keyword};
- result.push(inputResultFlag3);
- workIndex++;
- }
- }
- IsSimpleMultiNotPairTask = true; //ขึ้น, ขึ้น, ...- ลง, ลง ...
- IsSimpleMulti_MultiNotPairSingleTask = true; //ขึ้น, ขึ้น, ...- ลง
- }
- else if(numFlag2 > 1 && numFlag3 > 1)
- {
- let workIndex = 0;
- for(let res2 of SnDKeyResult)
- {
- if(res2.Flag === 2)
- {
- for(let res3 of SnDKeyResult)
- {
- if(res3.Flag === 3)
- {
- let inputResultFlag2 = {Index:res2.Index, WorkIndex:workIndex ,Flag:res2.Flag,Keyword:res2.Keyword};
- result.push(inputResultFlag2);
- let inputResultFlag3 = {Index:res3.Index, WorkIndex:workIndex ,Flag:res3.Flag,Keyword:res3.Keyword};
- result.push(inputResultFlag3);
- workIndex++;
- }
- }
- }
- }
- IsSimpleMultiNotPairTask = true; //ขึ้น, ขึ้น, ...- ลง, ลง ...
- IsSimpleMulti_MultiNotPairMultiTask = true; //ขึ้น, ขึ้น, ...- ลง, ลง ...
- }
- }
- else if(flagChangeIndex.length > 1)
- {
- //n ขึ้น ,n ลง
- //'-ขึ้น-ลง, ขึ้น-ลง, ...
- //if(flagChangeIndex.length === (numFlag2 + numFlag3 - 1))
- {
- let tempFlag = -1;
- let flipFlopCount = 0; //let flipFlopFlag = true;
- let workIndex = 0;
- for(let res of SnDKeyResult)
- {
- if(tempFlag === -1)
- {
- let inputResult = {Index:res.Index, WorkIndex:workIndex ,Flag:res.Flag,Keyword:res.Keyword};
- result.push(inputResult);
- }
- else if(res.Flag === tempFlag)
- {
- let inputResult = {Index:res.Index, WorkIndex:workIndex ,Flag:res.Flag,Keyword:res.Keyword};
- result.push(inputResult);
- }
- else if(res.Flag != tempFlag && tempFlag != -1)
- {
- if(res.Flag != tempFlag)
- {
- flipFlopCount++;
- }
- if(flipFlopCount === 2)
- {
- workIndex++;
- flipFlopCount = 0;
- }
- let inputResult = {Index:res.Index, WorkIndex:workIndex ,Flag:res.Flag,Keyword:res.Keyword};
- result.push(inputResult);
- }
- tempFlag = res.Flag;
- }
- }
- IsSimpleMultiPairTask = true; //ขึ้น-ลง, ขึ้น-ลง, ...
- }
- }
- if(foundCSSKey === true && foundCSDKey === true)
- {
- if(foundCSSKeyNearDSKey === true && foundCSDKeyNearDSKey === true)
- {
- //([simple = 0/complex = 1][num work]['up'/'down']['province'])
- //Result.push([simple/complex][work][up/down][province]);
- //workNum++;
- }
- }
- if (result2.length === 0) return { source: null, destination: null };
- result2 = orderBy(result, ["index"], ["asc"]);
- //result2.sort(function(a,b){return a.index - b.index});
- let source = getLocationMultiTask(result, result2,2);
- let destination = getLocationMultiTask(result, result2,3);
- if (!destination) {
- return { source: null, destination: null };
- }
- const sourceProvinceDetail = getProvinceDetail(source.province);
- const destProvinceDetail = getProvinceDetail(destination.province);
- let origin = Object.assign({}, source, {
- region: sourceProvinceDetail ? sourceProvinceDetail.region : null,
- geographical_region: sourceProvinceDetail
- ? sourceProvinceDetail.geographical_region
- : null
- });
- let dest = Object.assign({}, destination, {
- region: destProvinceDetail ? destProvinceDetail.region : null,
- geographical_region: destProvinceDetail
- ? destProvinceDetail.geographical_region
- : null
- });
- }
- }
- export default class LocationRule extends BaseParser {
- constructor() {
- super();
- }
- PreProcess(train) {}
- Parse(message) {
- const { origin, dest } = LocationRule.getProvinceSourceDestination(message);
- return {
- origin_province: !origin ? null : origin.province,
- dest_province: !dest ? null : dest.province,
- origin: !origin ? null : origin,
- dest: !dest ? null : dest
- };
- }
- // TODO:
- static getProvinceSourceDestination(msg) {
- // Search District Name in message
- if (!msg) {
- return { origin: null, dest: null };
- }
- let message = msg.replace(/[-\(\)#\:]/g, "");
- // console.log("message", message);
- let result = [];
- let sourceWord = [];
- let destinationWord = [];
- let breakLineIndex = getRegexPosition(/\n/g, message);
- // console.log("breakLineIndex", breakLineIndex);
- let ii = 0;
- for (let keyword of DISTRICT_KEYWORDS) {
- let index = [];
- ii++;
- if (keyword.district && keyword.district.length <= 3) {
- index = getRegexPosition(
- "(อ.|อำเภอ|ลูกศร|ขึ้น|ลง|up|down| )" + keyword.keyword + "( |\n)",
- message
- );
- } else {
- index = getRegexPosition(keyword.keyword, message);
- /*if (index && index.length === 0) {
- const newSearchWord = !keyword.district
- ? keyword.province
- : keyword.district;
- //console.log(msg, keyword);
- const { isMatch, index: messageIndex } = fuzzyMatching(
- message,
- newSearchWord
- );
- if (isMatch) {
- index = [messageIndex];
- }
- }*/
- }
- if (index && index.length > 0) {
- result.push(
- ...index.map(idx =>
- Object.assign({}, keyword, {
- index: idx
- })
- )
- );
- }
- }
- // console.log("result", result);
- //console.log(result, message);
- sourceWord = findPrefixWord(message, SOURCE_KEYWORDS);
- destinationWord = findPrefixWord(message, DESTINATION_KEYWORDS);
- // Remove prefix that contain in district keyword ex. ลง "อยู่ในคำว่า" แกลง
- sourceWord = removePrefixContainInResult(sourceWord, result);
- destinationWord = removePrefixContainInResult(destinationWord, result);
- //console.log("sourceWord", sourceWord);
- //console.log("destinationWord", destinationWord);
- // Sort index of district name
- if (result.length === 0) return { source: null, destination: null };
- result = orderBy(result, ["index"], ["asc"]);
- // console.log("eiei");
- //console.log("result", result);
- // First Index will be source province
- // Seconde Index (not same source province) will be destination
- let source = getSource(sourceWord, result);
- let destination = getDestination(
- destinationWord,
- result,
- source,
- breakLineIndex
- );
- //console.log("source", source);
- //console.log("destination", destination);
- if (!destination) {
- return { source: null, destination: null };
- }
- // Get Region
- const sourceProvinceDetail = LocationRule.getProvinceDetail(
- source.province
- );
- const destProvinceDetail = LocationRule.getProvinceDetail(
- destination.province
- );
- let origin = Object.assign({}, source, {
- region: sourceProvinceDetail ? sourceProvinceDetail.region : null,
- geographical_region: sourceProvinceDetail
- ? sourceProvinceDetail.geographical_region
- : null
- });
- let dest = Object.assign({}, destination, {
- region: destProvinceDetail ? destProvinceDetail.region : null,
- geographical_region: destProvinceDetail
- ? destProvinceDetail.geographical_region
- : null
- });
- return {
- origin,
- dest
- };
- }
- static getProvinceDetail(province) {
- if (!province) return null;
- // DICT SEARCH
- if (Object.keys(DICT_PROVINCE_KEYWORDS).length === 0) {
- for (let proObj of PROVINCE_KEYWORDS) {
- DICT_PROVINCE_KEYWORDS[proObj.province] = proObj;
- }
- }
- return DICT_PROVINCE_KEYWORDS[province];
- }
- }
- const findPrefixWord = (message, keywords) => {
- let result = [];
- for (let keyword of keywords) {
- let index = [];
- index = getRegexPosition(keyword, message);
- if (index && index.length > 0) {
- result.push(
- ...index.map(idx =>
- Object.assign(
- {},
- { keyword },
- {
- index: idx
- }
- )
- )
- );
- }
- }
- return result;
- };
- const getSource = (sourceWord, result) => {
- let source = null;
- let sourceCandidate = [];
- const offset = 4;
- for (let s of sourceWord) {
- for (let src of result) {
- if (
- s.index + s.keyword.length <= src.index &&
- src.index <= s.index + s.keyword.length + offset
- ) {
- sourceCandidate.push(src);
- break;
- }
- }
- }
- if (sourceCandidate.length > 0) {
- source = orderBy(sourceCandidate, ["index"], ["asc"])[0];
- result = result.filter(res => res.index != source.index);
- } else {
- source = result[0];
- result = result.slice(1);
- }
- return source;
- };
- const getDestination = (destinationWord, result, source, breakLineIndex) => {
- let destination = null;
- let destCandidate = [];
- for (let d of destinationWord) {
- for (let dest of result) {
- let nearestBreakLineIndex = getNearestBreakLine(d, breakLineIndex);
- //console.log("nearestBreakLine", nearestBreakLineIndex, d.index);
- let offset = 4;
- if (nearestBreakLineIndex) {
- offset = nearestBreakLineIndex - (d.index + d.keyword.length);
- }
- if (
- d.index + d.keyword.length <= dest.index &&
- dest.index <= d.index + d.keyword.length + offset &&
- source.index + source.length < dest.index
- ) {
- destCandidate.push(dest);
- break;
- }
- }
- }
- if (destCandidate.length > 0) {
- destination = orderBy(destCandidate, ["index"], ["asc"])[0];
- } else {
- for (let dest of result) {
- if (dest.province != source.province) {
- destination = dest;
- break;
- }
- }
- }
- return destination;
- };
- const removePrefixContainInResult = (prefix, result) => {
- if (!result || !prefix) return prefix;
- let newPrefix = [];
- for (let pre of prefix) {
- let prefixDup = result.filter(r => {
- return (
- r.district &&
- r.index <= pre.index &&
- pre.index <= r.index + r.district.length
- );
- });
- if (prefixDup.length === 0) newPrefix.push(pre);
- }
- return newPrefix;
- };
- const getNearestBreakLine = (prefix, breakLineIndex) => {
- if (!breakLineIndex || !prefix) return null;
- let nearestBreakLine = breakLineIndex.filter(bl => prefix.index < bl);
- //console.log("nearestBreakLine", nearestBreakLine);
- if (nearestBreakLine.length > 0) return nearestBreakLine[0];
- return null;
- };
- const getLocationMultiTask = (sourceWord, result,flag) => {
- let source = null;
- let sourceCandidate = [];
- const offset = 4;
- for (let s of sourceWord) {
- if(s.Flag === flag)
- {
- for (let src of result) {
- if (
- s.Index + s.Keyword.length <= src.index &&
- src.index <= s.Index + s.Keyword.length + offset
- ) {
- let inputSrc = {keyword:src.keyword ,district:src.district ,province:src.province , lat:src.lat ,lon:src.lon ,index:src.index , workindex:s.WorkIndex};
- sourceCandidate.push(inputSrc);
- break;
- }
- }
- }
- }
- if (sourceCandidate.length > 0) {
- source = orderBy(sourceCandidate, ["Index"], ["asc"])[0];
- //source = sourceCandidate.sort(function(a,b){return a.Index - b.Index});
- result = result.filter(res => res.Index != source.Index);
- } else {
- source = result[0];
- result = result.slice(1);
- }
- return source;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement