PK creative Spring Festival, I am participating in the “Spring Festival creative submission contest”, please see: Spring Festival creative submission Contest

preface

Hello everyone, I am Lin Sanxin, with the most easy to understand the most difficult knowledge point is my motto, the basis is advanced premise is my beginner’s mind, inform everyone a good news: fast New Year!! New Year’s day is coming!!

But I have a bad news for you: It’s the Spring Festival and you go to visit your relatives. Do you know how to address them? Like your dad’s brother’s brother’s son, do you know what to call it?? Ha ha ha, just kidding. However, it is true that many people do not know how to call their relatives when they go to visit them. At this time, the charm of programming is reflected. After all, the famous programmer “Woxia Desky” said that programming is good programming integrated into life

Common practices

practice

Now in fact, many online practices are doing so: appellation -> direct relationship -> appellation, and the data structure used is similar to the following:

{
  "Dad": {
    "Dad": "Grandpa"."Mother": "Grandma"."Brother": "Uncle"."Brother": "Uncle"."Sister": "Aunt"."Sister": "Aunt"."Husband": "Unknown"."Wife": "Mother"."Son": { "older": "Brother"."middle": "我"."younger": "Brother" },
    "Daughter": { "older": "Sister"."middle": "我"."younger": "Sister"}}}Copy the code

disadvantages

In fact, the above approach, there are many disadvantages:

  • 1, can not directly query such as “aunt’s mother-in-law” compound relationship

  • 2, the appellation can not reverse query, such as: “cousin’s mother” is “aunt”, “aunt”, or “aunt”?

  • 3. Bloated data structure and many “unknowns”

As the data structure above can see… What should papa’s husband be called ????? Unknown “…

  • 4, not compatible with a variety of names, such as “dad” can also be called “father”, “daddy”

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

Advanced practice

grammar

First lay down a set of criteria by which all reasoning about kinship should be based

Relationship between

The letter Relationship between
f The father
m The mother
h Husband,
w His wife
s The child
d female
xb brother
ob brother
lb brother
xs sister
os The elder sister
ls The younger sister

The modifier

The modifier meaning
1 men
0 women
&o older
&l young
# partition
[a|b] Tied for

The data structure

The data structure is a key-value pair: a chain of direct relationships -> a collection of terms

For example

  • ‘h’ : [‘ husband ‘, ‘husband’, ‘Sir,’ officer person ‘, ‘man’, ‘the man’, ‘, ‘, ‘husband’, ‘” xianggong “‘, ‘husband’, ‘love’, ‘his wife]

Because H stands for husband, and husband has several titles above

  • ‘h,f’ : [‘ father-in-law ‘,’ weng Qin ‘,’ grandpa ‘]

Because H and F represent the father of her husband, there are several appellations above

  • ‘[h, f | h, m]’ : [‘ in-laws’]

Because [h, f | h, m] means the husband’s father and husband’s mother, that’s in-laws

  • ‘[f, xb, s&o | f, xb, s&l]’ : [‘ cousin ‘]

Because [f, xb, s&o | f, xb, s&l] mean father brother elder son and dad’s brother’s young son, and that’s cousin

In this way, when querying the relation, only the calculation of the direct relation chain is good, rather than the dictionary lookup of the appellation

Open to

Train of thought

For example

  • 1. When users input “Auntie’s mother-in-law”, it can be divided into “auntie” and “mother-in-law” (the former’s mother-in-law).
  • 2, decomposed into “aunt” and “mother-in-law”, the relationship chain is respectivelym,xb,wandh,mTogether, it ism,xb,w,h,m
  • 3. There is redundancy in the merged relationship chain.w,hisWife's husband, self, can be reduced tom,xb,mAnd,xb,misBrother's motherThat’s your mother, and that simplifies tom,m
  • 4. The simplified chain of relationships is oneChain of direct relation, can be accessed throughKey/value pairTo match the query

implementation

  • 1.Key/value pair, the use ofJSONSave format, key-value pair form query speed
  • 2. Simplified use of relationship chainRegular expressionSimplify two relationships at a time until it can’t be simplified, but the simplification process takes into account gender, for example: “son’s mother” represents your wife when you are male and yourself when you are female. These all need to be usedRegular expressionMatch it and replace it, that’s why you need it, rightThe modifierThe reason why

relationship.js

use

The installation

npm install relationship.js
Copy the code

use

const relationship = require("relationship.js")

// Custom schema
/ / f relationship chain 】 【 : father, m: mother, h:, w: wife, s: son, d: female, xb: brother, ob: brother, lb: younger brother, xs: sister, OS: elder sister, ls: younger sister
/ / modifier 】 【 1: male, 0: women, & o: older, & l: young, # : partition, (a | b) :
relationship.setMode('northern', {'m,f': ['my grandfather'].'m,m': ['grandma'].'m,xb,s&o': ['cousin'].'m,xb,s&l': ['cousin'],})var options = {
	text: ' '.// Enter the text (Chinese characters for appellations, separated by the word 'd')
	target: ' '.// For objects: empty represents itself
	sex: 1.// Own gender: 0 female,1 male
	type: 'default'.// Conversion type: 'default' calculates appellation,'chain' calculates relationship
	reverse: false.// Address: true address me,false address me
	mode: 'default'		// Mode selection can be customized by users
};


// What should I call Grandma's brother?
console.log(relationship({ text: 'Mama's Mama's brother' })) // [' uncle ']

// What should uncle Seven call me?
console.log(relationship({ text: 'Seven great uncles'.reverse: true.sex: 1 })) // [' nephew ']

// What is the relation between uncle and me?
console.log(relationship({ text: 'great-uncle'.sex: 0.type: 'chain' }))
// [' father's mother's brother ', 'mother's mother's brother ',' husband's mother's brother ']

// What does aunt call grandma?
console.log(relationship({ text: 'grandma'.target: 'aunt'.sex: 1 })) // [' mother-in-law ']
Copy the code

Kinship Calculator – Easy version

With input, select, button you can easily implement the relative calculator – simple version

    // 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">male</option>
        <option :value="0">female</option>
      </select>
      <select v-model="options.type">
        <option value="default">appellation</option>
        <option value="chain">Relationship between</option>
      </select>
      <select v-model="options.reverse">
        <option :value="false">positive</option>
        <option :value="true">reverse</option>
      </select>
      <br />
      <button @click="computedRelationship">To calculate</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)
    },
Copy the code

Kinship Calculator – Complex edition

Kinship calculator

Refer to the article

  • “Relative calculator” algorithm implementation
  • The Github address of relationship.js

conclusion

I am Lin Sanxin, an enthusiastic front-end novice programmer. If you progress, like the front end, want to learn the front end, then we can make friends, touch fish ha ha, touch fish, point this –> touch fish boiling point