做了个代码神器,让你敲代码6到起飞, 代码开源给大家【人工智能/自然语言处理】(下)

本文涉及的产品
智能开放搜索 OpenSearch行业算法版,1GB 20LCU 1个月
NLP 自学习平台,3个模型定制额度 1个月
NLP自然语言处理_基础版,每接口每天50万次
简介: 做了个代码神器,让你敲代码6到起飞, 代码开源给大家【人工智能/自然语言处理】(上)https://developer.aliyun.com/article/107649

❓ 六、VARBook是怎么实现的


具体内容请见代码仓库


前端


前端的设计思路是通过Vue的组件化实现的

在这里给大家贴上一些代码

代码中都有相应的注释



components

Header

banner.vue

<template>
  <div class="banner-box"></div>
</template>
<script>
export default {
  name: "banner"
}
</script>
<style scoped>
/*Logo image style*/
.banner-box {
  text-align: center;
  background-image: url("../../assets/images/Banner.svg");
  background-size: 100%;
  background-repeat: no-repeat;
  margin: auto;
}
/*Match the banner style when accessing from the computer*/
@media (min-width: 768px) {
  .banner-box {
    width: 550px;
    height: 130px;
  }
}
/*Match the style of the banner when accessing from the mobile terminal*/
@media (max-width: 768px) {
  .banner-box {
    width: 420px;
    height: 110px
  }
}
@media (max-width: 550px) {
  .banner-box {
    width: 72%;
    height: 90px;
  }
}
@media (max-width: 460px) {
  .banner-box {
    width: 72%;
    height: 73px;
  }
}
/*end*/
</style>


Search

searchBar.vue

<template>
  <el-input id="searchBox" v-model="input_contents" placeholder="You need to translate the variables"
            @keydown.enter="openSearch"
            autofocus>
    <template #prefix>
      <el-icon class="el-input__icon" @click="openSearch">
        <svg class="icon" width="200" height="200" viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"
             data-v-365b8594="">
          <path fill="currentColor"
                d="M795.904 750.72l124.992 124.928a32 32 0 01-45.248 45.248L750.656 795.904a416 416 0 1145.248-45.248zM480 832a352 352 0 100-704 352 352 0 000 704z"></path>
        </svg>
      </el-icon>
    </template>
  </el-input>
</template>
<script>
import {ElMessage} from "element-plus";
export default {
  name: "searchBar",
  data(){
    return{
      // Content retrieved by users
      input_contents:'',
    }
  },
  mounted() {
    // Monitor shortcut keys, focus the input box when control is pressed
    document.onkeydown = function (e) {
      if (e.keyCode == 17){
        document.getElementById("searchBox").focus()
      }
    }
  },
  methods: {
    openSearch(){
      // Determine whether it contains Chinese and English
      let isNull = this.input_contents.replace(/[^^\u4E00-\u9FA5a-zA-Z]/g, '');
      if (isNull) {
        // Start the query and pass the input to the parent component
        this.$emit("startSearch",this.input_contents);
      } else {
        ElMessage({
          showClose: true,
          message: '目前仅支持搜索中/英文',
          type: 'warning',
        })
      }
    }
  }
}
</script>
<style scoped>
/*Modify the default style of el-input*/
.el-input {
  width: 70%;
  max-width: 600px;
  margin-bottom: 10px;
  margin-top: 10px;
}
</style>



searchResult.vue

<template>
  <el-card class="box-card" v-if="isLoading">
    <el-table :data="tableData" style="width: 100%;">
      <el-table-column label="适用参考" width="180">
        <template #default="scope">
          <span :id="'applicable-'+scope.$index">
            {{ scope.row.applicable }}
            </span>
        </template>
      </el-table-column>
      <el-table-column label="变量">
        <template #default="scope">
          <span class="var-style" :id="'var-'+scope.$index" @click="copy(scope.row.var,scope.$index)">
            {{ scope.row.var }}
          </span>
        </template>
      </el-table-column>
    </el-table>
  </el-card>
