4
头图

foreword

Hello everyone I am a forest three hearts, the most easy to understand the words of the hardest knowledge is my motto, is based on the premise advanced is the beginning of my heart, to inform you that a good news : Chinese new year La! ! ! It's New Year's Eve! ! !

image.png

But bad news : It's New Year's Eve, you go to visit relatives, do you know how to call your relatives? Such as your father's brother's brother's son, do you know what to call it? ? ? Hahaha, just kidding. However, it is true that many people do not know how to call their relatives when they go to visit relatives. At this time, programming is reflected. After all, the famous programmer "Worshabian Desky" said that integrated into life. program is a good program

image.png

common practice

practice

In fact, many online practices are doing this: title -> direct relationship -> title , and the data structure used is similar to the following:

{
  "爸爸": {
    "爸爸": "爷爷",
    "妈妈": "奶奶",
    "哥哥": "伯父",
    "弟弟": "叔叔",
    "姐姐": "姑妈",
    "妹妹": "姑妈",
    "丈夫": "未知",
    "妻子": "妈妈",
    "儿子": { "older": "哥哥", "middle": "我", "younger": "弟弟" },
    "女儿": { "older": "姐姐", "middle": "我", "younger": "妹妹" }
  }
}

截屏2022-01-10 上午7.19.48.png

shortcoming

In fact, the above approach has many disadvantages:

  • 1. It is impossible to directly query compound relationships such as "aunt's mother-in-law"

截屏2022-01-10 上午7.19.56.png

  • 2. The title cannot be reversely searched, for example: Is "cousin's mother" "aunt", "aunt", or "aunt"?

截屏2022-01-10 上午7.20.03.png

  • 3. The data structure is bloated, and there are many "unknowns"
    As seen in the data structure above. . . What should "Daddy's husband" be called? ? ? ? "unknown". . .

截屏2022-01-10 上午7.20.10.png

  • 4. Not compatible with multiple titles, such as "Dad" can also be called "Father", "Daddy"

截屏2022-01-10 上午7.20.16.png

  • 5. Inability to reason about the relationship chain, such as: What is the relationship chain between "aunt" and me?

截屏2022-01-10 上午7.20.22.png

Advanced Practice

grammar

First, a set of standards should be formulated, and this set of standards should be used when reasoning about kinship.

relation

letterrelation
ffather
mmother
hhusband
wwife
sson
dFemale
xbbrother
obbrother
lbyounger brother
xssisters
ossister
lssister

modifier

modifiermeaning
1male
0female
&oolder
&lyoung
#cut off
[a\b]tied

data structure

The data structure is a key-value pair like this: direct relation chain -> title set

for example

  • 'h': ['husband','husband','sir','official','man','man','husband','husband','xianggong','husband','lover' ,'mate']
    Because h represents husband, and husband has the above titles
  • 'h,f': ['Grandpa','Wengqin','Old man']
    Because h,f represents the father husband, there are several titles above
  • '[h,f|h,m]':['in-law']
    Because [h,f|h,m] means husband's father and husband's mother, that is in-laws
  • '[f,xb,s&o|f,xb,s&l]':['cousins']
    Because [f,xb,s&o|f,xb,s&l] means father's brother's older son and father's brother's younger son, that's cousins

In this way, when querying a relationship, only the direct relationship chain needs to be calculated, rather than a dictionary lookup for the predicate

start

ideas

for example

  • 1. When the user enters "aunt's mother-in-law", it can be decomposed into "aunt" and "mother-in-law" (the former mother-in-law)
  • 2. The decomposed "aunt" and "mother-in-law", the relationship chain is m,xb,w h,m respectively, and the combination is m,xb,w,h,m
  • w,h is redundancy in the merged relationship chain. 061e4b18bea445 is the husband wife, that is, himself, which can be simplified to m,xb,m , and xb,m is the mother of the brother, which is also his own mother, so it is finally simplified to m,m
  • 4. The simplified relationship chain is a direct relationship chain, and the query can be matched key-value pair

截屏2022-01-10 上午7.20.29.png

accomplish

  • 1. key-value pair, JSON format, fast query speed in key-value pair form
  • 2. Relationship chain simplification Use the regular expression to simplify two levels of relationships at a time until it cannot be simplified, but the simplification process needs to consider gender, such as: "son's mother", when you are male, it represents your wife, when you are Represent yourself when you are a woman. These all need to regular expression and replaced, which is why the modifier is needed

relationship.js

How to use

Install

npm install relationship.js

use

const relationship = require("relationship.js")

// 自定义模式
// 【关系链】f:父,m:母,h:夫,w:妻,s:子,d:女,xb:兄弟,ob:兄,lb:弟,xs:姐妹,os:姐,ls:妹
// 【修饰符】 1:男性,0:女性,&o:年长,&l:年幼,#:隔断,[a|b]:并列
relationship.setMode('northern',{
    'm,f': ['姥爷'],
    'm,m': ['姥姥'],
    'm,xb,s&o': ['表哥'],
    'm,xb,s&l': ['表弟'],
})

var options = {
    text: '',        //输入文本(称谓的汉字表达,称谓间用‘的’字分隔)
    target: '',            //针对对象:空表示自己
    sex: 1,            //自己的性别:0女性,1男性
    type: 'default',    //转换类型:'default'计算称谓,'chain'计算关系
    reverse: false,        //称呼方式:true对方称呼我,false我称呼对方
    mode: 'default'        //模式选择,可由用户自定义
};


// 如:我应该叫外婆的哥哥什么?
console.log(relationship({ text: '妈妈的妈妈的哥哥' })) // [ '外舅公' ]

// 如:七舅姥爷应该叫我什么?
console.log(relationship({ text: '七舅姥爷', reverse: true, sex: 1 })) // [ '甥外孙' ]

// 如:舅公和我是什么关系?
console.log(relationship({ text: '舅公', sex: 0, type: 'chain' }))
// [ '爸爸的妈妈的兄弟', '妈妈的妈妈的兄弟', '老公的妈妈的兄弟' ]

// 如:舅妈如何称呼外婆?
console.log(relationship({ text: '外婆', target: '舅妈', sex: 1 })) // [ '婆婆' ]

Kinship Calculator - Simple Version

With input、select、button you can easily implement relative relationship calculator - simple version

简易版亲戚.gif

    // template
    <div class="m-l-10 m-t-20">
      <input v-model="options.text" />
      <input v-model="options.target" />
      <br />
      <select v-model="options.sex">
        <option :value="1">男</option>
        <option :value="0">女</option>
      </select>
      <select v-model="options.type">
        <option value="default">称谓</option>
        <option value="chain">关系</option>
      </select>
      <select v-model="options.reverse">
        <option :value="false">正向</option>
        <option :value="true">反向</option>
      </select>
      <br />
      <button @click="computedRelationship">计算</button>
      <span>{{result}}</span>
    </div>
    
    // data
    options: {
        text: "",
        target: "",
        sex: 1,
        type: "default",
        reverse: false,
        mode: "default",
      },
    result: ''
    
    import relationship from './relationship'
    // methods
    computedRelationship() {
      console.log(this.options)
      this.result = relationship(this.options)
    },

Kinship Calculator - Complex Version

Calculator

Reference article

Epilogue

I'm Lin Sanxin, an enthusiastic front-end rookie programmer. If you are motivated, like the front-end, and want to learn the front-end, then we can make friends and fish together haha, touch the fish group, add me, please note [Si No]

image.png


Sunshine_Lin
2.1k 声望7.1k 粉丝