</template>
<script>
import {ElMessage, ElNotification} from "element-plus";
export default {
  name: "searchResult",
  props: {
    input_contents: String
  },
  data() {
    return {
      tableData: [],
      isLoading: false
    }
  },
  mounted() {
    this.updateData();
  },
  watch: {
    input_contents() {
      this.updateData();
    },
    tableData(){
      this.isLoading = true;
      // Automatically copy the last content
      let isAutoCopy = window.localStorage.getItem("autoCopyId");
      if (isAutoCopy) {
        setTimeout(() =>{
          document.getElementById(isAutoCopy).click();
        },500)
      }
    }
  },
  methods: {
    // copy content
    copy: function (data, index) {
      const input = document.createElement("input");
      input.value = data;
      document.body.appendChild(input);
      input.select();
      document.execCommand("Copy");
      document.body.removeChild(input);
      let applicable_references = document.getElementById("applicable-" + index).innerText;
      ElNotification({
        title: '复制成功',
        message: "适用参考: " + applicable_references + "",
        type: 'success',
        dangerouslyUseHTMLString: true
      })
      window.localStorage.setItem("autoCopyId", "var-" + index);
    },
    updateData() {
      let params = {
        "input": this.input_contents
      }
      this.$api.post("/translation", params).then((res) => {
        if (res.data.code == 200) {
          let ram_tableData = []
          let var_values = res.data.varData.var;
          let applicable = res.data.varData.applicable;
          for (const [index, value] of var_values.entries()) {
            ram_tableData.push({
              "var": value,
              "applicable": applicable[index]
            })
          }
          ram_tableData.push({
            "var": res.data.translate,
            "applicable": "注释"
          })
          this.tableData = ram_tableData;
        } else if (res.data.code == 501) {
          ElMessage({
            showClose: true,
            message: '服务器过载,请稍后重试~',
            type: 'warning',
          })
        } else {
          ElNotification({
            title: '未知错误',
            message: '错误码: ' + res.data.code,
            type: 'warning',
          })
        }
      }).catch((err) => {
        console.log(err)
      })
    },
  },
}
</script>
<style scoped>
.el-card {
  max-width: 655px !important;
  text-align: center !important;
  margin: 50px auto !important;
}
.var-style:hover {
  color: #0663c4;
}
.var-style {
  color: #409eff;
  cursor: pointer
}
</style>
<style>
.el-table .el-table__cell {
  text-align: center !important;
}
</style>



textTips.vue

<template>
  <el-link disabled>tips: 按 Ctrl 键自动对焦搜索框,按 Enter 键自动搜索</el-link>
</template>
<script>
export default {
  name: "textTips"
}
</script>

page

Home.vue

<template>
  <header :class="isContentOnce ? 'reduceTop' : 'increaseTop'">
    <banner></banner>
  </header>
  <section>
    <search-bar @start-search="openSearch"></search-bar>
    <div class="text-tips">
      <text-tips></text-tips>
    </div>
    <search-result v-if="isContentOnce" :input_contents="input"></search-result>
  </section>
</template>
<script>
import banner from "@/components/Header/banner";
import searchBar from "@/components/Search/searchBar";
import textTips from "@/components/Search/textTips";
import SearchResult from "@/components/Search/searchResult";
import {ref} from "vue";
export default {
  components: {
    SearchResult,
    banner, searchBar, textTips
  },
  setup() {
    // If it's the first search, move the head up if it's the first time
    const isContentOnce = ref(false);
    // Get the input value and pass it to the search-result component
    const input = ref('');
    const openSearch = v => {
      // update input
      input.value = v;
      // The first search will be performed and the content of the search-result tag will be displayed
      isContentOnce.value = true;
    }
    return {openSearch, input, isContentOnce}
  },
  data() {
    return {}
  },
}
</script>
<style scoped>
/*Reduce the newline of the header*/
.reduceTop {
  margin-top: 25px;
  transition: all 0.3s ease-in-out;
}
/*Add a new line to the header, as it looks when first opened*/
.increaseTop {
  margin-top: 130px;
  transform: translateY(0px);
}
/*When accessing the mobile terminal, the line wrapping of the header should be reduced, which is more beautiful*/
@media (max-width: 550px) {
  .increaseTop {
    margin-top: 70px;
  }
}
/*section tag content centered*/
section {
  text-align: center;
}
.text-tips {
  text-align: left;
  max-width: 600px;
  margin: auto;
  width: 70%;
}
</style>
<style>
/*global white*/
body {
  color: white;
}
</style>



router

index.js

import {createRouter, createWebHashHistory} from 'vue-router'
import Home from "@/page/Home";
const routes = [
    {
        path: '/',
        name: 'Home',
        component:Home
    },
]
const router = createRouter({
    history:createWebHashHistory(),
    routes
})
export default router

main.js

import {createApp} from 'vue'
import VARBook from './VARBook.vue'
import router from "@/router";
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import axios from "@/plugins/axios";
let varBook = createApp(VARBook);
varBook.use(ElementPlus)
varBook.use(router)
varBook.mount('#varBook')
varBook.config.globalProperties.$api = axios

package.json

{
  "name": "varbook",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "axios": "^0.24.0",
    "core-js": "^3.6.5",
    "element-plus": "^1.3.0-beta.1",
    "vue": "^3.0.0",
    "vue-axios": "^3.4.0",
    "vue-router": "^4.0.12"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/compiler-sfc": "^3.0.0",
    "babel-eslint": "^10.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-vue": "^7.0.0"
  },
  "eslintConfig": {
    "root": true,
    "env": {
      "node": true
    },
    "extends": [
      "plugin:vue/vue3-essential",
      "eslint:recommended"
    ],
    "parserOptions": {
      "parser": "babel-eslint"
    },
    "rules": {}
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ]
}


后端

后端的具体代码就不赘述了,内容多且不好描述,在此给大家贴上一个粗略的流程图,希望能给大家提供一些思路与灵感


13.png




关于开发进度,欢迎大家持续关注我的GitHub仓库



14.gif



欢迎大家使用,VARBook正在逐步完善中…欢迎大家出谋划策

快速访问 👉 https://varbook.uiuing.com/

GitHub仓库👉 https://github.com/uiuing/VARBook

VARBook Release v1.0.0 👉 https://github.com/uiuing/VARBook/releases/tag/v1.0.0


目录
相关文章
|
1月前
|
机器学习/深度学习 人工智能 自然语言处理
【自然语言处理】TF-IDF算法在人工智能方面的应用,附带代码
TF-IDF算法在人工智能领域,特别是自然语言处理(NLP)和信息检索中,被广泛用于特征提取和文本表示。以下是一个使用Python的scikit-learn库实现TF-IDF算法的简单示例,并展示如何将其应用于文本数据。
193 65
|
1月前
|
人工智能 监控 算法
智能时代的伦理困境:AI技术的道德边界探索人工智能在教育领域的革新之路未来编程:人工智能与代码共生的新篇章
【8月更文挑战第21天】在人工智能(AI)技术飞速发展的今天,我们正处在一个前所未有的科技变革时期。随着AI技术的深入人类生活的方方面面,它不仅带来了便利和效率的提升,同时也引发了关于道德和伦理的深刻讨论。本文将探讨AI技术发展中遇到的伦理挑战,以及如何建立合理的道德框架来指导AI的未来应用,确保技术进步与人类社会价值观的和谐共存。
222 61
|
1月前
|
机器学习/深度学习 人工智能 自然语言处理
【人工智能】Foxmail邮箱在人工智能领域的应用方法及代码解析
Foxmail邮箱作为一款流行的邮件客户端软件,主要用于个人和企业的邮件收发、管理等功能。虽然它与人工智能(AI)技术有着潜在的融合点,但直接关于Foxmail邮箱在人工智能方面的应用代码并不是常规的讨论内容,因为邮箱客户端本身并不直接包含复杂的AI算法或代码。
136 58
|
1月前
|
机器学习/深度学习 人工智能 自然语言处理
【自然语言处理】python之人工智能应用篇——文本生成技术
文本生成是指使用自然语言处理技术,基于给定的上下文或主题自动生成人类可读的文本。这种技术可以应用于各种领域,如自动写作、聊天机器人、新闻生成、广告文案创作等。
43 8
|
1月前
|
机器学习/深度学习 人工智能 搜索推荐
【图像生成技术】人工智能在广告营销的革新:图像生成技术的应用与实践代码示例
随着人工智能技术的飞速发展,广告营销行业迎来了前所未有的变革。图像生成技术,作为AI领域的一颗璀璨明星,正被广泛应用于创造个性化、高吸引力的产品展示图、海报乃至宣传视频,以精准对接目标受众,显著提升广告的转化率和整体营销效果。本文将深入探讨这一技术的应用场景,并通过一个简单的代码示例,展示如何利用深度学习框架TensorFlow来实现创意图像的自动生成。
43 4
|
1月前
|
机器学习/深度学习 人工智能 算法
【人工智能】传统语音识别算法概述,应用场景,项目实践及案例分析,附带代码示例
传统语音识别算法是将语音信号转化为文本形式的技术,它主要基于模式识别理论和数学统计学方法。以下是传统语音识别算法的基本概述
46 2
|
1月前
|
机器学习/深度学习 人工智能 自然语言处理
【人工智能】自然语言处理(NLP)的突破,关注NLP在机器翻译、情感分析、聊天机器人等方面的最新研究成果和应用案例。
自然语言处理(NLP)作为人工智能的一个重要分支,近年来取得了显著的突破,特别在机器翻译、情感分析、聊天机器人等领域取得了显著的研究成果和广泛的应用。以下是对这些领域最新研究成果和应用案例的概述,并附带相应的代码实例。
53 1
|
13天前
|
机器学习/深度学习 人工智能 算法
探索人工智能:机器学习的基本原理与Python代码实践
【9月更文挑战第6天】本文深入探讨了人工智能领域中的机器学习技术,旨在通过简明的语言和实际的编码示例,为初学者提供一条清晰的学习路径。文章不仅阐述了机器学习的基本概念、主要算法及其应用场景,还通过Python语言展示了如何实现一个简单的线性回归模型。此外,本文还讨论了机器学习面临的挑战和未来发展趋势,以期激发读者对这一前沿技术的兴趣和思考。
|
12天前
|
机器学习/深度学习 人工智能 自然语言处理
探索人工智能:从基础理论到实践应用
【8月更文挑战第39天】在本文中,我们将深入探讨人工智能(AI)的基本概念、发展历程以及其在现实世界中的应用。我们将首先介绍AI的定义和主要分类,然后回顾其发展历史,最后通过一个实际的代码示例来展示AI的应用。无论你是AI领域的初学者还是有一定基础的学习者,这篇文章都将为你提供有价值的信息和启示。
|
3天前
|
机器学习/深度学习 人工智能 自然语言处理
人工智能在教育中的创新应用:个性化学习的未来
【9月更文挑战第18天】人工智能在教育中的创新应用正在深刻改变着我们的教学方式和学习体验。从个性化学习方案的制定到智能化辅导与反馈,从多元化学习资源的推荐到自动化评分与智能考试系统,AI技术正在为教育领域带来前所未有的变革。面对这一变革,我们需要以开放和批判的态度拥抱它,共同探索AI时代教育的无限可能,为每一个学习者创造更美好的未来。
41 12