├── .gitignore ├── META-INF └── MANIFEST.MF ├── README.md ├── docs └── 接口调用说明.md ├── pom.xml └── src ├── main ├── java │ └── com │ │ └── qdcz │ │ ├── App.java │ │ ├── chat │ │ ├── allchat │ │ │ └── AllChatQA.java │ │ ├── cmbchat │ │ │ └── CMBQA.java │ │ ├── controller │ │ │ └── ChatController.java │ │ ├── entity │ │ │ └── RequestParameter.java │ │ ├── interfaces │ │ │ └── ChatQA.java │ │ ├── service │ │ │ ├── QuestionPaserService.java │ │ │ └── SmartQAService.java │ │ ├── socialchat │ │ │ └── SocialQA.java │ │ └── tools │ │ │ ├── Levenshtein.java │ │ │ ├── MyComparetor.java │ │ │ └── MyComparetorSJ.java │ │ ├── common │ │ ├── CommonTool.java │ │ ├── ConceptRuler.java │ │ └── XMLUtil.java │ │ ├── conf │ │ ├── DatabaseConfiguration.java │ │ └── LoadConfigListener.java │ │ ├── entity │ │ ├── Edge.java │ │ ├── Graph.java │ │ ├── GraphDatabase.java │ │ ├── IGraphEntity.java │ │ ├── Project.java │ │ └── Vertex.java │ │ ├── graph │ │ ├── controller │ │ │ └── GraphControler.java │ │ ├── interfaces │ │ │ ├── IGraphBuzi.java │ │ │ └── IGraphDAO.java │ │ ├── neo4jcypher │ │ │ ├── conf │ │ │ │ └── Neo4jConfiguration.java │ │ │ ├── connect │ │ │ │ └── Neo4jClientFactory.java │ │ │ ├── dao │ │ │ │ └── Neo4jCYDAO.java │ │ │ └── service │ │ │ │ ├── AnalysisListedCoService.java │ │ │ │ └── Neo4jCYService.java │ │ ├── service │ │ │ └── GraphOperateService.java │ │ └── tools │ │ │ └── ResultBuilder.java │ │ ├── index │ │ ├── elsearch │ │ │ ├── conf │ │ │ │ └── ELKConfig.java │ │ │ ├── dao │ │ │ │ └── ElasearchDAO.java │ │ │ ├── elk │ │ │ │ └── ElasearchClientFactory.java │ │ │ └── service │ │ │ │ └── ElasearchService.java │ │ └── interfaces │ │ │ ├── IIndexDAO.java │ │ │ └── IIndexService.java │ │ └── mongo │ │ ├── BaseMongoDAL.java │ │ ├── ConnManager.java │ │ ├── MyMongo.java │ │ └── conf │ │ └── MongoConfiguration.java └── resources │ ├── application-dev.properties │ ├── application-product.properties │ ├── application.properties │ ├── concept_rule_simple.txt │ ├── dev │ ├── database.xml │ ├── elasearch.properties │ ├── log4j2.xml │ ├── mongo.properties │ └── neo4j.properties │ ├── product │ ├── database.xml │ ├── elasearch.properties │ ├── log4j2.xml │ ├── mongo.properties │ └── neo4j.properties │ ├── sougou.dic │ └── stopword.dic └── test └── test └── com └── qdcz ├── conf └── TestLoadConfigListener.java ├── graph └── neo4jcypher │ └── service │ └── TestNeo4jCYService.java └── other ├── GetLawJson.java ├── GetVertexesEdges.java ├── ImportData.java ├── JustTest.java ├── NumStatistLaws.java ├── StuctData ├── StructCheckCon.java ├── StructListedCom.java ├── StructNewPaper.java ├── StructPaper.java ├── StuctBSData.java └── cutFile.java ├── hanlp └── DemoDependencyParser.java ├── parse ├── BytesToHexString.java ├── CreateParse.java └── GetDocumentInfo.java ├── testMongo.java ├── testRead.java └── wiki ├── CheckError.java └── StructWikiData.java /.gitignore: -------------------------------------------------------------------------------- 1 | # general 2 | hs_err_pid* 3 | 4 | # Idea 5 | .idea/ 6 | *.iml 7 | 8 | # Eclipse 9 | .project 10 | .classpath 11 | .settings/ 12 | 13 | # maven 14 | target/ 15 | pom.xml.tag 16 | pom.xml.releaseBackup 17 | pom.xml.versionsBackup 18 | pom.xml.next 19 | release.properties 20 | dependency-reduced-pom.xml 21 | buildNumber.properties 22 | .mvn/timing.properties 23 | 24 | # java 25 | *.jar 26 | *.war 27 | *.ear 28 | # Package Files # 29 | *.jar 30 | *.war 31 | *.ear 32 | *.zip 33 | *.tar.gz 34 | *.rar 35 | -------------------------------------------------------------------------------- /META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Class-Path: javax.mail-1.5.6.jar x-pack-api-5.4.0.jar netty-3.10.6.Fin 3 | al.jar hibernate-validator-5.3.5.Final.jar lucene-join-6.5.0.jar luce 4 | ne-spatial-extras-6.5.0.jar spring-test-4.2.7.RELEASE.jar slf4j-api-1 5 | .7.25.jar netty-codec-4.1.9.Final.jar log4j-api-2.7.jar lucene-backwa 6 | rd-codecs-6.5.0.jar jopt-simple-5.0.2.jar spring-boot-autoconfigure-1 7 | .5.6.RELEASE.jar lucene-spatial3d-6.5.0.jar log4j-slf4j-impl-2.7.jar 8 | commons-logging-1.1.3.jar percolator-client-5.4.0.jar netty-transport 9 | -4.1.9.Final.jar x-pack-transport-5.4.0.jar jackson-annotations-2.8.0 10 | .jar transport-netty3-client-5.4.0.jar bcprov-jdk15on-1.55.jar classm 11 | ate-1.3.3.jar lucene-analyzers-common-6.5.0.jar netty-handler-4.1.9.F 12 | inal.jar unboundid-ldapsdk-3.2.1.jar tomcat-embed-websocket-8.5.16.ja 13 | r lang-mustache-client-5.4.0.jar hanlp-1.2.9-SNAPSHOT.jar elasticsear 14 | ch-5.4.0.jar activation-1.1.jar spring-core-4.3.10.RELEASE.jar jackso 15 | n-core-2.8.9.jar lucene-spatial-6.5.0.jar snakeyaml-1.17.jar spring-e 16 | xpression-4.3.10.RELEASE.jar spring-beans-4.3.10.RELEASE.jar rest-5.4 17 | .0.jar jcl-over-slf4j-1.7.25.jar httpclient-4.5.3.jar lucene-suggest- 18 | 6.5.0.jar netty-buffer-4.1.9.Final.jar commons-codec-1.10.jar lombok- 19 | 1.16.2.jar spring-boot-starter-log4j2-1.5.6.RELEASE.jar httpcore-4.4. 20 | 6.jar transport-5.4.0.jar netty-codec-http-4.1.9.Final.jar netty-reso 21 | lver-4.1.9.Final.jar lucene-grouping-6.5.0.jar bcpkix-jdk15on-1.55.ja 22 | r joda-time-2.9.9.jar lucene-memory-6.5.0.jar neo4j-java-driver-1.3.0 23 | .jar tomcat-embed-el-8.5.16.jar lucene-queries-6.5.0.jar owasp-java-h 24 | tml-sanitizer-r239.jar json-20140107.jar log4j-core-2.7.jar securesm- 25 | 1.1.jar spring-boot-starter-web-1.5.6.RELEASE.jar netty-common-4.1.9. 26 | Final.jar spring-boot-starter-1.5.6.RELEASE.jar t-digest-3.0.jar comm 27 | ons-io-2.5.jar httpasyncclient-4.1.3.jar jna-4.4.0.jar tomcat-embed-c 28 | ore-8.5.16.jar jackson-dataformat-yaml-2.8.9.jar lucene-core-6.5.0.ja 29 | r reindex-client-5.4.0.jar junit-4.12.jar lucene-misc-6.5.0.jar mongo 30 | -java-driver-3.4.2.jar spring-aop-4.3.10.RELEASE.jar hamcrest-core-1. 31 | 3.jar jboss-logging-3.3.1.Final.jar transport-netty4-client-5.4.0.jar 32 | sniffer-5.4.0.jar compiler-0.9.3.jar spring-web-4.3.10.RELEASE.jar h 33 | ttpcore-nio-4.4.5.jar jackson-dataformat-smile-2.8.9.jar spring-boot- 34 | starter-tomcat-1.5.6.RELEASE.jar hppc-0.7.1.jar guava-19.0.jar super- 35 | csv-2.4.0.jar delete-by-query-2.4.5.jar jackson-dataformat-cbor-2.8.9 36 | .jar spring-webmvc-4.3.10.RELEASE.jar lucene-queryparser-6.5.0.jar lu 37 | cene-highlighter-6.5.0.jar validation-api-1.1.0.Final.jar spring-cont 38 | ext-4.3.10.RELEASE.jar spring-boot-1.5.6.RELEASE.jar jul-to-slf4j-1.7 39 | .25.jar jackson-databind-2.8.9.jar HdrHistogram-2.1.9.jar lucene-sand 40 | box-6.5.0.jar 41 | Main-Class: 42 | 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # **知识图谱管理与智能问答引擎 v2.0** 2 | 3 | # **项目描述** 4 | 5 | - **介绍**:本项目用于操作neo4j数据库,elasticSearch以及与前端交互的服务器app。 6 | - **用途**:对3.2.0版本的neo4j数据库进行增删改查外加索引的操作 7 | - **特性**: 8 | 1.neo4j自带的服务端对中文建立的内置索引分词粒度以字为单位,不太符合实际需求,而且cypher语法没有对分词器的选择操作语句,修改采用elastic做中间业务处理索引内容对接图数据。 9 | 2.本代码选用hanlp分词器做索引分词底层,可扩展词典修改分词粒度。 10 | 11 | # **调用接口文档** 12 | 描述各个功能接口调用方法、参数及响应值,详见:[接口调用文档](docs/接口调用说明.md "接口调用文档") 13 | 14 | 15 | # **版本更新** 16 | 17 | ### 2017-08-28 18 | 19 | 1. 重构第一个版本(v2.0) 20 | 2. 支持多类型图谱检索、管理(由project参数指定,暂时支持银团贷款业务知识图谱、社科知识图谱、金融概念知识图谱) 21 | 3. 图谱查询支持返回节点类型(供前端可视化时筛选) 22 | 23 | ### 2017-08-02 24 | 25 | 1. 支持银行、金融、票据、证券、法律等行业的名词概念问答 26 | 27 | ### 2017-07-20 28 | 29 | 1. 初始版本 30 | 2. 支持银团贷业务指引条款相关内容问答 31 | 3. 针对无法回答的问题,引入图灵机器人 32 | 33 | ------------ 34 | 35 | 编译、运行环境说明: 36 | ----------------------------------- 37 | 使用JDK1.8.0版本,具体依赖都在pom中,本地nexus没有所需依赖。 38 | 39 | 总体目录 40 | ----------------------------------- 41 | + main 42 | + java 43 | + com 44 | + qdcz 45 | * chat 46 | * common 47 | * graph 48 | * index 49 | * mongo 50 | + APP 51 | + resources 52 | + test 53 | 54 | ------------------- 55 | **chat**包含智能问答接口与逻辑处理
56 | **common**中包含一些公用静态方法
57 | **graph**neo4j图操作及接口
58 | **index**为elasticSearch建立索引的接口与方法
59 | **sdn**为图边节点的实例化(entity)以及对应所需要的知识库cypher(epository)。
60 | **service**为结合实际所需的服务逻辑编写,分为低中高三层,上层依赖下层。
61 | **Tools**为定义的工具类。
62 | **APP为** `SpringbootSdnEmbeddedApplication`,启动服务主入口。
63 | **resources**为配置文件
64 | 其中`IKAnalyzer.cfg.xml`为IK分词的扩展配置,加载了2个扩展词典`sougou`、 `stopword`、`银团指引词典`
65 | `neo4j.properties`设置了app启动的驱动,访问的数据库的位置,对外端口
66 | `mongo.properties`设置了mongo-driver所需的信息
67 | `hanlp.properties`设置了han分词器加载词典位置
68 | **test**下为各类单元测试时使用的测试。
69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /docs/接口调用说明.md: -------------------------------------------------------------------------------- 1 | # **智能问答接口** 2 | 3 | ![](https://img.shields.io/badge/version-v2.0-yellow.svg)![](https://img.shields.io/badge/neo4j-v3.1.1-519dd9.svg) 4 | 5 | ### **接口描述** 6 | - 该接口对知识库的相关知识提问做相应回答。 7 | 8 | ### **请求URL** 9 | - ` h134:14000/askFromWeChat?question=银团贷款业务借款人需要签署什么?&project=xxxxx ` 10 | 11 | ### **请求方式** 12 | - POST/GET 13 | 14 | ### **Header** 15 | 暂无特殊要求 16 | 17 | ### **参数** 18 | 19 | |参数名|必选|类型|说明| 20 | |:---- |:---|:----- |----- | 21 | |question | 是 | string |  对于相关知识点提出的问题  | 22 | |project | 是 | string |  所在工程库 目前有xz(小招) sk(社科) all(所有库 ,此部分未完成,预留接口)  | 23 | 24 | ### **参数示例** 25 | ``` 26 | question=银团贷款业务借款人需要签署什么?&project=xz 27 | ``` 28 | 29 | ### **返回示例** 30 | 31 | ``` 32 | { 33 | "title":"银团贷款", 34 | "type":"Graph", 35 | "data":"是由获准经营贷款业务的一家或数家银行牵头,多家银行与非银行金融机构参加而组成的银行集团(Banking Group)采用同一贷款协议,按商定的期限和条件向同一借款人提供融资的贷款方式。国际银团是由不同国家的多家银行组成的银行集团。" 36 | } 37 | ``` 38 | 说明:
39 | type为所有调用的返回类型,有**Graph,Turing,law**
40 | title为type是**Law**时候才会有的字段
41 | data为整理返回的展示结果集
42 | 43 | 44 | ------ 45 |
46 | # **知识库操作平台接口** 47 | 48 | ### **接口描述** 49 | - 该接口主要是为相关专业人士能够直接操作图谱与前台对接的增删改查的API。 50 | - 根据相应功能传入参入进行不同深度的DFS或BFS将相关结果处理后返回给前台。 51 | 52 | ### **请求URL** 53 | - ` http://h134:14000/graphOp ` 54 | 55 | ### **请求方式** 56 | - POST/GET 57 | 58 | ### **Header** 59 | 暂无特殊要求 60 | 61 | ### **参数** 62 | 请求参数为一个整体的Json请求体,整个Json中需包含以下字段参数
63 | 64 | |参数名|必选|类型|说明| 65 | |:---- |:---|:----- |----- | 66 | |type | 是 | string |  对图谱操作调用类型,包括
checkByName(通过名称查询)
checkByIndex(通过索引查询)
checkById(通过节点id查询)
addNode(新增节点)
deleteNode(删除节点)
changeNode(修改节点)
addEdge(新增边)
changeEdge(修改边)
deleteEdge(删除边)
addNodeEdge(新增边和终点)
queryNodeDetail(查看点的详情)
queryEdgeDetail(查看边的详情)
getEdgeAllName(查询边的所有名称) | 67 | |project | 是 | string | 所用的知识库图谱 目前有
ytdkyw(银团贷款业务)
shkx(社会科学知识库)
licom(上市公司分析)
down_2014(2014上市公司担保分析) | 68 | |info.node | 否 | Json |  点相关信息,请求type为点相关则必须填写内部相关信息 | 69 | |info.edge | 否 | Json |  边相关信息,请求type为边相关则必须填写内部相关信息 | 70 | |info.layer | 是 | string |  搜索的深度数量 | 71 | |info.node.name | 否 | string |  节点名称 | 72 | |info.node.id | 否 | string |  删改查时候才会用到,增时为系统自动生成 | 73 | |info.node.root | 否 | string |  节点所在子图 | 74 | |info.node.type | 否 | string |  节点类型 | 75 | |info.node.label | 否 | string |  节点所在的标签 | 76 | |info.node.content | 否 | string |  节点内容,为Json转成的string | 77 | |info.edge.id | 否 | string |  边的图上id,系统自动生成 | 78 | |info.edge.from | 否 | string |  边的起始点id | 79 | |info.edge.to | 否 | string |  边的终点id | 80 | |info.edge.relationship | 否 | string |  边所在的关系标签 | 81 | |info.edge.name | 否 | string |  边的名称 | 82 | 83 | ### **参数示例** 84 | - data= ` json `字符串 85 | - json字符串示例如下: 86 | 87 | ``` 88 | {"type":"deleteEdge", 89 | "project":"", 90 | "info":{ 91 | "node":{ 92 | "name":"", 93 | "id":"", 94 | "root":"", 95 | "type":"", 96 | "label":"edge", 97 | "content":"" 98 | }, 99 | "edge":{ 100 | "id":"", 101 | "from":"", 102 | "to":"", 103 | "relationship":"", 104 | "name":"" 105 | }, 106 | "layer":"2" 107 | } 108 | } 109 | ``` 110 | 说明:
111 | 请求参数名为` data `,后面为一个完整的**json**的` String `类型 112 | 113 | ### **返回示例** 114 | result: 115 | ``` 116 | { 117 | "nodeTypeName":[ 118 | { 119 | "type_cn":"描述", 120 | "type_en":"desc" 121 | }, 122 | { 123 | "type_cn":"别名", 124 | "type_en":"aliases" 125 | }, 126 | { 127 | "type_cn":"公司", 128 | "type_en":"com" 129 | } 130 | ], 131 | "nodes":[ 132 | { 133 | "identity":"46bf30c1-14fc-4bbe-8727-ac39a4b65a72", 134 | "root":"wiki数据", 135 | "name":"地球", 136 | "name_length":2, 137 | "id":"5443432", 138 | "label":"wikidata_label", 139 | "type":"com", 140 | "content":"" 141 | }, 142 | { 143 | "identity":"b245c7b2-6946-4a45-bf8c-4292299d9c17", 144 | "root":"wiki数据", 145 | "name":"Sol d", 146 | "name_length":5, 147 | "id":"5443444", 148 | "label":"wikidata_label", 149 | "type":"aliases", 150 | "content":"" 151 | } 152 | ], 153 | "center":"5443432", 154 | "edges":[ 155 | { 156 | "root":"wiki数据", 157 | "name":"aliases", 158 | "from":"5443432", 159 | "name_length":7, 160 | "to":"5443442", 161 | "id":"534328", 162 | "relationship":"wikidata_relationship" 163 | }, 164 | { 165 | "root":"wiki数据", 166 | "name":"aliases", 167 | "from":"5443432", 168 | "name_length":7, 169 | "to":"5443437", 170 | "id":"534323", 171 | "relationship":"wikidata_relationship" 172 | } 173 | ] 174 | } 175 | 176 | 177 | ``` 178 | 说明:
179 | 180 | |字段名|类型|说明| 181 | |:---- |:----- |----- | 182 | |nodeTypeName | JsonArray |  结果集合中点的类型集合 | 183 | |nodes | JsonArray |  结果集中所有的点信息 | 184 | |edges | JsonArray |  结果集中所有的边信息 | 185 | |center | String |  结果集子图的中心点的id | 186 | |nodeTypeName.type_cn | String |  节点类型中文描述 | 187 | |nodeTypeName.type_en | String |  节点类型英文描述 | 188 | |identity | String |  外部定义的唯一表示,用于插入时做标示使用 | 189 | |root | String |  所在子项目的名称,主要只是要求任务的名称 | 190 | |name | String |  具体名称 | 191 | |name_length | String |  名称长度 | 192 | |id | String |  neo4j产生的内部唯一标示 | 193 | |label | String |  节点所在标签 | 194 | |content | String |  自身其他信息,但是不用于业务中 | 195 | |from | String |  边起始id | 196 | |to | String |  边终点id | 197 | |relationship | String |  边所在的关系标签 | 198 | 199 | 200 | ------ 201 |
202 | 203 | 204 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | com.qdcz 7 | KBQA 8 | 1.0-SNAPSHOT 9 | 10 | 11 | org.springframework.boot 12 | spring-boot-starter-parent 13 | 1.5.6.RELEASE 14 | 15 | 16 | 17 | 4.0.0 18 | 19 | UTF-8 20 | 1.8 21 | 1.8 22 | 1.0.0 23 | 1.16.2 24 | 4.1.2.RELEASE 25 | 19.0 26 | 2.0.3 27 | 3.2.2 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | elasticsearch-releases 37 | https://artifacts.elastic.co/maven 38 | 39 | true 40 | 41 | 42 | false 43 | 44 | 45 | 46 | 47 | 48 | 49 | org.json 50 | json 51 | 20140107 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-starter-test 56 | 1.5.6.RELEASE 57 | test 58 | 59 | 60 | com.vaadin.external.google 61 | android-json 62 | 63 | 64 | 65 | 66 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | org.elasticsearch 79 | elasticsearch 80 | 5.4.0 81 | 82 | 83 | org.elasticsearch.client 84 | x-pack-transport 85 | 5.4.0 86 | 87 | 88 | org.elasticsearch.plugin 89 | delete-by-query 90 | 2.4.5 91 | 92 | 93 | 94 | 95 | 96 | org.springframework.boot 97 | spring-boot-starter-web 98 | 99 | 100 | org.springframework.boot 101 | spring-boot-starter-logging 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | org.springframework.boot 111 | spring-boot-starter-log4j2 112 | 1.5.6.RELEASE 113 | 114 | 115 | 116 | 117 | org.apache.poi 118 | poi 119 | 3.8 120 | test 121 | 122 | 123 | 124 | org.apache.poi 125 | poi-ooxml 126 | 3.8 127 | test 128 | 129 | 130 | 131 | org.apache.poi 132 | poi-scratchpad 133 | 3.8 134 | test 135 | 136 | 137 | 138 | commons-io 139 | commons-io 140 | 2.5 141 | 142 | 143 | 144 | 145 | pdfbox 146 | pdfbox 147 | 0.7.3 148 | test 149 | 150 | 151 | 152 | 157 | 158 | 159 | 160 | com.hankcs 161 | hanlp 162 | 1.2.9-SNAPSHOT 163 | 164 | 165 | 166 | 167 | 168 | org.mongodb 169 | mongo-java-driver 170 | 3.4.2 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | org.neo4j.driver 183 | neo4j-java-driver 184 | 1.3.0 185 | 186 | 187 | 188 | 189 | org.projectlombok 190 | lombok 191 | ${lombok.version} 192 | 193 | 194 | com.google.guava 195 | guava 196 | ${guava.version} 197 | 198 | 199 | 200 | net.sourceforge.jexcelapi 201 | jxl 202 | 2.6 203 | 204 | 205 | 206 | org.apache.commons 207 | commons-lang3 208 | 3.6 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | org.apache.maven.plugins 223 | maven-surefire-plugin 224 | 2.18.1 225 | 226 | true 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | org.apache.maven.plugins 236 | 237 | maven-jar-plugin 238 | 239 | target/classes/ 240 | 241 | 242 | 243 | com.qdcz.App 244 | 245 | false 246 | true 247 | lib/ 248 | 249 | 250 | . 251 | 252 | 253 | 254 | 255 | 256 | org.apache.maven.plugins 257 | maven-dependency-plugin 258 | 259 | 260 | copy-dependencies 261 | prepare-package 262 | 263 | copy-dependencies 264 | 265 | 266 | ${project.build.directory}/lib 267 | compile 268 | false 269 | false 270 | true 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | src/main/resources 280 | 281 | **/* 282 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/App.java: -------------------------------------------------------------------------------- 1 | package com.qdcz; 2 | 3 | import com.hankcs.hanlp.utility.Predefine; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; 7 | import org.springframework.boot.web.servlet.ServletComponentScan; 8 | 9 | /** 10 | *Created by hadoop on 17-6-22. 11 | * APP main 12 | */ 13 | @ServletComponentScan 14 | @SpringBootApplication(exclude = {MongoAutoConfiguration.class}) 15 | public class App { 16 | public static void main(String[] args){ 17 | SpringApplication.run(App.class, args); 18 | } 19 | } 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/chat/allchat/AllChatQA.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.chat.allchat; 2 | 3 | import com.qdcz.chat.entity.RequestParameter; 4 | import com.qdcz.chat.interfaces.ChatQA; 5 | import org.neo4j.driver.v1.types.Path; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.Set; 11 | 12 | /** 13 | * Created by star on 17-8-11. 14 | */ 15 | @Service 16 | public class AllChatQA extends ChatQA { 17 | @Override 18 | public Set MatchPath(List> maps, RequestParameter requestParameter) { 19 | return null; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/chat/cmbchat/CMBQA.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.chat.cmbchat; 2 | 3 | import com.qdcz.chat.entity.RequestParameter; 4 | import com.qdcz.chat.interfaces.ChatQA; 5 | import com.qdcz.chat.tools.Levenshtein; 6 | import org.neo4j.driver.v1.types.Path; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.*; 10 | 11 | /** 12 | * Created by star on 17-8-11. 13 | */ 14 | @Service 15 | public class CMBQA extends ChatQA { 16 | 17 | @Override 18 | public Set MatchPath(List> maps, RequestParameter requestParameter) { 19 | Set result= null; 20 | if(maps.size()==2){ 21 | result = questionPaserService.traversePathBynode(requestParameter,maps); 22 | } 23 | else if(maps.size()>2){ 24 | float max=0; 25 | float maxScore=0; 26 | Map vertexNode=null; 27 | float second=0; 28 | float secScore=0; 29 | Map edgeNode=null; 30 | Levenshtein lt=new Levenshtein(); 31 | //对候选边/节点进行筛选,分别挑选最高分数的node作为对应类型的代表 32 | for(Map node:maps){ 33 | if(node!=null) { 34 | float diffLocation=lt.getSimilarityRatio(node.get("name").toString(),requestParameter.question); 35 | node.put("questSimilar",diffLocation); 36 | float score=Float.parseFloat(""+ node.get("score")); 37 | if ("edge".equals(node.get("typeOf"))) { //边 38 | if (diffLocation > second) { 39 | second = diffLocation ; 40 | edgeNode = node; 41 | maxScore = score; 42 | }else if(diffLocation == second){ 43 | // 当前词和之前的词与问句具有相同的编辑距离相似度,则通过索引分数对比 44 | if(maxScore< score){ 45 | edgeNode = node; 46 | maxScore = score; 47 | } 48 | } 49 | } else {//点 50 | if (diffLocation >max) { 51 | max = diffLocation; 52 | vertexNode = node; 53 | secScore = score; 54 | }else if(diffLocation ==max){ 55 | if(secScore < score){ 56 | vertexNode = node; 57 | secScore = score; 58 | } 59 | } 60 | } 61 | } 62 | } 63 | System.out.println("key:"+vertexNode+"\t"+edgeNode); 64 | if(vertexNode!=null&&edgeNode!=null) { 65 | List> maps2 = new ArrayList(); 66 | maps2.add(vertexNode); 67 | maps2.add(edgeNode); 68 | result = questionPaserService.traversePathBynode(requestParameter,maps2); 69 | }else if(vertexNode!=null&&edgeNode==null) { 70 | maps.remove(vertexNode); 71 | //查找分数第二高的索引 72 | Map vertexNode2=questionPaserService.getCloestMaps(maps); 73 | maps.add(vertexNode); 74 | List> maps2 = new ArrayList(); 75 | maps2.add(vertexNode); 76 | maps2.add(vertexNode2); 77 | result = questionPaserService.traversePathBynode(requestParameter,maps2); 78 | }else if(edgeNode!=null&&vertexNode==null){ 79 | maps.remove(edgeNode); 80 | Map edgeNode2=questionPaserService.getCloestMaps(maps); 81 | maps.add(vertexNode); 82 | List> maps2 = new ArrayList(); 83 | maps2.add(edgeNode); 84 | maps2.add(edgeNode2); 85 | result = questionPaserService.traversePathBynode(requestParameter,maps2); 86 | } 87 | } 88 | return result; 89 | } 90 | 91 | 92 | 93 | } 94 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/chat/controller/ChatController.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.chat.controller; 2 | 3 | import com.qdcz.chat.entity.RequestParameter; 4 | import com.qdcz.chat.service.SmartQAService; 5 | import com.qdcz.mongo.MyMongo; 6 | import org.json.JSONArray; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.web.bind.annotation.*; 9 | 10 | import java.util.ArrayList; 11 | 12 | /** 13 | * Created by star on 17-8-1. 14 | */ 15 | @RestController 16 | public class ChatController { 17 | 18 | @Autowired 19 | private SmartQAService smartQAService; 20 | 21 | 22 | @CrossOrigin 23 | @RequestMapping(path = "/ask", method = {RequestMethod.POST,RequestMethod.GET}) 24 | @ResponseBody 25 | public String ask(@RequestParam String question 26 | ,@RequestParam String project) { 27 | RequestParameter requestParameter =null; 28 | String s = null; 29 | try { 30 | requestParameter =new RequestParameter(project); 31 | requestParameter.question=question; 32 | s = smartQAService.smartQA(requestParameter,project); 33 | } catch (Exception e) { 34 | e.printStackTrace(); 35 | s=e.getMessage(); 36 | } 37 | return s; 38 | } 39 | 40 | 41 | @CrossOrigin 42 | @RequestMapping(path = "/askFromWeChat", method = {RequestMethod.POST,RequestMethod.GET}) 43 | @ResponseBody 44 | public String askOfWeChat(@RequestParam String question, 45 | @RequestParam String project){ 46 | RequestParameter requestParameter =null; 47 | String s = null; 48 | try { 49 | requestParameter =new RequestParameter(project); 50 | requestParameter.question=question; 51 | s = smartQAService.smartQA(requestParameter,project); 52 | } catch (Exception e) { 53 | e.printStackTrace(); 54 | } 55 | return s; 56 | } 57 | 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/chat/entity/RequestParameter.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.chat.entity; 2 | 3 | import com.qdcz.conf.DatabaseConfiguration; 4 | import com.qdcz.entity.Graph; 5 | 6 | import java.util.*; 7 | 8 | /** 9 | * Created by hadoop on 17-7-27. 10 | */ 11 | public class RequestParameter { 12 | public RequestParameter(String project) throws Exception { 13 | relationship=new ArrayList<>(); 14 | 15 | List graphs = DatabaseConfiguration.getProject(project); 16 | this.label = graphs.get(0).getLabel(); 17 | relationship.add(graphs.get(0).getRelationship()); 18 | } 19 | public String type; 20 | public String label; 21 | public List relationship; 22 | public String requestSource; 23 | public String question; 24 | 25 | public void clear(){ 26 | type=null; 27 | 28 | label=null; 29 | relationship.clear(); 30 | relationship=null; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/chat/interfaces/ChatQA.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.chat.interfaces; 2 | 3 | import com.hankcs.hanlp.seg.common.Term; 4 | import com.hankcs.hanlp.tokenizer.NLPTokenizer; 5 | import com.hankcs.hanlp.tokenizer.StandardTokenizer; 6 | import com.hankcs.hanlp.utility.Predefine; 7 | import com.mongodb.util.JSON; 8 | import com.qdcz.chat.entity.RequestParameter; 9 | import com.qdcz.chat.service.QuestionPaserService; 10 | import com.qdcz.common.CommonTool; 11 | import com.qdcz.graph.tools.ResultBuilder; 12 | import org.json.JSONArray; 13 | import org.json.JSONException; 14 | import org.json.JSONObject; 15 | import org.neo4j.driver.v1.types.Path; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.stereotype.Service; 18 | 19 | import java.util.*; 20 | 21 | /** 22 | * Created by star on 17-8-11. 23 | */ 24 | @Service 25 | public abstract class ChatQA { 26 | @Autowired 27 | public QuestionPaserService questionPaserService; 28 | 29 | 30 | public abstract Set MatchPath(List> maps , RequestParameter requestParameter); 31 | 32 | public String smartQA(RequestParameter requestParameter) throws Exception {//智能问答 33 | System.out.println("智能问答提出问题:\t"+requestParameter.question); 34 | /* 35 | *分词获取分词关键词 36 | */ 37 | List termLists = getltpInfo(requestParameter.question); 38 | 39 | /* 40 | *关键词转换为图上信息 41 | */ 42 | List transGraphInfo = transGraphInfo(requestParameter,termLists); 43 | 44 | /* 45 | * 搜索图上关键实体 46 | */ 47 | List> maps = searchGraphEntity(termLists, requestParameter); 48 | 49 | 50 | /* 51 | *实体匹配路径 52 | */ 53 | Set paths = MatchPath(maps, requestParameter); 54 | 55 | 56 | /* 57 | * 路径解析 58 | */ 59 | StringBuffer resultPath = parsePathToResult(paths,maps,requestParameter); 60 | 61 | /* 62 | *结果发送 63 | */ 64 | StringBuffer stringBuffer = sortResult( requestParameter, resultPath); 65 | 66 | return stringBuffer.toString(); 67 | } 68 | 69 | /** 70 | * 分词 71 | * @param question 72 | * @return 73 | */ 74 | public List getltpInfo(String question){ 75 | StandardTokenizer.SEGMENT.enableAllNamedEntityRecognize(false); 76 | List termList = NLPTokenizer.segment(question); 77 | List termreplace =new ArrayList<>(); 78 | for(int i=0;i transGraphInfo(RequestParameter requestParameter,List termLists){ 101 | 102 | if("shkx_label".equals(requestParameter.label)){ 103 | 104 | } 105 | 106 | return termLists; 107 | } 108 | 109 | public List> searchGraphEntity(List termLists,RequestParameter requestParameter){ 110 | List> maps= new ArrayList(); 111 | for(Term term:termLists) { 112 | Map node = questionPaserService.getNode(requestParameter,term.word); 113 | if(node!=null) { 114 | maps.add(node); 115 | } 116 | } 117 | return maps; 118 | } 119 | 120 | 121 | public StringBuffer sortResult(RequestParameter requestParameter,StringBuffer pathResult) throws Exception { 122 | 123 | String str = pathResult.toString(); 124 | if("".equals(str)) { 125 | //应急处理阶段--正则定义 126 | str = questionPaserService.findDefine(requestParameter); 127 | } 128 | 129 | 130 | //组装结果 131 | if (str == null || "".equals(str)) { 132 | str=questionPaserService.requestTuring(requestParameter.question); 133 | }else{ 134 | try{ 135 | JSONObject object = new JSONObject(str); 136 | }catch (JSONException e){ 137 | JSONObject resultobj=new JSONObject(); 138 | resultobj.put("type","Graph"); 139 | JSONArray dataArray=new JSONArray(); 140 | dataArray.put(str); 141 | str=resultobj.toString(); 142 | } 143 | } 144 | StringBuffer finalResult=new StringBuffer(); 145 | finalResult.append(str); 146 | return finalResult; 147 | } 148 | 149 | 150 | public StringBuffer parsePathToResult(Set paths, List> maps, RequestParameter requestParameter) { 151 | StringBuffer sb=new StringBuffer(); 152 | ResultBuilder resultBuilder=new ResultBuilder(); 153 | Map> resultPaths= resultBuilder.cleanRestult(paths); 154 | 155 | JSONObject resultJSon=new JSONObject(); 156 | for (Map.Entry> entry : resultPaths.entrySet()){ 157 | JSONArray jsonArray=new JSONArray(); 158 | String result=""; 159 | String key = entry.getKey().replace("--","的"); 160 | Vector value = entry.getValue(); 161 | result += key+"为"; 162 | for(String str:value){ 163 | try{ 164 | JSONObject object = new JSONObject(str); 165 | result=""; 166 | jsonArray.put(object.toString()); 167 | }catch ( JSONException je){ 168 | // je.printStackTrace(); 169 | result+=str+"、"; 170 | } 171 | } 172 | if(!"".equals(result)) { 173 | result = result.substring(0, result.length() - 1) + "。"; 174 | sb.append(result); 175 | } 176 | else if(jsonArray.length()>0){ 177 | resultJSon.put("title",key); 178 | resultJSon.put("type","Law"); 179 | resultJSon.put("data",jsonArray); 180 | sb.append(resultJSon); 181 | } 182 | } 183 | return sb; 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/chat/service/SmartQAService.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.chat.service; 2 | 3 | import com.hankcs.hanlp.seg.common.Term; 4 | import com.qdcz.chat.allchat.AllChatQA; 5 | import com.qdcz.chat.cmbchat.CMBQA; 6 | import com.qdcz.chat.interfaces.ChatQA; 7 | import com.qdcz.chat.entity.RequestParameter; 8 | 9 | import com.qdcz.chat.socialchat.SocialQA; 10 | import com.qdcz.graph.controller.GraphControler; 11 | import org.apache.logging.log4j.LogManager; 12 | import org.apache.logging.log4j.Logger; 13 | import org.neo4j.driver.v1.types.Path; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.stereotype.Service; 16 | 17 | import java.util.*; 18 | 19 | /** 20 | * Created by star on 17-8-1. 21 | */ 22 | @Service 23 | public class SmartQAService { 24 | private Logger logger = LogManager.getLogger(GraphControler.class.getSimpleName()); 25 | @Autowired 26 | private SocialQA socialchatQA; 27 | @Autowired 28 | private CMBQA cmbchatQA; 29 | 30 | @Autowired 31 | private AllChatQA allChatQA; 32 | 33 | public String smartQA(RequestParameter requestParameter,String project) throws Exception {//智能问答 34 | ChatQA chatQA = null; 35 | switch (project){ 36 | case "xz": 37 | chatQA = cmbchatQA; 38 | break; 39 | case "sk": 40 | chatQA = socialchatQA; 41 | break; 42 | case "all": 43 | chatQA = allChatQA; 44 | break; 45 | default: 46 | logger.error("非法请求!\t"+project); 47 | return "非法请求!"; 48 | } 49 | 50 | return chatQA.smartQA(requestParameter); 51 | } 52 | 53 | 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/chat/socialchat/SocialQA.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.chat.socialchat; 2 | 3 | import com.qdcz.chat.entity.RequestParameter; 4 | import com.qdcz.chat.interfaces.ChatQA; 5 | import com.qdcz.chat.tools.MyComparetor; 6 | import com.qdcz.common.CommonTool; 7 | import com.qdcz.entity.Vertex; 8 | import com.qdcz.graph.interfaces.IGraphBuzi; 9 | import com.qdcz.graph.tools.ResultBuilder; 10 | import org.json.JSONArray; 11 | import org.json.JSONObject; 12 | import org.neo4j.driver.v1.types.Path; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.beans.factory.annotation.Qualifier; 15 | import org.springframework.stereotype.Service; 16 | 17 | import java.util.*; 18 | 19 | /** 20 | * Created by star on 17-8-11. 21 | */ 22 | @Service 23 | public class SocialQA extends ChatQA { 24 | @Autowired 25 | @Qualifier("neo4jCypherService") 26 | private IGraphBuzi graphBuzi; 27 | 28 | @Override 29 | public Set MatchPath(List> maps, RequestParameter requestParameter) { 30 | Set paths = null; 31 | MyComparetor mc = new MyComparetor("questSimilar"); 32 | Collections.sort(maps, mc); 33 | Collections.reverse(maps); 34 | String str = null; 35 | //降序后,第一个node就是分数最大的点 36 | Map maxNode = null; 37 | for (Map node : maps) { 38 | if ("node".equals(node.get("typeOf"))) { 39 | maxNode = node; 40 | break; 41 | } 42 | } 43 | if (maxNode != null) { 44 | Vertex vertex=new Vertex(); 45 | CommonTool.transMap2Bean(maxNode,vertex); 46 | try { 47 | List paths1 = graphBuzi.bfExtersion(vertex, 2); 48 | 49 | Set set=new HashSet(); 50 | set.addAll(paths1);//给set填充 51 | paths1.clear();//清空list,不然下次把set元素加入此list的时候是在原来的基础上追加元素的 52 | paths1.addAll(set);//把set的 53 | paths=set; 54 | } catch (Exception e) { 55 | e.printStackTrace(); 56 | } 57 | } 58 | return paths; 59 | } 60 | 61 | @Override 62 | public StringBuffer parsePathToResult(Set paths, List> maps, RequestParameter requestParameter) { 63 | if(paths==null){ 64 | return new StringBuffer(); 65 | } 66 | StringBuffer sb=new StringBuffer(); 67 | ResultBuilder resultBuilder = new ResultBuilder(); 68 | List list = new ArrayList(paths); 69 | JSONObject object = resultBuilder.graphResult( list); 70 | JSONArray nodes = object.getJSONArray("nodes"); 71 | JSONArray edgesJarry = object.getJSONArray("edges"); 72 | Map nodesMaps=new HashMap<>(); 73 | for(int i=0;i> edgeMapping=new HashMap<>(); 80 | Vector value=new Vector(); 81 | value.add("同事");value.add("伙伴");value.add("哪些"); 82 | edgeMapping.put("属于",value); 83 | value=new Vector(); 84 | value.add("合作");value.add("协作"); value.add("作者");value.add("发表"); 85 | edgeMapping.put("发表",value); 86 | 87 | MyComparetor mc = new MyComparetor("questSimilar"); 88 | Collections.sort(maps,mc); 89 | Collections.reverse(maps); 90 | String str = null; 91 | Map maxNode=null; 92 | for(Map node:maps){ 93 | if("node".equals(node.get("typeOf"))){ 94 | maxNode=node; 95 | break; 96 | } 97 | } 98 | if(maxNode!=null) { 99 | Map>> reSultMaps=new HashMap<>(); 100 | Set>> entries = edgeMapping.entrySet(); 101 | for (Map.Entry> entry : entries) { 102 | Vector value1 = entry.getValue(); 103 | for (String s : value1) { 104 | if(requestParameter.question.contains(s)){ 105 | Map> hashmap=new HashMap<>(); 106 | for(int i=0;i strings = hashmap.get(name2); 118 | strings.add(name); 119 | }else{ 120 | Set setStrs=new HashSet<>(); 121 | setStrs.add(name); 122 | hashmap.put(name2,setStrs); 123 | } 124 | } 125 | } 126 | reSultMaps.put(entry.getKey(),hashmap); 127 | 128 | } 129 | } 130 | } 131 | System.out.println(); 132 | for (String s : reSultMaps.keySet()) { 133 | switch (s){ 134 | case "属于": 135 | if("author".equals(maxNode.get("type"))){ 136 | Map> stringSetMap = reSultMaps.get(s); 137 | String r1=""; 138 | for (Map.Entry> entry : stringSetMap.entrySet()) { 139 | String key = entry.getKey(); 140 | Set values = entry.getValue(); 141 | r1 += "在"+key+"的同事有";//为 142 | for(String value2:values){ 143 | if(value2.equals(maxNode.get("name"))){ 144 | continue; 145 | } 146 | r1+=value2+"、"; 147 | } 148 | r1= r1.substring(0, r1.length() - 1) + "。"; 149 | } 150 | sb.append(r1); 151 | }else if("com".equals(maxNode.get("type"))){ 152 | Map> stringSetMap = reSultMaps.get(s); 153 | String r0=""; 154 | for (Map.Entry> entry : stringSetMap.entrySet()) { 155 | String key = entry.getKey(); 156 | Set values = entry.getValue(); 157 | if(key.equals(maxNode.get("name"))){ 158 | r0 += "在"+key+"的人员有";//为 159 | for(String value2:values){ 160 | r0+=value2+"、"; 161 | } 162 | r0= r0.substring(0, r0.length() - 1) + "。"; 163 | } 164 | } 165 | sb.append(r0); 166 | } 167 | 168 | break; 169 | case "发表": 170 | if("author".equals(maxNode.get("type"))){ 171 | Map> stringSetMap = reSultMaps.get(s); 172 | String r2=""; 173 | for (Map.Entry> entry : stringSetMap.entrySet()) { 174 | String key = entry.getKey(); 175 | Set values = entry.getValue(); 176 | if(requestParameter.question.contains(s)){ 177 | for(String value2:values){ 178 | if(value2.equals(maxNode.get("name"))){ 179 | r2+=value2+"发表过的《"+key+"》、"; 180 | break; 181 | } 182 | } 183 | } 184 | else{ 185 | r2 += "在《"+key+"》中合作过的成员有";//为 186 | for(String value2:values){ 187 | if(value2.equals(maxNode.get("name"))){ 188 | continue; 189 | } 190 | r2+=value2+"、"; 191 | } 192 | } 193 | r2= r2.substring(0, r2.length() - 1) + "。"; 194 | } 195 | sb.append(r2); 196 | }else if("paper".equals(maxNode.get("type"))){ 197 | Map> stringSetMap = reSultMaps.get(s); 198 | String r3=""; 199 | for (Map.Entry> entry : stringSetMap.entrySet()) { 200 | String key = entry.getKey(); 201 | if(key.equals(maxNode.get("name").toString())){ 202 | Set values = entry.getValue(); 203 | r3 += "在《"+key+"》中作者有";//为 204 | for(String value2:values){ 205 | r3+=value2+"、"; 206 | } 207 | r3= r3.substring(0, r3.length() - 1) + "。"; 208 | } 209 | } 210 | sb.append(r3); 211 | } 212 | break; 213 | default: 214 | break; 215 | } 216 | } 217 | } 218 | return sb; 219 | } 220 | } 221 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/chat/tools/Levenshtein.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.chat.tools; 2 | 3 | /** 4 | * Created by hadoop on 17-4-20. 5 | */ 6 | public class Levenshtein { 7 | // 字符串相似度匹配 8 | private int compare(String str, String target) { 9 | int d[][]; // 矩阵 10 | int n = str.length(); 11 | int m = target.length(); 12 | int i; // 遍历str的 13 | int j; // 遍历target的 14 | char ch1; // str的 15 | char ch2; // target的 16 | int temp; // 记录相同字符,在某个矩阵位置值的增量,不是0就是1 17 | 18 | if (n == 0) { 19 | return m; 20 | } 21 | 22 | if (m == 0) { 23 | return n; 24 | } 25 | 26 | d = new int[n + 1][m + 1]; 27 | 28 | for (i = 0; i <= n; i++) { // 初始化第一列 29 | d[i][0] = i; 30 | } 31 | 32 | for (j = 0; j <= m; j++) { // 初始化第一行 33 | d[0][j] = j; 34 | } 35 | 36 | for (i = 1; i <= n; i++) { // 遍历str 37 | ch1 = str.charAt(i - 1); 38 | // 去匹配target 39 | for (j = 1; j <= m; j++) { 40 | ch2 = target.charAt(j - 1); 41 | if (ch1 == ch2) { 42 | temp = 0; 43 | } else { 44 | temp = 1; 45 | } 46 | 47 | // 左边+1,上边+1, 左上角+temp取最小 48 | d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] 49 | + temp); 50 | } 51 | } 52 | // int min=d[n][0]; 53 | // for(int x=0;x(); 67 | InputStream resourceAsStream = LoadConfigListener.class.getResourceAsStream("/concept_rule_simple.txt"); 68 | InputStreamReader isr = new InputStreamReader(resourceAsStream); 69 | // BufferedReader reader = new BufferedReader(isr); 70 | LineNumberReader reader = new LineNumberReader(isr); 71 | String line = null; 72 | while((line=reader.readLine())!=null){ 73 | //System.out.println(line); 74 | if(!"".equals(line) && !line.startsWith("#")){ 75 | String[] arr = line.split(" "); 76 | ruler.add(new ConceptRule(arr[0].trim(), Integer.parseInt(arr[1])+1)); 77 | } 78 | } 79 | reader.close(); 80 | } 81 | } 82 | 83 | class ConceptRule{ 84 | public Pattern pattern; 85 | public int index; 86 | 87 | public ConceptRule(String reg, int index){ 88 | this.pattern = Pattern.compile(reg); 89 | this.index = index; 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/common/XMLUtil.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.common; 2 | 3 | import javax.xml.bind.JAXBContext; 4 | import javax.xml.bind.JAXBException; 5 | import javax.xml.bind.Marshaller; 6 | import javax.xml.bind.Unmarshaller; 7 | import java.io.*; 8 | 9 | /** 10 | * 封装了XML转换成object,object转换成XML的代码 11 | * 12 | * @author Steven 13 | * 14 | */ 15 | public class XMLUtil { 16 | /** 17 | * 将对象直接转换成String类型的 XML输出 18 | * 19 | * @param obj 20 | * @return 21 | */ 22 | public static String convertToXml(Object obj) { 23 | // 创建输出流 24 | StringWriter sw = new StringWriter(); 25 | try { 26 | // 利用jdk中自带的转换类实现 27 | JAXBContext context = JAXBContext.newInstance(obj.getClass()); 28 | 29 | Marshaller marshaller = context.createMarshaller(); 30 | // 格式化xml输出的格式 31 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, 32 | Boolean.TRUE); 33 | // 将对象转换成输出流形式的xml 34 | marshaller.marshal(obj, sw); 35 | } catch (JAXBException e) { 36 | e.printStackTrace(); 37 | } 38 | return sw.toString(); 39 | } 40 | 41 | /** 42 | * 将对象根据路径转换成xml文件 43 | * 44 | * @param obj 45 | * @param path 46 | * @return 47 | */ 48 | public static void convertToXml(Object obj, String path) { 49 | try { 50 | // 利用jdk中自带的转换类实现 51 | JAXBContext context = JAXBContext.newInstance(obj.getClass()); 52 | 53 | Marshaller marshaller = context.createMarshaller(); 54 | // 格式化xml输出的格式 55 | marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, 56 | Boolean.TRUE); 57 | // 将对象转换成输出流形式的xml 58 | // 创建输出流 59 | FileWriter fw = null; 60 | try { 61 | fw = new FileWriter(path); 62 | } catch (IOException e) { 63 | e.printStackTrace(); 64 | } 65 | marshaller.marshal(obj, fw); 66 | } catch (JAXBException e) { 67 | e.printStackTrace(); 68 | } 69 | } 70 | 71 | @SuppressWarnings("unchecked") 72 | /** 73 | * 将String类型的xml转换成对象 74 | */ 75 | public static Object convertXmlStrToObject(Class clazz, String xmlStr) { 76 | Object xmlObject = null; 77 | try { 78 | JAXBContext context = JAXBContext.newInstance(clazz); 79 | // 进行将Xml转成对象的核心接口 80 | Unmarshaller unmarshaller = context.createUnmarshaller(); 81 | StringReader sr = new StringReader(xmlStr); 82 | xmlObject = unmarshaller.unmarshal(sr); 83 | } catch (JAXBException e) { 84 | e.printStackTrace(); 85 | } 86 | return xmlObject; 87 | } 88 | 89 | @SuppressWarnings("unchecked") 90 | /** 91 | * 将file类型的xml转换成对象 92 | */ 93 | public static Object convertXmlFileToObject(Class clazz, String xmlPath) { 94 | Object xmlObject = null; 95 | try { 96 | JAXBContext context = JAXBContext.newInstance(clazz); 97 | Unmarshaller unmarshaller = context.createUnmarshaller(); 98 | FileReader fr = null; 99 | try { 100 | fr = new FileReader(xmlPath); 101 | } catch (FileNotFoundException e) { 102 | e.printStackTrace(); 103 | } 104 | xmlObject = unmarshaller.unmarshal(fr); 105 | } catch (JAXBException e) { 106 | e.printStackTrace(); 107 | } 108 | return xmlObject; 109 | } 110 | 111 | 112 | 113 | @SuppressWarnings("unchecked") 114 | /** 115 | * 将file类型的xml转换成对象 116 | */ 117 | public static Object convertXmlFileToObject(Class clazz, InputStream inStream) { 118 | Object xmlObject = null; 119 | try { 120 | JAXBContext context = JAXBContext.newInstance(clazz); 121 | Unmarshaller unmarshaller = context.createUnmarshaller(); 122 | InputStreamReader fr = null; 123 | try { 124 | fr =new InputStreamReader(inStream); 125 | } catch (Exception e) { 126 | e.printStackTrace(); 127 | } 128 | xmlObject = unmarshaller.unmarshal(fr); 129 | } catch (JAXBException e) { 130 | e.printStackTrace(); 131 | } 132 | return xmlObject; 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/conf/DatabaseConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.conf; 2 | 3 | import com.qdcz.common.XMLUtil; 4 | import com.qdcz.entity.Graph; 5 | import com.qdcz.entity.GraphDatabase; 6 | 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.util.List; 10 | import java.util.Map; 11 | import java.util.Properties; 12 | 13 | /** 14 | * 存储所有的数据库名称 15 | * Created by star on 17-8-8. 16 | */ 17 | public class DatabaseConfiguration extends Properties{ 18 | public static Map graphs; 19 | public static Map> projects; 20 | 21 | @Override 22 | public synchronized void load(InputStream inStream) throws IOException { 23 | GraphDatabase graphDatabase = ((GraphDatabase) XMLUtil.convertXmlFileToObject(GraphDatabase.class,inStream)); 24 | graphs = graphDatabase.getGraphMap(); 25 | projects = graphDatabase.getProjectMap(); 26 | } 27 | 28 | 29 | public static String getRelationshipByLabel(String label) throws Exception { 30 | for (Graph graph:graphs.values()){ 31 | if(label.equals(graph.getLabel())){ 32 | return graph.getRelationship(); 33 | } 34 | } 35 | throw new Exception("未找到对应的边库"+label); 36 | } 37 | 38 | public static String getLabelByRelationship(String relationship) throws Exception { 39 | for (Graph graph:graphs.values()){ 40 | if(relationship.equals(graph.getRelationship())){ 41 | return graph.getLabel(); 42 | } 43 | } 44 | throw new Exception("未找到对应的点库"+relationship); 45 | } 46 | 47 | 48 | public static Graph getGraph(String graphName) throws Exception { 49 | if(graphs.containsKey(graphName)){ 50 | return graphs.get(graphName); 51 | } 52 | throw new Exception("未找到对应的图谱Graph"+graphName); 53 | } 54 | 55 | 56 | public static List getProject(String projectName) throws Exception { 57 | if(projects.containsKey(projectName)){ 58 | return projects.get(projectName); 59 | } 60 | throw new Exception("未找到对应的工程project:"+projectName); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/conf/LoadConfigListener.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.conf; 2 | 3 | import com.hankcs.hanlp.utility.Predefine; 4 | import com.qdcz.mongo.conf.MongoConfiguration; 5 | import com.qdcz.graph.neo4jcypher.conf.Neo4jConfiguration; 6 | import com.qdcz.index.elsearch.conf.ELKConfig; 7 | import org.springframework.beans.factory.annotation.Value; 8 | 9 | import javax.servlet.ServletContextEvent; 10 | import javax.servlet.ServletContextListener; 11 | import javax.servlet.annotation.WebListener; 12 | import java.io.IOException; 13 | 14 | /** 15 | * Created by hadoop on 17-6-27. 16 | * 17 | */ 18 | @WebListener 19 | public class LoadConfigListener implements ServletContextListener { 20 | 21 | 22 | 23 | @Value("${source_dir}") 24 | private String source_dir; 25 | 26 | 27 | 28 | @Value("${hanlp_path}") 29 | private String hanlp_path; 30 | 31 | @Override 32 | public void contextDestroyed(ServletContextEvent arg0) { 33 | // TODO Auto-generated method stub 34 | 35 | } 36 | 37 | @Override 38 | public void contextInitialized(ServletContextEvent arg0) { 39 | try { 40 | Predefine.HANLP_PROPERTIES_PATH = hanlp_path+"hanlp.properties"; 41 | //------------加载neo4j的配置------------ 42 | System.out.println("------------加载neo4j的配置------------"); 43 | Neo4jConfiguration neo4jConfiger=new Neo4jConfiguration(); 44 | neo4jConfiger.load(LoadConfigListener.class.getResourceAsStream(source_dir+"neo4j.properties")); 45 | 46 | System.out.println("------------加载MongoDB配置文件------------"); 47 | MongoConfiguration mongoConf = new MongoConfiguration(); 48 | mongoConf.load(LoadConfigListener.class.getResourceAsStream(source_dir+"mongo.properties")); 49 | 50 | System.out.println("------------加载ElaSearch配置文件------------"); 51 | ELKConfig elkConfig = new ELKConfig(); 52 | elkConfig.load(LoadConfigListener.class.getResourceAsStream(source_dir+"elasearch.properties")); 53 | 54 | System.out.println("------------加载Database配置文件------------"); 55 | DatabaseConfiguration databaseConf = new DatabaseConfiguration(); 56 | databaseConf.load(LoadConfigListener.class.getResourceAsStream(source_dir+ "database.xml")); 57 | 58 | } catch (IOException e) { 59 | e.printStackTrace(); 60 | } 61 | } 62 | public String getSource_dir() { 63 | return source_dir; 64 | } 65 | 66 | public void setSource_dir(String source_dir) { 67 | this.source_dir = source_dir; 68 | } 69 | public String getHanlp_path() { 70 | return hanlp_path; 71 | } 72 | 73 | public void setHanlp_path(String hanlp_path) { 74 | this.hanlp_path = hanlp_path; 75 | } 76 | 77 | } -------------------------------------------------------------------------------- /src/main/java/com/qdcz/entity/Edge.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.entity; 2 | 3 | import org.json.JSONObject; 4 | import org.neo4j.driver.v1.Value; 5 | import org.neo4j.driver.v1.types.Relationship; 6 | import org.neo4j.driver.v1.util.Function; 7 | 8 | import java.util.Map; 9 | 10 | 11 | /** 12 | * Created by hadoop on 17-6-22. 13 | */ 14 | public class Edge implements IGraphEntity,Relationship{ 15 | 16 | private String id = ""; 17 | 18 | private String name = ""; 19 | private String root = ""; 20 | 21 | 22 | private String from = ""; 23 | private String to; 24 | private String relationShip;//表名 25 | 26 | private int ela_start = 0; 27 | private int ela_end = 10; 28 | 29 | 30 | public Edge(){ 31 | 32 | } 33 | public Edge(JSONObject json){ 34 | if(json.has("id")){ 35 | this.id = json.getString("id"); 36 | } 37 | if(json.has("name")){ 38 | this.name = json.getString("name"); 39 | } 40 | if(json.has("from")){ 41 | this.from = json.getString("from"); 42 | } 43 | if(json.has("root")){ 44 | this.root = json.getString("root"); 45 | } 46 | if(json.has("to")){ 47 | this.to = json.getString("to"); 48 | } 49 | if(json.has("relationship")){ 50 | this.relationShip = json.getString("relationship"); 51 | } 52 | } 53 | 54 | 55 | public Edge(String name, String root, String from, String to, String relationShip) { 56 | this.name = name; 57 | this.root = root; 58 | this.from = from; 59 | this.to = to; 60 | this.relationShip = relationShip; 61 | } 62 | @Override 63 | public String toString() { 64 | return String.format("%s/%s/%s", from, name, to); 65 | } 66 | 67 | @Override 68 | public JSONObject toJSON() { 69 | JSONObject obj = new JSONObject(); 70 | 71 | obj.put("from",from); 72 | obj.put("to",to); 73 | obj.put("root",root); 74 | obj.put("name",name); 75 | obj.put("name_length",name.length()); 76 | 77 | return obj; 78 | } 79 | 80 | @Override 81 | public String getGraphId() { 82 | 83 | return id; 84 | } 85 | 86 | @Override 87 | public String getGraphType() { 88 | return relationShip; 89 | } 90 | 91 | @Override 92 | public JSONObject toQueryJSON() { 93 | JSONObject obj = new JSONObject(); 94 | 95 | if(root!=null && !root.isEmpty()) { 96 | obj.put("root",root); 97 | } 98 | if(root!=null && !root.isEmpty()) { 99 | obj.put("root",root); 100 | } 101 | if(root!=null && !root.isEmpty()) { 102 | obj.put("root",root); 103 | } 104 | if(root!=null && !root.isEmpty()) { 105 | obj.put("root",root); 106 | } 107 | 108 | return obj; 109 | } 110 | public String getId(){ 111 | return id; 112 | } 113 | 114 | public void setId(String id) { 115 | this.id = id; 116 | } 117 | 118 | public String getName() { 119 | return name; 120 | } 121 | 122 | public void setName(String name) { 123 | this.name = name; 124 | } 125 | 126 | public String getRoot() { 127 | return root; 128 | } 129 | 130 | public void setRoot(String root) { 131 | this.root = root; 132 | } 133 | 134 | public String getFrom() { 135 | return from; 136 | } 137 | 138 | public void setFrom(String from) { 139 | this.from = from; 140 | } 141 | 142 | public String getTo() { 143 | return to; 144 | } 145 | 146 | public void setTo(String to) { 147 | this.to = to; 148 | } 149 | 150 | public String getRelationShip() { 151 | return relationShip; 152 | } 153 | 154 | public void setRelationShip(String relationShip) { 155 | this.relationShip = relationShip; 156 | } 157 | 158 | public int getEla_start() { 159 | return ela_start; 160 | } 161 | 162 | public void setEla_start(int ela_start) { 163 | this.ela_start = ela_start; 164 | } 165 | 166 | public int getEla_end() { 167 | return ela_end; 168 | } 169 | 170 | public void setEla_end(int ela_end) { 171 | this.ela_end = ela_end; 172 | } 173 | 174 | @Override 175 | public long startNodeId() { 176 | return Long.parseLong(this.from); 177 | } 178 | 179 | @Override 180 | public long endNodeId() { 181 | return Long.parseLong(this.to); 182 | } 183 | 184 | @Override 185 | public String type() { 186 | return this.getRelationShip(); 187 | } 188 | 189 | @Override 190 | public boolean hasType(String s) { 191 | return false; 192 | } 193 | 194 | @Override 195 | public long id() { 196 | return Long.parseLong(id); 197 | } 198 | 199 | @Override 200 | public Iterable keys() { 201 | 202 | return null; 203 | } 204 | 205 | @Override 206 | public boolean containsKey(String s) { 207 | return false; 208 | } 209 | 210 | @Override 211 | public Value get(String s) { 212 | return null; 213 | } 214 | 215 | @Override 216 | public int size() { 217 | return 0; 218 | } 219 | 220 | @Override 221 | public Iterable values() { 222 | return null; 223 | } 224 | 225 | @Override 226 | public Iterable values(Function function) { 227 | return null; 228 | } 229 | 230 | @Override 231 | public Map asMap() { 232 | return null; 233 | } 234 | 235 | @Override 236 | public Map asMap(Function function) { 237 | return null; 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/entity/Graph.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.entity; 2 | 3 | import javax.xml.bind.annotation.XmlAccessType; 4 | import javax.xml.bind.annotation.XmlAccessorType; 5 | import javax.xml.bind.annotation.XmlRootElement; 6 | import java.util.List; 7 | 8 | 9 | @XmlAccessorType(XmlAccessType.FIELD) 10 | //XML文件中的根标识 11 | @XmlRootElement(name = "graphs") 12 | class Graphs{ 13 | private List graph; 14 | 15 | public List getGraph() { 16 | return graph; 17 | } 18 | 19 | public void setGraphs(List graphs) { 20 | this.graph = graphs; 21 | } 22 | } 23 | // 24 | //@XmlAccessorType(XmlAccessType.FIELD) 25 | ////XML文件中的根标识 26 | //@XmlRootElement(name = "graph") 27 | //public class Graph { 28 | // private String name; 29 | // private Value value; 30 | // 31 | // public String getName() { 32 | // return name; 33 | // } 34 | // 35 | // public void setName(String name) { 36 | // this.name = name; 37 | // } 38 | //} 39 | 40 | 41 | @XmlAccessorType(XmlAccessType.FIELD) 42 | //XML文件中的根标识 43 | @XmlRootElement(name = "graph") 44 | public class Graph{ 45 | private String name; 46 | private String title; 47 | private String label; 48 | private String relationship; 49 | 50 | public String getName() { 51 | return name; 52 | } 53 | 54 | public void setName(String name) { 55 | this.name = name; 56 | } 57 | 58 | public String getTitle() { 59 | return title; 60 | } 61 | 62 | public void setTitle(String title) { 63 | this.title = title; 64 | } 65 | 66 | public String getLabel() { 67 | return label; 68 | } 69 | 70 | public void setLabel(String label) { 71 | this.label = label; 72 | } 73 | 74 | public String getRelationship() { 75 | return relationship; 76 | } 77 | 78 | public void setRelationship(String relationship) { 79 | this.relationship = relationship; 80 | } 81 | } -------------------------------------------------------------------------------- /src/main/java/com/qdcz/entity/GraphDatabase.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.entity; 2 | 3 | 4 | import com.qdcz.common.XMLUtil; 5 | 6 | import javax.xml.bind.annotation.XmlAccessType; 7 | import javax.xml.bind.annotation.XmlAccessorType; 8 | import javax.xml.bind.annotation.XmlRootElement; 9 | import java.util.ArrayList; 10 | import java.util.HashMap; 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | @XmlAccessorType(XmlAccessType.FIELD) 15 | //XML文件中的根标识 16 | @XmlRootElement(name = "xml") 17 | public class GraphDatabase { 18 | public static void main(String[] args) { 19 | 20 | GraphDatabase graphDatabase= (GraphDatabase) XMLUtil.convertXmlFileToObject(GraphDatabase.class,"/media/star/Soft/WorkSpace/KBQA/src/main/resources/dev/database.xml"); 21 | System.out.println(XMLUtil.convertToXml(graphDatabase)); 22 | } 23 | 24 | private Graphs graphs; 25 | 26 | private Projects projects; 27 | 28 | 29 | 30 | public Projects getProjects() { 31 | return projects; 32 | } 33 | 34 | public void setProjects(Projects projects) { 35 | this.projects = projects; 36 | } 37 | 38 | 39 | public Graphs getGraphs() { 40 | return graphs; 41 | } 42 | 43 | public void setGraphs(Graphs graphs) { 44 | this.graphs = graphs; 45 | } 46 | 47 | 48 | /** 49 | * 获取graphmap,便于检索 50 | * @return 51 | */ 52 | public Map getGraphMap(){ 53 | Map graphMap = new HashMap<>(); 54 | 55 | for (Graph graph:graphs.getGraph()) { 56 | graphMap.put(graph.getName(),graph); 57 | } 58 | return graphMap; 59 | } 60 | 61 | /** 62 | * 获取projectmap,便于检索 63 | * @return 64 | */ 65 | public Map> getProjectMap(){ 66 | Map> projectMap = new HashMap<>(); 67 | 68 | for (Project project:projects.getProject()) { 69 | String project_name = project.getProject_name(); 70 | List project_graphs = new ArrayList<>(); 71 | for (String graphName:project.getGraphs().getGraph_name()) { 72 | for (Graph graph:graphs.getGraph()) { 73 | if(graphName.equals(graph.getName())){ 74 | project_graphs.add(graph); 75 | break; 76 | } 77 | } 78 | } 79 | projectMap.put(project_name,project_graphs); 80 | 81 | } 82 | return projectMap; 83 | } 84 | 85 | 86 | 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/entity/IGraphEntity.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.entity; 2 | 3 | import org.json.JSONObject; 4 | import org.neo4j.driver.v1.types.Relationship; 5 | 6 | /** 7 | * elasearch 需要的业务 8 | * Created by star on 17-8-2. 9 | */ 10 | public interface IGraphEntity { 11 | public JSONObject toJSON(); 12 | public String getGraphId(); 13 | public String getGraphType(); 14 | 15 | public JSONObject toQueryJSON(); 16 | 17 | 18 | public int getEla_start(); 19 | public int getEla_end(); 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/entity/Project.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.entity; 2 | 3 | import javax.xml.bind.annotation.XmlAccessType; 4 | import javax.xml.bind.annotation.XmlAccessorType; 5 | import javax.xml.bind.annotation.XmlRootElement; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by star on 17-8-11. 10 | */ 11 | 12 | @XmlAccessorType(XmlAccessType.FIELD) 13 | //XML文件中的根标识 14 | @XmlRootElement(name = "projects") 15 | class Projects{ 16 | private List project; 17 | 18 | public List getProject() { 19 | return project; 20 | } 21 | 22 | public void setProjects(List projects) { 23 | this.project = projects; 24 | } 25 | } 26 | 27 | 28 | @XmlAccessorType(XmlAccessType.FIELD) 29 | //XML文件中的根标识 30 | @XmlRootElement(name = "project") 31 | public class Project { 32 | private String project_name; 33 | private ProjectName graphs; 34 | 35 | public ProjectName getGraphs() { 36 | return graphs; 37 | } 38 | 39 | public void setGraphs(ProjectName graphs) { 40 | this.graphs = graphs; 41 | } 42 | 43 | public String getProject_name() { 44 | return project_name; 45 | } 46 | 47 | public void setProject_name(String project_name) { 48 | this.project_name = project_name; 49 | } 50 | 51 | 52 | 53 | 54 | } 55 | 56 | 57 | @XmlAccessorType(XmlAccessType.FIELD) 58 | //XML文件中的根标识 59 | @XmlRootElement(name = "graphs") 60 | class ProjectName{ 61 | public List getGraph_name() { 62 | return graph_name; 63 | } 64 | 65 | public void setGraph_name(List graph_name) { 66 | this.graph_name = graph_name; 67 | } 68 | 69 | private List graph_name; 70 | } -------------------------------------------------------------------------------- /src/main/java/com/qdcz/entity/Vertex.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.entity; 2 | 3 | import org.json.JSONObject; 4 | import org.neo4j.driver.v1.Value; 5 | import org.neo4j.driver.v1.types.Node; 6 | import org.neo4j.driver.v1.util.Function; 7 | 8 | import java.util.HashSet; 9 | import java.util.Map; 10 | import java.util.Set; 11 | 12 | 13 | /** 14 | * Created by hadoop on 17-6-22. 15 | */ 16 | public class Vertex implements IGraphEntity,Node { 17 | 18 | private String id = ""; 19 | 20 | private String name = ""; 21 | private String root = ""; 22 | 23 | private String label = "";//表名 24 | private String content = ""; 25 | private String type = ""; 26 | private String identity = ""; 27 | 28 | private int ela_start = 0; 29 | private int ela_end = 10; 30 | 31 | public int getEla_start() { 32 | return ela_start; 33 | } 34 | 35 | public void setEla_start(int ela_start) { 36 | this.ela_start = ela_start; 37 | } 38 | 39 | public int getEla_end() { 40 | return ela_end; 41 | } 42 | 43 | public void setEla_end(int ela_end) { 44 | this.ela_end = ela_end; 45 | } 46 | 47 | public Vertex() { 48 | 49 | } 50 | 51 | public Vertex(JSONObject json) { 52 | if(json.has("id")){ 53 | this.id = json.getString("id"); 54 | } 55 | if(json.has("name")){ 56 | this.name = json.getString("name"); 57 | } 58 | if(json.has("content")){ 59 | this.content = json.getString("content"); 60 | } 61 | if(json.has("root")){ 62 | this.root = json.getString("root"); 63 | } 64 | if(json.has("type")){ 65 | this.type = json.getString("type"); 66 | } 67 | if(json.has("identity")){ 68 | this.identity = json.getString("identity"); 69 | } 70 | if(json.has("label")){ 71 | this.label = json.getString("label"); 72 | } 73 | 74 | 75 | } 76 | 77 | 78 | public Vertex(String name, String root, String label, String type) { 79 | this.name = name; 80 | this.root = root; 81 | this.label = label; 82 | this.type = type; 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return String.format("%s/%s/%s", type, name,id); 88 | } 89 | 90 | @Override 91 | public JSONObject toJSON() { 92 | JSONObject obj = new JSONObject(); 93 | 94 | obj.put("type",type); 95 | obj.put("name",name); 96 | obj.put("root",root); 97 | obj.put("content",content); 98 | obj.put("identity",identity); 99 | obj.put("name_length",name.length()); 100 | 101 | return obj; 102 | } 103 | 104 | @Override 105 | public String getGraphId() { 106 | return id; 107 | } 108 | 109 | @Override 110 | public String getGraphType() { 111 | return label; 112 | } 113 | 114 | 115 | @Override 116 | public JSONObject toQueryJSON() { 117 | JSONObject obj = new JSONObject(); 118 | 119 | if(type!=null && !type.isEmpty()) { 120 | 121 | obj.put("type", type); 122 | 123 | } 124 | 125 | if(name!=null && !name.isEmpty()) { 126 | obj.put("name", name); 127 | } 128 | 129 | if(root!=null && !root.isEmpty()) { 130 | obj.put("root",root); 131 | } 132 | if(content!=null && !content.isEmpty()) { 133 | obj.put("content",content); 134 | } 135 | 136 | return obj; 137 | } 138 | 139 | 140 | public String getId(){ 141 | return id; 142 | } 143 | 144 | public void setId(String id) { 145 | this.id = id; 146 | } 147 | 148 | public String getName() { 149 | return name; 150 | } 151 | 152 | public void setName(String name) { 153 | this.name = name; 154 | } 155 | 156 | public String getRoot() { 157 | return root; 158 | } 159 | 160 | public void setRoot(String root) { 161 | this.root = root; 162 | } 163 | 164 | public String getLabel() { 165 | return label; 166 | } 167 | 168 | public void setLabel(String label) { 169 | this.label = label; 170 | } 171 | 172 | public String getContent() { 173 | return content; 174 | } 175 | 176 | public void setContent(String content) { 177 | this.content = content; 178 | } 179 | 180 | public String getType() { 181 | return type; 182 | } 183 | 184 | public void setType(String type) { 185 | this.type = type; 186 | } 187 | 188 | public String getIdentity() { 189 | return identity; 190 | } 191 | 192 | public void setIdentity(String identity) { 193 | this.identity = identity; 194 | } 195 | 196 | 197 | 198 | 199 | @Override 200 | public Iterable labels() { 201 | Set set=new HashSet<>(); 202 | set.add(label); 203 | return set; 204 | } 205 | 206 | @Override 207 | public boolean hasLabel(String s) { 208 | return false; 209 | } 210 | 211 | @Override 212 | public long id() { 213 | return Long.parseLong(id); 214 | } 215 | 216 | @Override 217 | public Iterable keys() { 218 | return null; 219 | } 220 | 221 | @Override 222 | public boolean containsKey(String s) { 223 | return false; 224 | } 225 | 226 | @Override 227 | public Value get(String s) { 228 | return null; 229 | } 230 | 231 | @Override 232 | public int size() { 233 | return 0; 234 | } 235 | 236 | @Override 237 | public Iterable values() { 238 | return null; 239 | } 240 | 241 | @Override 242 | public Iterable values(Function function) { 243 | return null; 244 | } 245 | 246 | @Override 247 | public Map asMap() { 248 | return null; 249 | } 250 | 251 | @Override 252 | public Map asMap(Function function) { 253 | return null; 254 | } 255 | } 256 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/graph/controller/GraphControler.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.graph.controller; 2 | 3 | import com.qdcz.conf.DatabaseConfiguration; 4 | import com.qdcz.entity.Edge; 5 | import com.qdcz.entity.Graph; 6 | import com.qdcz.entity.Vertex; 7 | import com.qdcz.graph.service.GraphOperateService; 8 | 9 | import org.apache.logging.log4j.LogManager; 10 | import org.apache.logging.log4j.Logger; 11 | import org.json.JSONArray; 12 | import org.json.JSONObject; 13 | import org.springframework.beans.factory.annotation.Autowired; 14 | import org.springframework.web.bind.annotation.*; 15 | import javax.servlet.http.HttpServletRequest; 16 | import java.io.IOException; 17 | import java.util.Map; 18 | //import java.util.logging.Logger; 19 | 20 | /** 21 | * Created by star on 17-8-1. 22 | */ 23 | @RestController 24 | public class GraphControler { 25 | 26 | 27 | private Logger logger = LogManager.getLogger(GraphControler.class.getSimpleName()); 28 | @Autowired 29 | private GraphOperateService newTrasa; 30 | // @Autowired 31 | 32 | 33 | @RequestMapping(path = "/bluckaddedges", method = {RequestMethod.POST,RequestMethod.GET}) 34 | public boolean bluckaddedges( 35 | @RequestParam String edgesfile, 36 | @RequestParam String relationship) throws Exception { 37 | logger.info("bluckadd——edgesPath:"+edgesfile+"\trelationship:"+relationship); 38 | 39 | 40 | return newTrasa.bluckaddedges(edgesfile,relationship); 41 | } 42 | 43 | @RequestMapping(path = "/bluckaddvertex", method = {RequestMethod.POST,RequestMethod.GET}) 44 | public boolean bluckAddvertex(@RequestParam String vertexsPath, 45 | @RequestParam String label) throws Exception { 46 | logger.info("bluckadd——vetexsPath:"+vertexsPath+"\tlabel:"+label); 47 | 48 | 49 | return newTrasa.bluckAddvertexWiki(vertexsPath,label); 50 | } 51 | 52 | 53 | 54 | @RequestMapping(path = "/bluckadd2", method = {RequestMethod.POST,RequestMethod.GET}) 55 | public boolean testadd2(@RequestParam String vertexsPath, 56 | @RequestParam String label, 57 | @RequestParam String edgesPath, 58 | @RequestParam String relationship){ 59 | logger.info("bluckadd——vetexsPath:"+vertexsPath+"\tlabel:"+label+"\tedgesPath:"+edgesPath+"\trelationship:"+relationship); 60 | 61 | 62 | return newTrasa.addVertexsByPath2(vertexsPath,label,edgesPath,relationship); 63 | } 64 | 65 | 66 | 67 | 68 | @RequestMapping(path = "/bluckadd", method = {RequestMethod.POST,RequestMethod.GET}) 69 | public boolean testadd(@RequestParam String vertexsPath, 70 | @RequestParam String label, 71 | @RequestParam String edgesPath, 72 | @RequestParam String relationship){ 73 | logger.info("bluckadd——vetexsPath:"+vertexsPath+"\tlabel:"+label+"\tedgesPath:"+edgesPath+"\trelationship:"+relationship); 74 | 75 | 76 | return newTrasa.addVertexsByPath(vertexsPath,label,edgesPath,relationship); 77 | } 78 | @RequestMapping(path = "/testdel", method = {RequestMethod.POST,RequestMethod.GET}) 79 | public boolean testdek(@RequestParam String vertexsPath, 80 | @RequestParam String label){ 81 | 82 | //TODO 83 | newTrasa.delVertexByPath(vertexsPath,label); 84 | return false; 85 | } 86 | 87 | 88 | @CrossOrigin 89 | @RequestMapping(path = "/graphOp", method = {RequestMethod.POST,RequestMethod.GET}) 90 | @ResponseBody 91 | public String graphOp(HttpServletRequest request){ 92 | 93 | JSONObject obj=null; 94 | String type = null; 95 | 96 | Map parameterMap = request.getParameterMap(); 97 | if(parameterMap.size()==0){ 98 | return "param is null"; 99 | } 100 | 101 | String result =null; 102 | try { 103 | if(parameterMap.containsKey("data")){ 104 | // System.out.println(parameterMap.get("data")[0]); 105 | obj= new JSONObject(parameterMap.get("data")[0]); 106 | }else{ 107 | System.out.println( "error param"); 108 | return "failure"; 109 | } 110 | 111 | Vertex vertex = null; 112 | Edge edge = null; 113 | 114 | 115 | vertex = new Vertex(obj.getJSONObject("info").getJSONObject("node")); 116 | edge = new Edge(obj.getJSONObject("info").getJSONObject("edge")); 117 | 118 | 119 | type = obj.getString("type"); 120 | 121 | // vertex.setRoot("社会科学知识库"); 122 | // edge.setRoot("社会科学知识库"); 123 | 124 | 125 | 126 | logger.debug(type+"\trequest:"+obj); 127 | 128 | switch (type){ 129 | case "checkByName": 130 | //通过名称查询 131 | 132 | JSONArray graph_Byname = obj.getJSONArray("graph"); 133 | // 134 | // vertex.setLabel("ytdk_label"); 135 | // edge.setRelationShip("ytdk_relationship"); 136 | 137 | // JSONArray graph_Byname = new JSONArray(); 138 | // graph_Byname.put("ytdkyw"); 139 | 140 | int layer = Integer.parseInt(obj.getString("layer")); 141 | // int layer =3; 142 | 143 | result = newTrasa.directedBfExtersion(vertex.getName(),graph_Byname,layer); 144 | // result = newTrasa.exactMatchQuery(vertex.getName(),graph_Byname,layer); 145 | 146 | break; 147 | case "checkByNameAndDepth": 148 | 149 | // int depth=Integer.parseInt(obj.getJSONObject("info").getString("layer")); 150 | // result = newTrasa.exactMatchQuery(obj.getJSONObject("info").getJSONObject("node").getString("name"),depth); 151 | 152 | break; 153 | case "checkByIndex": 154 | //TODO 暂时调用 155 | 156 | 157 | JSONArray graph_byIndex = obj.getJSONArray("graph"); 158 | // 159 | // vertex.setLabel("shkx_label"); 160 | // edge.setRelationShip("shkx_relationship"); 161 | 162 | 163 | // JSONArray graph_Byname_index = new JSONArray(); 164 | // graph_Byname_index.put("ytdkyw"); 165 | 166 | 167 | 168 | result = newTrasa.indexMatchingQuery(vertex.getName(),graph_byIndex); 169 | 170 | // result = newTrasa.indexMatchingQuery(obj.getJSONObject("info").getJSONObject("node").getString("name")); 171 | 172 | break; 173 | case "checkById": 174 | 175 | 176 | 177 | result=newTrasa.getGraphById(Long.parseLong(obj.getString("id")),Integer.parseInt(obj.getString("depth"))); 178 | 179 | break; 180 | case "addNode": 181 | //新增节点 182 | 183 | result = newTrasa.addVertex(vertex); 184 | 185 | break; 186 | 187 | 188 | case "deleteNode": 189 | //删除节点 190 | 191 | result = newTrasa.deleteVertex(vertex); 192 | 193 | 194 | break; 195 | case "changeNode": 196 | //修改节点 197 | 198 | result = newTrasa.changeVertex(vertex); 199 | 200 | 201 | break; 202 | 203 | case "addEdge": 204 | //新增边 205 | 206 | 207 | result = newTrasa.addEgde(edge); 208 | break; 209 | 210 | 211 | case "changeEdge": 212 | //修改边 213 | 214 | 215 | result = newTrasa.changeEgde(edge); 216 | 217 | break; 218 | case "deleteEdge": 219 | //删除边 220 | 221 | 222 | result = newTrasa.deleteEgde(edge); 223 | 224 | break; 225 | 226 | case "addNodeEdge": 227 | //新增边和终点 228 | 229 | result = newTrasa.addNodeEdge(vertex,edge); 230 | 231 | break; 232 | case "queryNodeDetail": 233 | //查看点的详情 234 | 235 | result = newTrasa.queryNodeDetail(vertex); 236 | 237 | break; 238 | case "queryEdgeDetail": 239 | //查看边的详情 240 | 241 | result = newTrasa.queryEdgeDetail(edge); 242 | 243 | break; 244 | case "getEdgeAllName": 245 | //查询边的所有名称 246 | graph_Byname = obj.getJSONArray("graph"); 247 | 248 | result = newTrasa.relationshipName(edge,graph_Byname); 249 | break; 250 | default: 251 | logger.error("error type:"+type); 252 | result = "failure"; 253 | break; 254 | } 255 | logger.debug(type+"\tresult:"+result); 256 | } catch (Exception e) { 257 | logger.error(type+"\t"+e.getMessage()+"\n"+obj); 258 | } 259 | return result; 260 | } 261 | } 262 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/graph/interfaces/IGraphBuzi.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.graph.interfaces; 2 | 3 | import com.qdcz.entity.Edge; 4 | import com.qdcz.entity.IGraphEntity; 5 | import com.qdcz.entity.Vertex; 6 | import org.json.JSONObject; 7 | import org.neo4j.driver.v1.types.Path; 8 | 9 | import java.util.List; 10 | import java.util.Map; 11 | 12 | /** 13 | * 每个图对外提供的操作业务 14 | * Created by star on 17-8-3. 15 | */ 16 | public interface IGraphBuzi { 17 | 18 | /** 19 | * 批量文件增点 20 | * @param label 21 | * @param filepath 22 | * @return 23 | */ 24 | public Map batchInsertVertex(String label,String filepath); 25 | 26 | public Map batchInsertEdgeById(String relatinship,String filepath); 27 | /** 28 | * 批量文件增边 29 | * @param relatinship 30 | * @param filepath 31 | * @return 32 | */ 33 | public Map batchInsertEdge(String relatinship,String filepath); 34 | /** 35 | * 添加点 36 | * @param vertex 37 | * @return 38 | */ 39 | public String addVertex(Vertex vertex); 40 | 41 | /** 42 | * 无向搜索 43 | * @param vertex 44 | * @param depth 45 | * @return 46 | */ 47 | public List directedBfExtersion(Vertex vertex, int depth); 48 | 49 | /** 50 | * 修改点 51 | * @param vertex 52 | * @return 53 | */ 54 | public String changeVertex(Vertex vertex); 55 | 56 | 57 | /** 58 | *删除点 59 | */ 60 | public List deleteVertex(Vertex vertex); 61 | 62 | 63 | /** 64 | * 新增边 65 | * @param edge 66 | * @return 67 | */ 68 | public String addEdges(Edge edge); 69 | 70 | 71 | /** 72 | * 修改边 73 | * @param edge 74 | * @return 75 | */ 76 | public String changeEdge(Edge edge); 77 | 78 | /** 79 | * 删除边 80 | * @param edge 81 | * @return 82 | */ 83 | public String deleteEdge(Edge edge); 84 | 85 | /** 86 | * 根据节点查询广搜查询 87 | * @param vertex 88 | * @param depth 89 | * @return 90 | */ 91 | public List bfExtersion(Vertex vertex,int depth); 92 | /** 93 | * 深度优先遍历 94 | * @param fromId 95 | * @param toId 96 | * @return 97 | */ 98 | public Path dfExection(long fromId, long toId, int depth); 99 | 100 | 101 | /** 102 | * 根据identity查询系统点信息 103 | * @param label 104 | * @param identity 105 | */ 106 | public Vertex checkVertexByIdentity(String label,String identity); 107 | /** 108 | * 根据 id与广搜结果 109 | * @param id 110 | * @param depth 111 | * @return 112 | */ 113 | public List checkGraphById(long id,int depth); 114 | /** 115 | * 根据边id查询边首尾点 116 | * @param id 117 | * 118 | */ 119 | public Map checkVertexByEdgeId(long id); 120 | /** 121 | * 根据边类型返回包含名称 122 | * @param relationshipType 123 | * 124 | */ 125 | public List relationshipName(String relationshipType); 126 | } 127 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/graph/interfaces/IGraphDAO.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.graph.interfaces; 2 | 3 | import com.qdcz.entity.Edge; 4 | import com.qdcz.entity.IGraphEntity; 5 | import com.qdcz.entity.Vertex; 6 | import org.json.JSONObject; 7 | import org.neo4j.driver.v1.types.Path; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * 图谱操作类接口 13 | * Created by star on 17-8-2. 14 | */ 15 | public interface IGraphDAO { 16 | 17 | /** 18 | * 添加点 19 | * @param vertex 20 | * @return 21 | */ 22 | public String addVertex(Vertex vertex); 23 | 24 | 25 | /** 26 | * 修改点 27 | * @param vertex 28 | * @return 29 | */ 30 | public String changeVertex(Vertex vertex); 31 | 32 | 33 | /** 34 | *删除点 35 | */ 36 | public List deleteVertex(Vertex vertex); 37 | 38 | 39 | /** 40 | * 新增边 41 | * @param edge 42 | * @return 43 | */ 44 | public String addEdges(Edge edge); 45 | 46 | 47 | /** 48 | * 修改边 49 | * @param edge 50 | * @return 51 | */ 52 | public String changeEdge(Edge edge); 53 | 54 | /** 55 | * 删除边 56 | * @param edge 57 | * @return 58 | */ 59 | public String deleteEdge(Edge edge); 60 | 61 | /** 62 | * 根据节点查询广搜查询 63 | * @param vertex 64 | * @param depth 65 | * @return 66 | */ 67 | public List bfExtersion(Vertex vertex,int depth,boolean direct) throws Exception; 68 | /** 69 | * 深度优先遍历 70 | * @param fromId 71 | * @param toId 72 | * @return 73 | */ 74 | public Path dfExection(long fromId, long toId, int depth); 75 | /** 76 | * 根据identity查询系统点信息 77 | * @param label 78 | * @param identity 79 | */ 80 | public Vertex checkVertexByIdentity(String label,String identity); 81 | /** 82 | * 根据 id与广搜结果 83 | * @param id 84 | * @param depth 85 | * @return 86 | */ 87 | public List checkGraphById(long id,int depth); 88 | 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/graph/neo4jcypher/conf/Neo4jConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.graph.neo4jcypher.conf; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.Properties; 6 | 7 | /** 8 | * Created by hadoop on 17-8-3. 9 | */ 10 | public class Neo4jConfiguration extends Properties { 11 | 12 | public static String driver; 13 | public static String url; 14 | public static String name; 15 | public static String pass; 16 | 17 | 18 | @Override 19 | public synchronized void load(InputStream inStream) throws IOException { 20 | super.load(inStream); 21 | driver = this.getProperty("driver"); 22 | url = this.getProperty("url"); 23 | name = this.getProperty("name"); 24 | pass = this.getProperty("pass"); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/graph/neo4jcypher/connect/Neo4jClientFactory.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.graph.neo4jcypher.connect; 2 | 3 | import com.qdcz.graph.neo4jcypher.conf.Neo4jConfiguration; 4 | import org.neo4j.driver.v1.AuthTokens; 5 | import org.neo4j.driver.v1.Driver; 6 | import org.neo4j.driver.v1.GraphDatabase; 7 | 8 | /** 9 | * Created by hadoop on 17-8-3. 10 | */ 11 | public class Neo4jClientFactory { 12 | public static Driver create(){ 13 | Driver driver =GraphDatabase.driver( Neo4jConfiguration.url, AuthTokens.basic( Neo4jConfiguration.name, Neo4jConfiguration.pass ) ); 14 | 15 | return driver; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/graph/neo4jcypher/service/Neo4jCYService.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.graph.neo4jcypher.service; 2 | 3 | import com.mongodb.util.JSON; 4 | import com.qdcz.common.CommonTool; 5 | import com.qdcz.conf.LoadConfigListener; 6 | import com.qdcz.entity.Edge; 7 | import com.qdcz.entity.IGraphEntity; 8 | import com.qdcz.entity.Vertex; 9 | import com.qdcz.graph.interfaces.IGraphBuzi; 10 | import com.qdcz.graph.neo4jcypher.connect.Neo4jClientFactory; 11 | import com.qdcz.graph.neo4jcypher.dao.Neo4jCYDAO;; 12 | import jxl.Workbook; 13 | import jxl.format.VerticalAlignment; 14 | import jxl.read.biff.BiffException; 15 | import jxl.write.*; 16 | import org.json.JSONArray; 17 | import org.json.JSONObject; 18 | 19 | import org.neo4j.driver.v1.Driver; 20 | import org.neo4j.driver.v1.Record; 21 | import org.neo4j.driver.v1.StatementResult; 22 | import org.neo4j.driver.v1.Value; 23 | import org.neo4j.driver.v1.types.Node; 24 | import org.neo4j.driver.v1.types.Path; 25 | import org.neo4j.driver.v1.types.Relationship; 26 | import org.springframework.stereotype.Service; 27 | 28 | import java.io.File; 29 | import java.io.IOException; 30 | import java.text.DecimalFormat; 31 | import java.util.*; 32 | 33 | /** 34 | * cypher语句,neo4j对外提供的操作 35 | * Created by star on 17-8-3. 36 | */ 37 | @Service("neo4jCypherService") 38 | public class Neo4jCYService implements IGraphBuzi { 39 | 40 | public static void main(String[] args) { 41 | 42 | LoadConfigListener loadConfigListener=new LoadConfigListener(); 43 | loadConfigListener.setSource_dir("/dev/"); 44 | loadConfigListener.contextInitialized(null); 45 | Vertex vertex=new Vertex(); 46 | vertex.setRoot("起点"); 47 | vertex.setName("牵头行"); 48 | vertex.setType("挖掘部"); 49 | vertex.setId("55"); 50 | vertex.setContent(""); 51 | vertex.setLabel("ytdk_label"); 52 | 53 | Neo4jCYService instance= new Neo4jCYService(); 54 | // instance.deleteVertex(vertex); 55 | Edge edge=new Edge(); 56 | edge.setRelationShip("gra"); 57 | edge.setId(2181l+""); 58 | instance.relationshipName("licom_relationship"); 59 | // instance.bfExtersion(vertex,1); 60 | // instance.dfExection(19,22,4); 61 | // instance.batchInsertEdge("hehe_rel","edge.csv"); 62 | // instance.batchInsertVertex("hehe","vertex.csv"); 63 | 64 | } 65 | 66 | 67 | 68 | private Neo4jCYDAO neo4jCYDAO; 69 | 70 | private Driver driver; 71 | 72 | public Neo4jCYService(){ 73 | driver = Neo4jClientFactory.create(); 74 | neo4jCYDAO = new Neo4jCYDAO(driver); 75 | } 76 | @Override 77 | public String addVertex(Vertex vertex) { 78 | 79 | return neo4jCYDAO.addVertex(vertex); 80 | } 81 | 82 | @Override 83 | public String changeVertex(Vertex vertex) { 84 | 85 | return neo4jCYDAO.changeVertex(vertex); 86 | } 87 | public Map batchInsertVertex(String label,String filepath){ 88 | Map mapsResult=new HashMap<>(); 89 | String sql=null; 90 | sql="USING PERIODIC COMMIT 1000 " + 91 | "LOAD CSV WITH HEADERS FROM \"file:///" + filepath + "\" AS line " + 92 | "MERGE (p:"+label+"{root:line.root,name:line.name,type:line.type,content:line.content,identity:line.identity}) return line.identity,id(p)"; 93 | 94 | StatementResult execute = neo4jCYDAO.execute(sql); 95 | while(execute.hasNext()){ 96 | Record next = execute.next(); 97 | String id = next.get("id(p)").toString(); 98 | String identity = next.get("line.identity").asString(); 99 | mapsResult.put(identity,id); 100 | } 101 | return mapsResult; 102 | } 103 | public Map batchInsertEdge(String relatinship,String filepath){ 104 | Map mapsResult=new HashMap<>(); 105 | String sql=null; 106 | sql="USING PERIODIC COMMIT 1000 " + 107 | "LOAD CSV WITH HEADERS FROM \"file:///"+filepath+"\" AS line " + 108 | "MATCH (m{identity:line.from_id} ) MATCH (n{identity:line.to_id}) " + 109 | "MERGE (m)-[r:"+relatinship+"{from:id(m),root:line.root,name:line.name,to:id(n)}]->(n) return line.identity,id(r);"; 110 | StatementResult execute = neo4jCYDAO.execute(sql); 111 | while(execute.hasNext()){ 112 | Record next = execute.next(); 113 | String id = next.get("id(r)").toString(); 114 | String identity = next.get("line.identity").toString(); 115 | mapsResult.put(identity,id); 116 | } 117 | return mapsResult; 118 | } 119 | public Map batchInsertEdgeById(String relatinship,String filepath){ 120 | Map mapsResult=new HashMap<>(); 121 | String sql=null; 122 | sql="USING PERIODIC COMMIT 1000 " + 123 | "LOAD CSV WITH HEADERS FROM \"file:///"+filepath+"\" AS line " + 124 | "MATCH (m ) MATCH (n ) where id(m)=apoc.number.parseInt(line.from_id) AND id(n)=apoc.number.parseInt(line.to_id) "+ 125 | "MERGE (m)-[r:"+relatinship+"{from:id(m),root:line.root,name:line.name,to:id(n),weight:apoc.number.parseInt(line.weight)}]->(n) return line.identity,id(r);"; 126 | System.out.println(sql); 127 | StatementResult execute = neo4jCYDAO.execute(sql); 128 | while(execute.hasNext()){ 129 | Record next = execute.next(); 130 | String id = next.get("id(r)").toString(); 131 | String identity = next.get("line.identity").asString(); 132 | mapsResult.put(identity,id); 133 | } 134 | return mapsResult; 135 | } 136 | @Override 137 | public List deleteVertex(Vertex vertex) { 138 | return neo4jCYDAO.deleteVertex(vertex); 139 | } 140 | 141 | @Override 142 | public String addEdges(Edge edge) { 143 | return neo4jCYDAO.addEdges(edge); 144 | } 145 | 146 | @Override 147 | public String changeEdge(Edge edge) { 148 | return neo4jCYDAO.changeEdge(edge); 149 | } 150 | 151 | @Override 152 | public String deleteEdge(Edge edge) { 153 | return neo4jCYDAO.deleteEdge(edge); 154 | } 155 | 156 | @Override 157 | public List directedBfExtersion(Vertex vertex, int depth){ 158 | List paths =new ArrayList<>(); 159 | try { 160 | paths = neo4jCYDAO.bfExtersion(vertex, depth,true); 161 | } catch (Exception e) { 162 | e.printStackTrace(); 163 | } 164 | return paths; 165 | } 166 | 167 | 168 | @Override 169 | public List bfExtersion(Vertex vertex, int depth) { 170 | List paths =new ArrayList<>(); 171 | try { 172 | paths = neo4jCYDAO.bfExtersion(vertex, depth,false); 173 | } catch (Exception e) { 174 | e.printStackTrace(); 175 | } 176 | return paths; 177 | } 178 | 179 | @Override 180 | public Path dfExection(long fromId, long toId, int depth) { 181 | return neo4jCYDAO.dfExection(fromId,toId,depth); 182 | 183 | } 184 | 185 | @Override 186 | public Vertex checkVertexByIdentity(String label, String identity) { 187 | return neo4jCYDAO.checkVertexByIdentity(label,identity); 188 | } 189 | @Override 190 | public List checkGraphById(long id,int depth) { 191 | 192 | return neo4jCYDAO.checkGraphById(id,depth); 193 | } 194 | @Override 195 | public Map checkVertexByEdgeId(long id) { 196 | Map result=new HashMap<>(); 197 | String sqlString="MATCH (p)-[r]->(m)WHERE id(r)= "+id+" RETURN p,m"; 198 | StatementResult execute = neo4jCYDAO.execute(sqlString); 199 | while(execute.hasNext()){ 200 | Record next = execute.next(); 201 | Node n = next.get("p").asNode(); 202 | Map nodeInfo = n.asMap(); 203 | Vertex startVertex=new Vertex(); 204 | CommonTool.transMap2Bean(nodeInfo,startVertex); 205 | startVertex.setId(n.id()+""); 206 | if(!result.containsKey("start")){ 207 | result.put("start",startVertex); 208 | } 209 | Node m = next.get("m").asNode(); 210 | nodeInfo = m.asMap(); 211 | Vertex endVertex=new Vertex(); 212 | CommonTool.transMap2Bean(nodeInfo,endVertex); 213 | endVertex.setId(m.id()+""); 214 | if(!result.containsKey("end")){ 215 | result.put("end",endVertex); 216 | } 217 | 218 | } 219 | 220 | return result; 221 | } 222 | public List relationshipName(String relationshipType){ 223 | List result=new ArrayList<>(); 224 | String sql="match()-[r:"+relationshipType+"]->() return distinct(r.name)as names limit 100"; 225 | StatementResult execute = neo4jCYDAO.execute(sql); 226 | while(execute.hasNext()){ 227 | Record next = execute.next(); 228 | String names = next.get("names").asString(); 229 | result.add(names); 230 | } 231 | System.out.println(result.toString()+result.size()); 232 | return result; 233 | } 234 | 235 | 236 | 237 | 238 | 239 | 240 | public void testExtersion(String sql){ 241 | StatementResult execute = neo4jCYDAO.execute(sql); 242 | } 243 | 244 | } 245 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/graph/tools/ResultBuilder.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.graph.tools; 2 | 3 | import com.qdcz.common.CommonTool; 4 | import com.qdcz.entity.Edge; 5 | import com.qdcz.entity.Vertex; 6 | import org.json.JSONArray; 7 | import org.json.JSONObject; 8 | import org.neo4j.driver.v1.types.Node; 9 | import org.neo4j.driver.v1.types.Path; 10 | import org.neo4j.driver.v1.types.Relationship; 11 | 12 | 13 | import java.util.*; 14 | 15 | /** 16 | * Created by hadoop on 17-6-23. 17 | * Result set operations entity 18 | */ 19 | public class ResultBuilder { 20 | //对结果集去重组合 21 | public Map> cleanRestult(Set parsePaths){ 22 | Map> resultPaths=new HashMap(); 23 | if(parsePaths!=null){ 24 | for(Path path:parsePaths){ 25 | Node start = path.start(); 26 | Vertex startVertex=new Vertex(); 27 | CommonTool.transMap2Bean(start.asMap(),startVertex); 28 | Node end = path.end(); 29 | Vertex endVertex=new Vertex(); 30 | CommonTool.transMap2Bean(end.asMap(),endVertex); 31 | List relationships = (List) path.relationships(); 32 | if(relationships==null||relationships.size()==0){ 33 | continue; 34 | } 35 | Relationship relationship = relationships.get(relationships.size() - 1); 36 | String key=startVertex.getName()+"--"+relationship.get("name").asString(); 37 | String content=end.get("content").asString(); 38 | String value=null; 39 | if(!"".equals(content)){ 40 | value=endVertex.toJSON().toString(); 41 | }else { 42 | value = endVertex.getName(); 43 | } 44 | if(resultPaths.containsKey(key)){ 45 | Vector strs=resultPaths.get(key); 46 | strs.add(value); 47 | }else{ 48 | Vector strs=new Vector<>(); 49 | strs.add(value); 50 | resultPaths.put(key,strs); 51 | } 52 | }} 53 | 54 | return resultPaths; 55 | } 56 | // //获取所需结果集 57 | public JSONObject graphResult(List paths ){ 58 | JSONArray nodesJarry=new JSONArray(); 59 | JSONArray edgesJarry=new JSONArray(); 60 | Set nodeIds=new HashSet<>(); 61 | Set edgeIds=new HashSet<>(); 62 | JSONObject centreNodeObj =null; 63 | JSONObject result =new JSONObject(); 64 | if(paths!=null) { 65 | for (Path path : paths) { 66 | Iterable nodes = path.nodes(); 67 | for (Node node : nodes) { 68 | Map nodeInfo = node.asMap(); 69 | String label = node.labels().iterator().next(); 70 | Vertex newVertex = new Vertex(); 71 | CommonTool.transMap2Bean(nodeInfo, newVertex); 72 | newVertex.setId(node.id() + ""); 73 | newVertex.setLabel(label); 74 | if (!nodeIds.contains(newVertex.getGraphId())) { 75 | JSONObject resultobj = newVertex.toJSON(); 76 | resultobj.put("id", newVertex.getId()); 77 | resultobj.put("label", newVertex.getLabel()); 78 | if (centreNodeObj == null) { 79 | centreNodeObj = resultobj; 80 | } 81 | resultobj.put("content",""); 82 | nodesJarry.put(resultobj); 83 | } 84 | nodeIds.add(node.id() + ""); 85 | } 86 | Iterable relationships = path.relationships(); 87 | for (Relationship relationship : relationships) { 88 | 89 | Map edgeInfo = relationship.asMap(); 90 | Edge newEdge = new Edge(); 91 | CommonTool.transMap2Bean(edgeInfo, newEdge); 92 | newEdge.setId(relationship.id() + ""); 93 | newEdge.setRelationShip(relationship.type()); 94 | if (!edgeIds.contains(newEdge.getGraphId())) { 95 | JSONObject resultobj = newEdge.toJSON(); 96 | resultobj.put("id", newEdge.getId()); 97 | resultobj.put("relationship", newEdge.getRelationShip()); 98 | edgesJarry.put(resultobj); 99 | } 100 | edgeIds.add(relationship.id() + ""); 101 | } 102 | } 103 | nodeIds.clear(); 104 | edgeIds.clear(); 105 | String center = ""; 106 | if (centreNodeObj != null) { 107 | center = centreNodeObj.getString("id"); 108 | } else { 109 | return result; 110 | } 111 | result.put("nodes", nodesJarry); 112 | result.put("edges", edgesJarry); 113 | result.put("center", center); 114 | } 115 | return result; 116 | } 117 | 118 | //两个结果集合并 119 | public JSONObject mergeResult(JSONObject obj1, JSONObject obj2){ 120 | JSONObject jsonObject = new JSONObject(); 121 | 122 | if(!obj1.has("nodes")){ 123 | obj1.put("nodes",new JSONArray()); 124 | } 125 | if(!obj1.has("edges")){ 126 | obj1.put("edges",new JSONArray()); 127 | } 128 | if(!obj2.has("nodes")){ 129 | obj2.put("nodes",new JSONArray()); 130 | } 131 | if(!obj2.has("edges")){ 132 | obj2.put("edges",new JSONArray()); 133 | } 134 | if(obj1.has("center")){ 135 | jsonObject.put("center",obj1.getString("center")); 136 | } 137 | if(obj2.has("center")){ 138 | jsonObject.put("center",obj2.getString("center")); 139 | } 140 | jsonObject.put("nodes", mergeArray( obj1.getJSONArray("nodes"),obj2.getJSONArray("nodes"))); 141 | 142 | 143 | jsonObject.put("edges", mergeArray( obj1.getJSONArray("edges"),obj2.getJSONArray("edges"))); 144 | 145 | return jsonObject; 146 | } 147 | //合并两个jsonarray 148 | public JSONArray mergeArray(JSONArray a1,JSONArray a2){ 149 | for(int i=0;i(); 159 | for(int i=0;i key_value) throws Exception { 57 | BulkRequestBuilder bulkRequest = client.prepareBulk(); 58 | Scanner sc = new Scanner(new File(path)); 59 | int count = 1; 60 | while(sc.hasNext()){ 61 | String line = sc.nextLine(); 62 | JSONObject obj = new JSONObject(line); 63 | String identity = obj.getString("identity"); 64 | if(type.contains("relationship")){ 65 | obj.remove("identity"); 66 | } 67 | 68 | bulkRequest.add(client.prepareIndex(index,type).setId(key_value.get(identity)).setSource(obj.toString())); 69 | if (count%1000==0) { 70 | BulkResponse bulkResponse = bulkRequest.execute().actionGet(); 71 | if (bulkResponse.hasFailures()){ 72 | System.out.println("Bulk add index failures"+bulkResponse.buildFailureMessage()); 73 | throw new Exception("批量导入elasearch失败"+type); 74 | } 75 | System.out.println("1000"); 76 | } 77 | count++; 78 | } 79 | bulkRequest.execute().actionGet(); 80 | } 81 | 82 | @Override 83 | public void bulkIndex(IGraphEntity... entities) { 84 | if(entities.length == 0){ 85 | return; 86 | } 87 | 88 | BulkRequestBuilder bulkRequestBuilder = client.prepareBulk(); 89 | for (int i = 0; i < entities.length; i++){ 90 | JSONObject obj=entities[i].toJSON(); 91 | bulkRequestBuilder.add(client.prepareIndex(index, entities[i].getGraphType()) 92 | .setId(entities[i].getGraphId()) 93 | .setSource(obj.toString())); 94 | 95 | if(i%20==1 && i!=1){ 96 | BulkResponse bulkResponse = bulkRequestBuilder.get(); 97 | if (bulkResponse.hasFailures()){ 98 | System.out.println("Bulk add index failures"+bulkResponse.buildFailureMessage()); 99 | } 100 | bulkRequestBuilder = client.prepareBulk(); 101 | } 102 | } 103 | 104 | BulkResponse bulkResponse = bulkRequestBuilder.get(); 105 | if (bulkResponse.hasFailures()){ 106 | System.out.println("Bulk add index failures"+bulkResponse.buildFailureMessage()); 107 | } 108 | } 109 | 110 | @Override 111 | public void bulkDelete(IGraphEntity... entities) { 112 | if(entities.length==0){ 113 | return; 114 | } 115 | for (IGraphEntity entity:entities){ 116 | DeleteResponse response = client.prepareDelete(index, entity.getGraphType(), entity.getGraphId()) 117 | .get(); 118 | 119 | } 120 | } 121 | 122 | @Override 123 | public JSONObject queryById(IGraphEntity queryEctity){ 124 | QueryBuilder matchQuery = QueryBuilders.idsQuery().addIds(queryEctity.getGraphId()); 125 | 126 | SearchResponse response = client.prepareSearch(index ).setTypes(queryEctity.getGraphType()) 127 | .setQuery(matchQuery) 128 | .execute().actionGet(); 129 | 130 | 131 | SearchHits searchHits = response.getHits(); 132 | //遍历结果 133 | 134 | JSONObject result = null; 135 | if(searchHits.totalHits>0){ 136 | SearchHit hit = searchHits.iterator().next(); 137 | JSONObject source = new JSONObject(hit.getSourceAsString()); 138 | source.put("_id",hit.getId()); 139 | source.put("score",hit.getScore()); 140 | 141 | source.remove("name_length"); 142 | 143 | 144 | result =source; 145 | } 146 | 147 | return result; 148 | } 149 | 150 | 151 | @Override 152 | public Map queryByName(String graphtype, String name,int range_low,int range_high,int size) { 153 | QueryBuilder matchQuery = QueryBuilders 154 | .boolQuery() 155 | .must(QueryBuilders.matchQuery("name",name)) 156 | .must(QueryBuilders.rangeQuery("name_length").gt(range_low).lt(range_high)); 157 | 158 | // 搜索数据 159 | SearchResponse response = client.prepareSearch(index).setTypes(graphtype).setSearchType(SearchType.DFS_QUERY_THEN_FETCH) 160 | .setQuery(matchQuery).setSize(size) 161 | .execute().actionGet(); 162 | 163 | 164 | int page=1; 165 | int pagesize=10; 166 | // 搜索数据(分页) 167 | 168 | //获取查询结果集 169 | SearchHits searchHits = response.getHits(); 170 | //System.out.println(searchHits.totalHits); 171 | Map result= new HashMap<>(); 172 | //遍历结果 173 | for(SearchHit hit:searchHits){ 174 | JSONObject source = new JSONObject(hit.getSourceAsString()); 175 | source.put("id",hit.getId()); 176 | source.put("score",hit.getScore()); 177 | source.remove("name_length"); 178 | 179 | result.put(hit.getId(),source); 180 | 181 | } 182 | 183 | // System.out.println("共搜到:"+result.size()+"条结果!"); 184 | // System.out.println(result); 185 | return result; 186 | } 187 | 188 | 189 | public List queryAllMatch(IGraphEntity queryEntity) { 190 | 191 | // JSONObject queryJson = queryEntity.toQueryJSON(); 192 | 193 | 194 | 195 | JSONObject queryJson = new JSONObject(); 196 | queryJson.put("root","保险管理"); 197 | 198 | 199 | QueryBuilder matchQuery = null; 200 | 201 | 202 | // matchQuery = QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must(QueryBuilders.termQuery("from", "完善组织架构")); 203 | 204 | matchQuery = QueryBuilders.queryStringQuery("\"完善组织架构\"").field("from"); 205 | 206 | 207 | 208 | // matchQuery = QueryBuilders.termQuery("from", "完善组织架构"); 209 | 210 | // matchQuery = QueryBuilders.matchQuery("root", "保险管理"); 211 | // matchQuery = QueryBuilders.termQuery("root", "保险管理"); 212 | // matchQuery = QueryBuilders.regexpQuery("root", "保险资金"); 213 | // BoolQueryBuilder matchQuery = QueryBuilders.boolQuery(); 214 | // matchQuery.must(QueryBuilders.matchAllQuery()); 215 | 216 | 217 | 218 | 219 | // for (Object key :queryJson.keySet()){ 220 | // matchQuery.must(QueryBuilders.matchPhraseQuery(key.toString(), queryJson.getString(key.toString()))); 221 | // } 222 | 223 | // 搜索数据 224 | SearchResponse response = client.prepareSearch("graph" ).setTypes("edges") 225 | .setQuery(matchQuery) 226 | .execute().actionGet(); 227 | 228 | 229 | int page=1; 230 | int pagesize=10; 231 | // 搜索数据(分页) 232 | 233 | //获取查询结果集 234 | SearchHits searchHits = response.getHits(); 235 | System.out.println(searchHits.totalHits); 236 | List result= new ArrayList<>(); 237 | //遍历结果 238 | for(SearchHit hit:searchHits){ 239 | JSONObject source = new JSONObject(hit.getSourceAsString()); 240 | source.put("_id",hit.getId()); 241 | source.put("score",hit.getScore()); 242 | 243 | 244 | result.add(source.toString()); 245 | 246 | 247 | 248 | 249 | 250 | } 251 | 252 | // System.out.println("共搜到:"+result.size()+"条结果!"); 253 | // System.out.println(result); 254 | return result; 255 | } 256 | 257 | } 258 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/index/elsearch/elk/ElasearchClientFactory.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.index.elsearch.elk; 2 | 3 | import com.qdcz.index.elsearch.conf.ELKConfig; 4 | import org.elasticsearch.client.transport.TransportClient; 5 | import org.elasticsearch.common.settings.Settings; 6 | import org.elasticsearch.common.transport.InetSocketTransportAddress; 7 | import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient; 8 | import org.elasticsearch.xpack.security.authc.support.SecuredString; 9 | import org.springframework.stereotype.Service; 10 | 11 | import java.net.InetAddress; 12 | import java.net.UnknownHostException; 13 | import java.util.Collections; 14 | 15 | import static org.elasticsearch.xpack.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; 16 | 17 | /** 18 | * Created by star on 17-8-3. 19 | */ 20 | @Service 21 | public class ElasearchClientFactory { 22 | public static TransportClient create(){ 23 | TransportClient client = null; 24 | 25 | String[] portStr= ELKConfig.ELKports.split(","); 26 | int portNum=portStr.length; 27 | int[] port = new int[portNum] ; 28 | for(int i=0;i key_value) throws Exception{ 68 | elasearchDAO.bluckByFile(type,path,key_value); 69 | } 70 | @Override 71 | public void addOrUpdateIndex(IGraphEntity entity) { 72 | 73 | elasearchDAO.addOrUpdateIndex(entity); 74 | } 75 | 76 | @Override 77 | public void delIndex(IGraphEntity entity) { 78 | elasearchDAO.delIndex(entity); 79 | } 80 | 81 | 82 | @Override 83 | public void bulkIndex(IGraphEntity... entities) { 84 | elasearchDAO.bulkIndex(entities); 85 | } 86 | 87 | @Override 88 | public void bulkDelete(IGraphEntity... entities) { 89 | elasearchDAO.bulkDelete(entities); 90 | } 91 | 92 | @Override 93 | public JSONObject queryById(IGraphEntity entity){ 94 | return elasearchDAO.queryById(entity); 95 | } 96 | 97 | @Override 98 | public Map queryByName(String graphtype, String name) { 99 | return elasearchDAO.queryByName(graphtype,name,0,10,15); 100 | } 101 | 102 | @Override 103 | public Map queryByName(String graphtype, String name,int range_low,int range_high) { 104 | return elasearchDAO.queryByName(graphtype,name,range_low,range_high,15); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/index/interfaces/IIndexDAO.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.index.interfaces; 2 | 3 | import com.qdcz.entity.IGraphEntity; 4 | import org.json.JSONObject; 5 | 6 | import java.util.Map; 7 | 8 | /** 9 | * 索引模块DAO接口 10 | * Created by star on 17-8-2. 11 | */ 12 | public interface IIndexDAO { 13 | 14 | 15 | public void addOrUpdateIndex(IGraphEntity entity); 16 | public void delIndex(IGraphEntity entity); 17 | 18 | public void bulkIndex(IGraphEntity... entities); 19 | 20 | public void bulkDelete(IGraphEntity... entities); 21 | 22 | 23 | public JSONObject queryById(IGraphEntity queryEctity); 24 | 25 | public Map queryByName(String graphtype, String name,int range_low,int range_high,int size); 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/index/interfaces/IIndexService.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.index.interfaces; 2 | 3 | import com.qdcz.entity.IGraphEntity; 4 | import org.json.JSONObject; 5 | 6 | import java.util.Map; 7 | 8 | /** 9 | * Created by star on 17-8-3. 10 | */ 11 | public interface IIndexService { 12 | public void addOrUpdateIndex(IGraphEntity entity); 13 | public void delIndex(IGraphEntity entity); 14 | 15 | public void bulkIndex(IGraphEntity... entities); 16 | 17 | 18 | public void bulkDelete(IGraphEntity... entities); 19 | 20 | 21 | public void bluckByFile(String type,String path,Map key_value)throws Exception; 22 | 23 | 24 | public JSONObject queryById(IGraphEntity queryEctity); 25 | 26 | /** 27 | * 根据字符串长度区间进行查询 28 | * @param graphtype 29 | * @param name 30 | * @param range_low 31 | * @param range_high 32 | * @return 33 | */ 34 | public Map queryByName(String graphtype, String name,int range_low,int range_high); 35 | 36 | @Deprecated 37 | /** 38 | * 默认为(0,10) 39 | */ 40 | public Map queryByName(String graphtype, String name); 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/mongo/ConnManager.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.mongo; 2 | 3 | import com.mongodb.MongoClient; 4 | import com.mongodb.MongoClientOptions; 5 | import com.mongodb.MongoCredential; 6 | import com.mongodb.ServerAddress; 7 | 8 | import java.util.Arrays; 9 | import java.util.HashMap; 10 | import java.util.Map; 11 | 12 | /** 13 | * Created by hadoop on 17-7-3. 14 | */ 15 | public class ConnManager { 16 | private static Map clients = null; 17 | public static MongoClient getClient(String host,String databaseName, int port,String user,String pass){ 18 | if(clients==null){ 19 | clients = new HashMap(); 20 | } 21 | if(clients.containsKey(databaseName) && clients.get(databaseName)!=null){ 22 | return clients.get(databaseName); 23 | }else{ 24 | MongoClientOptions.Builder buide = new MongoClientOptions.Builder(); 25 | buide.connectionsPerHost(100); //与目标数据库能够建立的最大connection数量为50 26 | // buide.autoConnectRetry(true); //自动重连数据库启动 27 | //如果当前所有的connection都在使用中,则每个connection上可以有50个线程排队等待 28 | buide.threadsAllowedToBlockForConnectionMultiplier(100); 29 | buide.connectionsPerHost(100);// 与目标数据库可以建立的最大链接数 30 | buide.connectTimeout(1000 * 60 * 20);// 与数据库建立链接的超时时间 31 | buide.maxWaitTime(100 * 60 * 5);// 一个线程成功获取到一个可用数据库之前的最大等待时间 32 | buide.maxConnectionIdleTime(0); 33 | buide.maxConnectionLifeTime(0); 34 | buide.socketTimeout(0); 35 | buide.socketKeepAlive(true); 36 | 37 | MongoClientOptions myOptions = buide.build(); 38 | MongoCredential credential = MongoCredential.createCredential(user, 39 | databaseName, pass.toCharArray()); 40 | MongoClient new_client = new MongoClient(new ServerAddress(host, port), 41 | Arrays.asList(credential),myOptions); 42 | // clients.put(databaseName, new_client); 43 | return new_client; 44 | } 45 | } 46 | 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/mongo/MyMongo.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.mongo; 2 | 3 | import com.qdcz.mongo.conf.MongoConfiguration; 4 | 5 | /** 6 | * Created by hadoop on 17-7-3. 7 | */ 8 | public class MyMongo extends BaseMongoDAL{ 9 | 10 | public MyMongo(String databaseName, 11 | String collectionName) { 12 | super(MongoConfiguration.host, databaseName, MongoConfiguration.port, collectionName, MongoConfiguration.name, MongoConfiguration.pass); 13 | } 14 | 15 | } 16 | 17 | -------------------------------------------------------------------------------- /src/main/java/com/qdcz/mongo/conf/MongoConfiguration.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.mongo.conf; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | import java.util.Properties; 6 | 7 | /** 8 | * Created by hadoop on 17-7-3. 9 | */ 10 | public class MongoConfiguration extends Properties { 11 | 12 | public static String host = null; 13 | 14 | public static int port; 15 | public static String name; 16 | public static String pass; 17 | public static String dbTest; 18 | public static String dbHouse; 19 | public static String dbOnline; 20 | @Override 21 | public synchronized void load(InputStream inStream) throws IOException { 22 | super.load(inStream); 23 | host = this.getProperty("mongo_host"); 24 | port = Integer.parseInt(this.getProperty("mongo_port")); 25 | name = this.getProperty("mongo_user"); 26 | pass = this.getProperty("mongo_pass"); 27 | dbTest = this.getProperty("mongo_db_test"); 28 | dbHouse = this.getProperty("mongo_db_TradeCollection"); 29 | dbOnline = this.getProperty("mongo_db_Trade"); 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/resources/application-dev.properties: -------------------------------------------------------------------------------- 1 | server.port=14000 2 | logging.config=classpath:dev/log4j2.xml 3 | source_dir=/dev/ 4 | #wnd 5 | #hanlp_path=/mnt/vol_0/wnd/ml/judou/ 6 | #star 7 | hanlp_path=/media/star/Doc/work/dictionary/ -------------------------------------------------------------------------------- /src/main/resources/application-product.properties: -------------------------------------------------------------------------------- 1 | server.port=14000 2 | logging.config=classpath:product/log4j2.xml 3 | source_dir=/product/ 4 | hanlp_path=/home/hadoop/wnd/ml/judou/ -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | spring.profiles.active=product -------------------------------------------------------------------------------- /src/main/resources/concept_rule_simple.txt: -------------------------------------------------------------------------------- 1 | #概念匹配规则 2 | 什么(叫作|叫做|叫|是)(.*) 1 3 | 何(谓|为)(.*) 1 4 | (请问){0,2}(.*)是[(怎么样的|怎样的|什么|啥){0,4}] 1 5 | (如何理解){0,4}(.*)[(的){0,1}](概念|定义|解释|含义) 1 6 | (怎么|如何)理解(.*) 1 7 | (.*)是什么意思 0 8 | (.*)(是指|介绍|简介)(什么){0,2} 0 9 | (.*)(是|叫|指)什么 0 10 | (.*) 0 11 | -------------------------------------------------------------------------------- /src/main/resources/dev/database.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ytdkyw 6 | 银团贷款业务 7 | 8 | ytdk_relationship 9 | 10 | 11 | 12 | shkx 13 | 社会科学知识库 14 | 15 | shkx_relationship 16 | 17 | 18 | licom 19 | 上市公司分析 20 | 21 | licom_relationship 22 | 23 | 24 | up_2013 25 | 2013上市公司担保分析 26 | 27 | up_2013_relationship 28 | 29 | 30 | down_2013 31 | 2013上市公司担保分析 32 | 33 | down_2013_relationship 34 | 35 | 36 | up_2014 37 | 2014上市公司担保分析 38 | 39 | up_2014_relationship 40 | 41 | 42 | down_2014 43 | 2014上市公司担保分析 44 | 45 | down_2014_relationship 46 | 47 | 48 | up_2015 49 | 2015上市公司担保分析 50 | 51 | up_2015_relationship 52 | 53 | 54 | down_2015 55 | 2015上市公司担保分析 56 | 57 | down_2015_relationship 58 | 59 | 60 | up_2016 61 | 2016上市公司担保分析 62 | 63 | up_2016_relationship 64 | 65 | 66 | down_2016 67 | 2016上市公司担保分析 68 | 69 | down_2016_relationship 70 | 71 | 72 | up_2017 73 | 2017上市公司担保分析 74 | 75 | up_2017_relationship 76 | 77 | 78 | 79 | 80 | 81 | 82 | xz 83 | 84 | ytdkyw 85 | 86 | 87 | 88 | sk 89 | 90 | shkx 91 | 92 | 93 | 94 | all 95 | 96 | ytdkyw 97 | shkx 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /src/main/resources/dev/elasearch.properties: -------------------------------------------------------------------------------- 1 | ELKcluster=star_ela 2 | ELKuser=elastic 3 | ELKpasswd=123qdcz$%^ 4 | ELKhost=192.168.2.9 5 | ELKports=9300,9301 6 | ELKindex=graph -------------------------------------------------------------------------------- /src/main/resources/dev/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ${sys:user.home}/tmp 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 26 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 42 | 43 | 44 | 45 | 46 | 48 | 49 | 50 | 51 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /src/main/resources/dev/mongo.properties: -------------------------------------------------------------------------------- 1 | mongo_host=h131 2 | mongo_port=27018 3 | mongo_user=sjcjb 4 | mongo_pass=hpre&-*123 5 | mongo_db_test=test 6 | mongo_db_TradeCollection=TradeCollection 7 | mongo_db_Trade=Trade -------------------------------------------------------------------------------- /src/main/resources/dev/neo4j.properties: -------------------------------------------------------------------------------- 1 | #driver=org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver 2 | driver=org.neo4j.jdbc.Driver 3 | #driver=org.neo4j.ogm.drivers.bolt.driver.BoltDriver 4 | #db=file:///mnt/vol_0/neo4j-community-3.1.1/data/databases/graph.db 5 | #db=file:///home/star/tmp/graph.db 6 | #db=http://wnd:wangniasd1@localhost:7474 7 | port=14000 8 | project=law;zhiku 9 | 10 | #local 11 | river=org.neo4j.jdbc.Driver 12 | url=bolt://192.168.2.149:7687 13 | name=wnd 14 | pass=wangniasd1 15 | #url=bolt://192.168.2.54:7687 16 | #name=neo4j 17 | #pass=admin123 18 | -------------------------------------------------------------------------------- /src/main/resources/product/database.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ytdkyw 6 | 银团贷款业务 7 | 8 | ytdk_relationship 9 | 10 | 11 | 12 | shkx 13 | 社会科学知识库 14 | 15 | shkx_relationship 16 | 17 | 18 | wikidata 19 | wiki图谱 20 | 21 | wikidata_relationship 22 | 23 | 24 | 25 | businessinfo 26 | 工商图谱 27 | 28 | businessinfo_relationship 29 | 30 | 31 | 32 | 33 | 34 | xz 35 | 36 | ytdkyw 37 | 38 | 39 | 40 | sk 41 | 42 | shkx 43 | 44 | 45 | 46 | wiki 47 | 48 | wikidata 49 | 50 | 51 | 52 | businessinfo 53 | 54 | businessinfo 55 | 56 | 57 | 58 | all 59 | 60 | ytdkyw 61 | shkx 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /src/main/resources/product/elasearch.properties: -------------------------------------------------------------------------------- 1 | ELKcluster=my-application 2 | ELKuser=elastic 3 | ELKpasswd=123qdcz$%^ 4 | ELKhost=h134 5 | ELKports=9300,9301 6 | ELKindex=graph -------------------------------------------------------------------------------- /src/main/resources/product/log4j2.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ${sys:user.home}/wnd/tmp 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 26 | 28 | 29 | 30 | 31 | 32 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 42 | 43 | 44 | 45 | 46 | 48 | 49 | 50 | 51 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /src/main/resources/product/mongo.properties: -------------------------------------------------------------------------------- 1 | mongo_host=h131 2 | mongo_port=27018 3 | mongo_user=sjcjb 4 | mongo_pass=hpre&-*123 5 | mongo_db_test=test 6 | mongo_db_TradeCollection=TradeCollection 7 | mongo_db_Trade=Trade -------------------------------------------------------------------------------- /src/main/resources/product/neo4j.properties: -------------------------------------------------------------------------------- 1 | driver=org.neo4j.jdbc.Driver 2 | url=bolt://h134:7687 3 | name=wnd 4 | pass=wangniasd1 5 | -------------------------------------------------------------------------------- /src/test/test/com/qdcz/conf/TestLoadConfigListener.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.conf; 2 | 3 | import com.qdcz.conf.DatabaseConfiguration; 4 | import com.qdcz.conf.LoadConfigListener; 5 | import com.qdcz.graph.neo4jcypher.conf.Neo4jConfiguration; 6 | import com.qdcz.index.elsearch.conf.ELKConfig; 7 | import com.qdcz.mongo.conf.MongoConfiguration; 8 | import org.springframework.context.ApplicationContext; 9 | import org.springframework.context.support.ClassPathXmlApplicationContext; 10 | import org.springframework.test.context.TestContext; 11 | import org.springframework.test.context.TestExecutionListener; 12 | import org.springframework.web.context.support.XmlWebApplicationContext; 13 | 14 | import java.io.IOException; 15 | 16 | /** 17 | * Created by star on 17-8-9. 18 | */ 19 | public class TestLoadConfigListener implements TestExecutionListener { 20 | private String source_dir = "/dev/"; 21 | @Override 22 | public void beforeTestClass(TestContext testContext) throws Exception { 23 | 24 | } 25 | 26 | @Override 27 | public void prepareTestInstance(TestContext testContext) throws Exception { 28 | try { 29 | //------------加载neo4j的配置------------ 30 | System.out.println("------------加载neo4j的配置------------"); 31 | Neo4jConfiguration neo4jConfiger=new Neo4jConfiguration(); 32 | neo4jConfiger.load(LoadConfigListener.class.getResourceAsStream(source_dir+"neo4j.properties")); 33 | 34 | System.out.println("------------加载MongoDB配置文件------------"); 35 | MongoConfiguration mongoConf = new MongoConfiguration(); 36 | mongoConf.load(LoadConfigListener.class.getResourceAsStream(source_dir+"mongo.properties")); 37 | 38 | System.out.println("------------加载ElaSearch配置文件------------"); 39 | ELKConfig elkConfig = new ELKConfig(); 40 | elkConfig.load(LoadConfigListener.class.getResourceAsStream(source_dir+"elasearch.properties")); 41 | 42 | System.out.println("------------加载Database配置文件------------"); 43 | DatabaseConfiguration databaseConf = new DatabaseConfiguration(); 44 | databaseConf.load(LoadConfigListener.class.getResourceAsStream(source_dir+ "database.xml")); 45 | 46 | ApplicationContext context = testContext.getApplicationContext(); 47 | // System.out.println(context.getParent().getApplicationName()); 48 | 49 | } catch (IOException e) { 50 | e.printStackTrace(); 51 | } 52 | } 53 | 54 | @Override 55 | public void beforeTestMethod(TestContext testContext) throws Exception { 56 | 57 | } 58 | 59 | @Override 60 | public void afterTestMethod(TestContext testContext) throws Exception { 61 | 62 | } 63 | 64 | @Override 65 | public void afterTestClass(TestContext testContext) throws Exception { 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/test/test/com/qdcz/graph/neo4jcypher/service/TestNeo4jCYService.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.graph.neo4jcypher.service; 2 | 3 | import com.qdcz.App; 4 | import com.qdcz.conf.TestLoadConfigListener; 5 | import com.qdcz.entity.Vertex; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.context.SpringBootTest; 10 | import org.springframework.test.context.TestExecutionListeners; 11 | import org.springframework.test.context.junit4.SpringRunner; 12 | import org.springframework.test.context.web.WebAppConfiguration; 13 | 14 | /** 15 | * Created by star on 17-8-9. 16 | */ 17 | @RunWith(SpringRunner.class) 18 | @SpringBootTest(classes = App.class) 19 | @TestExecutionListeners(listeners = { TestLoadConfigListener.class }) 20 | @WebAppConfiguration 21 | public class TestNeo4jCYService { 22 | @Autowired 23 | Neo4jCYService graphBuzi; 24 | 25 | @Test 26 | public void testBfExtersion(){ 27 | graphBuzi = new Neo4jCYService(); 28 | System.out.println(graphBuzi); 29 | Vertex v = new Vertex(); 30 | v.setName("银团贷款业务"); 31 | v.setLabel("ytdk_label"); 32 | 33 | System.out.println(graphBuzi.bfExtersion(v,2)); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/test/test/com/qdcz/other/ImportData.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.other; 2 | 3 | import com.qdcz.common.CommonTool; 4 | import com.qdcz.other.StuctData.StructListedCom; 5 | 6 | import java.io.IOException; 7 | 8 | /** 9 | * Created by star on 17-8-14. 10 | */ 11 | public class ImportData { 12 | //bluckaddedges bluckaddvertex 13 | private static String host_port = "http://localhost:14000/bluckaddvertex?"; 14 | public static void main(String[] args) throws Exception { 15 | 16 | String dir = "/mnt/vol_0/neo4j-community-3.2.1/import/"; 17 | StructListedCom instance = new StructListedCom(); 18 | instance.inputFile="2016下半年";//2012下半年 2013上半年 2013下半年 2014上半年 2014下半年 2015下半年 2016下半年 2015上半年 2016上半年 2017上半年 19 | String re1="^\\d+"; 20 | String debtRatioStr= CommonTool.get_one_match(instance.inputFile,re1); 21 | String graph =null; 22 | if(instance.inputFile.contains("上")){ 23 | graph = "up_"+debtRatioStr; 24 | }else{ 25 | graph = "down_"+debtRatioStr; 26 | } 27 | String vertexsPath =dir; 28 | if(graph!=null){ 29 | if(host_port.contains("bluckaddvertex")){ 30 | // instance.doIt(); 31 | importXZ(graph,vertexsPath,"vertex.csv"); 32 | } 33 | if(host_port.contains("bluckaddedges")){ 34 | importXZ(graph,vertexsPath,"edges.csv"); 35 | importXZ(graph+"_danbao",vertexsPath,"edges2.csv"); 36 | } 37 | } 38 | 39 | } 40 | 41 | 42 | public static void importXZ(String graph,String vertexsPath, 43 | String edgesfile) throws IOException { 44 | 45 | String url = host_port+"label="+graph+"_label&vertexsPath="+java.net.URLEncoder.encode(vertexsPath,"utf-8")+"&relationship="+graph+"_relationship&edgesfile="+java.net.URLEncoder.encode(edgesfile,"utf-8"); 46 | 47 | CommonTool.query("",url); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/test/test/com/qdcz/other/JustTest.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.other; 2 | 3 | import java.util.HashSet; 4 | import java.util.Set; 5 | import java.util.Vector; 6 | import java.util.regex.Matcher; 7 | import java.util.regex.Pattern; 8 | 9 | /** 10 | * Created by star on 17-8-18. 11 | */ 12 | public class JustTest { 13 | public static void main(String[] args) { 14 | String mm = "柳工机械中东有限公司 2014年 12月 29日 4,270 一般保证 一年 否 是 "; 15 | 16 | 17 | System.out.println(replaceAllDataSpace(mm)); 18 | } 19 | 20 | public static String replaceAllDataSpace(String text){ 21 | Set set = getAllMatch("\\d{4}年\\s+\\d{1,2}月\\s+\\d{1,2}日",text); 22 | for (String one: set) { 23 | text = text.replace(one,one.replaceAll("\\s+","")); 24 | } 25 | return text; 26 | } 27 | 28 | public static Set getAllMatch(String reg,String str){ 29 | Set set = new HashSet<>(); 30 | Pattern p=Pattern.compile(reg); 31 | Matcher m=p.matcher(str); 32 | while(m.find()) { 33 | set.add(m.group()); 34 | } 35 | return set; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/test/test/com/qdcz/other/NumStatistLaws.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.other; 2 | 3 | import com.qdcz.common.CommonTool; 4 | 5 | import java.io.File; 6 | import java.io.FileNotFoundException; 7 | import java.util.List; 8 | import java.util.Scanner; 9 | import java.util.Vector; 10 | 11 | /** 12 | * Created by hadoop on 17-10-11. 13 | * 统计1行三会法规条数 14 | */ 15 | public class NumStatistLaws { 16 | public static void main(String[] args) { 17 | NumStatistLaws instance=new NumStatistLaws(); 18 | File dirInput = new File(args[0]); 19 | File[] files = dirInput.listFiles(); 20 | int sum=0; 21 | for (File file: files) { 22 | System.out.println(file); 23 | // if(file.toString().contains("决定")){ 24 | // continue; 25 | // } 26 | // FileWriter fileWriter = new FileWriter(file); 27 | int maxNum=0; 28 | Scanner scanner = null; 29 | try { 30 | scanner = new Scanner(file); 31 | while (scanner.hasNextLine()) { 32 | String strLine = scanner.nextLine(); 33 | int tiaoNum= instance.parse(strLine); 34 | if(tiaoNum>maxNum){ 35 | maxNum=tiaoNum; 36 | } 37 | } 38 | } catch (FileNotFoundException e) { 39 | e.printStackTrace(); 40 | } 41 | finally { 42 | if(scanner!=null){ 43 | scanner.close(); 44 | } 45 | 46 | // fileWriter.flush(); 47 | // fileWriter.close(); 48 | } 49 | sum+=maxNum; 50 | System.out.println("MaxNum:\t"+maxNum); 51 | } 52 | System.out.println("Sum:\t"+sum); 53 | 54 | } 55 | private int parse(String strline){ 56 | int result=0; 57 | String regex="第[一二一二三四五六七八九十百零]{1,}条"; 58 | Vector all_match = CommonTool.get_all_match(strline, regex); 59 | if(all_match.size()>0){ 60 | String s = all_match.get(all_match.size() - 1); 61 | result=chineseNumber2Int(s); 62 | } 63 | return result; 64 | } 65 | 66 | 67 | 68 | //将中文数字转化为阿拉伯数字 69 | private static int chineseNumber2Int(String chineseNumber){ 70 | int result = 0; 71 | int temp = 1;//存放一个单位的数字如:十万 72 | int count = 0;//判断是否有chArr 73 | char[] cnArr = new char[]{'一','二','三','四','五','六','七','八','九'}; 74 | char[] chArr = new char[]{'十','百','千','万','亿'}; 75 | for (int i = 0; i < chineseNumber.length(); i++) { 76 | boolean b = true;//判断是否是chArr 77 | char c = chineseNumber.charAt(i); 78 | for (int j = 0; j < cnArr.length; j++) {//非单位,即数字 79 | if (c == cnArr[j]) { 80 | if(0 != count){//添加下一个单位之前,先把上一个单位值添加到结果中 81 | result += temp; 82 | temp = 1; 83 | count = 0; 84 | } 85 | // 下标+1,就是对应的值 86 | temp = j + 1; 87 | b = false; 88 | break; 89 | } 90 | } 91 | if(b){//单位{'十','百','千','万','亿'} 92 | for (int j = 0; j < chArr.length; j++) { 93 | if (c == chArr[j]) { 94 | switch (j) { 95 | case 0: 96 | temp *= 10; 97 | break; 98 | case 1: 99 | temp *= 100; 100 | break; 101 | case 2: 102 | temp *= 1000; 103 | break; 104 | case 3: 105 | temp *= 10000; 106 | break; 107 | case 4: 108 | temp *= 100000000; 109 | break; 110 | default: 111 | break; 112 | } 113 | count++; 114 | } 115 | } 116 | } 117 | if (i == chineseNumber.length() - 1) {//遍历到最后一个字符 118 | result += temp; 119 | } 120 | } 121 | return result; 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /src/test/test/com/qdcz/other/StuctData/StructNewPaper.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.other.StuctData; 2 | 3 | import com.qdcz.common.CommonTool; 4 | import org.json.JSONObject; 5 | 6 | import java.io.File; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | import java.util.Scanner; 10 | import java.util.UUID; 11 | 12 | /** 13 | * Created by star on 17-7-28. 14 | */ 15 | public class StructNewPaper { 16 | public static void main(String[] args) throws Exception{ 17 | newStruct(); 18 | } 19 | 20 | 21 | public static void newStruct() throws Exception{ 22 | Scanner sc = new Scanner(new File("/media/star/Doc/工作文档/微信-论文项目/csis2.csv")); 23 | List nodes = new ArrayList<>(); 24 | List edges = new ArrayList<>(); 25 | String com_id = UUID.randomUUID().toString(); 26 | 27 | 28 | 29 | 30 | 31 | JSONObject content_com = new JSONObject(); 32 | content_com.put("area",""); 33 | content_com.put("zipcode",""); 34 | 35 | JSONObject node_com = new JSONObject(); 36 | node_com.put("identity",com_id); 37 | node_com.put("root","社会科学知识库"); 38 | node_com.put("name","美国战略国际研究中心"); 39 | node_com.put("type","com"); 40 | node_com.put("content",content_com.toString()); 41 | 42 | 43 | nodes.add(node_com.toString()); 44 | 45 | while(sc.hasNext()){ 46 | 47 | 48 | 49 | String line = sc.nextLine(); 50 | String company = line.split(",")[0]; 51 | String person = line.split(",")[1]; 52 | 53 | String pweson_id = UUID.randomUUID().toString(); 54 | JSONObject node_person = new JSONObject(); 55 | node_person.put("identity",pweson_id); 56 | node_person.put("root","社会科学知识库"); 57 | node_person.put("name",person); 58 | node_person.put("type","author"); 59 | node_person.put("content",new JSONObject().toString()); 60 | 61 | 62 | // JSONObject content_com = new JSONObject(); 63 | // content_com.put("area",""); 64 | // content_com.put("zipcode",""); 65 | // 66 | // JSONObject node_com = new JSONObject(); 67 | // node_com.put("identity",com_id); 68 | // node_com.put("root","社会科学知识库"); 69 | // node_com.put("name",company.trim()); 70 | // node_com.put("type","com"); 71 | // node_com.put("content",content_com); 72 | 73 | 74 | 75 | JSONObject content_one_edge = new JSONObject(); 76 | content_one_edge.put("order","1"); 77 | 78 | JSONObject one_edges = new JSONObject(); 79 | one_edges.put("root","社会科学知识库"); 80 | one_edges.put("from",pweson_id); 81 | one_edges.put("to",com_id); 82 | one_edges.put("name","属于"); 83 | one_edges.put("content",content_one_edge.toString()); 84 | 85 | 86 | 87 | nodes.add(node_person.toString()); 88 | 89 | edges.add(one_edges.toString()); 90 | } 91 | sc.close(); 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | StringBuffer sb = new StringBuffer(); 101 | for (String line:nodes){ 102 | sb.append(line+"\n"); 103 | } 104 | CommonTool.printFile(sb.toString().getBytes(),"/media/star/Doc/工作文档/微信-论文项目/vertex3.txt"); 105 | sb.delete(0,sb.length()); 106 | 107 | for (String line:edges){ 108 | sb.append(line+"\n"); 109 | } 110 | CommonTool.printFile(sb.toString().getBytes(),"/media/star/Doc/工作文档/微信-论文项目/edges3.txt"); 111 | sb.delete(0,sb.length()); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /src/test/test/com/qdcz/other/StuctData/StuctBSData.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.other.StuctData; 2 | 3 | import com.qdcz.common.CommonTool; 4 | import org.json.JSONArray; 5 | import org.json.JSONException; 6 | import org.json.JSONObject; 7 | 8 | import java.io.File; 9 | import java.util.*; 10 | 11 | /** 12 | * Created by hadoop on 17-10-1. 13 | */ 14 | public class StuctBSData { 15 | private static String root="上市公司分析"; 16 | private static String dir = "/mnt/vol_0/neo4j-community-3.2.1/import/"; 17 | private static String inputFile ="part-r-00000"; 18 | private static String ve_csv = dir+"vertex.csv"; 19 | private static String ve_txt = dir+"vertex_before.txt"; 20 | private static String edge_txt = dir+"edges_before.txt"; 21 | public static void main(String[] args) throws Exception { 22 | StuctBSData instance = new StuctBSData(); 23 | // instance.doIt(); 24 | System.out.println(ChineseNameTest("完那1")); 25 | } 26 | public void doIt() throws Exception { 27 | Map vertexs = new HashMap<>(); 28 | List edges = new ArrayList<>(); 29 | Map coms = new HashMap<>(); 30 | StringBuffer sb_neo4j = new StringBuffer(); 31 | StringBuffer sb_ela = new StringBuffer(); 32 | CommonTool.printFile("root,name,type,content,identity,weight\n",ve_csv,false); 33 | 34 | CommonTool.printFile("",ve_txt,false); 35 | 36 | CommonTool.printFile("",edge_txt,false); 37 | 38 | 39 | Scanner sc = new Scanner(new File(dir+inputFile)); 40 | while(sc.hasNext()){ 41 | // vertexs.clear(); 42 | edges.clear(); 43 | 44 | String line = sc.nextLine(); 45 | JSONObject one_com=null; 46 | try { 47 | one_com = new JSONObject(line.split("\t")[1]); 48 | } catch (JSONException e) { 49 | System.out.println(line.toString()); 50 | } 51 | 52 | String companyName = one_com.getString("CompanyName"); 53 | 54 | if(companyName.endsWith("公")){ 55 | companyName = companyName+"司"; 56 | } 57 | 58 | if(!StructListedCom.verifyName(companyName)){ 59 | continue; 60 | } 61 | 62 | 63 | companyName = StructListedCom.changeName(companyName); 64 | String identity=null; 65 | if(!coms.containsKey(companyName)){ 66 | identity = UUID.randomUUID().toString(); 67 | //公司 68 | JSONObject node_COM = new JSONObject(); 69 | node_COM.put("identity",identity); 70 | node_COM.put("root",root); 71 | node_COM.put("name",companyName.trim()); 72 | node_COM.put("type","com"); 73 | node_COM.put("content",new JSONObject().toString()); 74 | 75 | vertexs.put(companyName,node_COM); 76 | coms.put(companyName,identity); 77 | 78 | }else{ 79 | identity = coms.get(companyName); 80 | } 81 | //公司属性 82 | dealStrInfo( edges, vertexs,one_com,identity,coms); 83 | printEdgeData(sb_neo4j,edges); 84 | } 85 | printVertexData(sb_neo4j,sb_ela,vertexs); 86 | 87 | } 88 | 89 | private void dealStrInfo(List edges, Map vertexs , 90 | JSONObject obj,String identity, Map coms){ 91 | String useType=obj.getString("Type"); 92 | 93 | String name = null; 94 | String type = null; 95 | switch(useType) 96 | { 97 | case "holders": 98 | if(obj.has("shareholder")){ 99 | name = obj.getString("shareholder"); 100 | }else { 101 | name = ""; 102 | } 103 | if(obj.has("shareholder_Type")){ 104 | type = obj.getString("shareholder_Type"); 105 | }else { 106 | type = ""; 107 | } 108 | break; 109 | case "Branches": 110 | if(obj.has("BranchesName")){ 111 | name = obj.getString("BranchesName"); 112 | }else{ 113 | name = ""; 114 | } 115 | type = "分支机构"; 116 | break; 117 | case "KeyPerson": 118 | if(obj.has("name")){ 119 | name = obj.getString("name"); 120 | }else{ 121 | name = ""; 122 | } 123 | if(obj.has("position")){ 124 | type = obj.getString("position"); 125 | }else { 126 | type = ""; 127 | } 128 | break; 129 | case "invested": 130 | if(obj.has("name")){ 131 | name = obj.getString("name"); 132 | }else{ 133 | name = ""; 134 | } 135 | type = "对外投资"; 136 | break; 137 | default: 138 | name= ""; 139 | break; 140 | } 141 | if(!"".equals(name)){ 142 | if(vertexs.containsKey(name.hashCode())){ 143 | JSONObject jsonObject = vertexs.get(name.hashCode()); 144 | JSONObject jsonObject1 = new JSONObject(jsonObject.getString("content")); 145 | if(jsonObject1.has(useType)){ 146 | JSONArray jsonArray = jsonObject1.getJSONArray(useType); 147 | jsonArray.put(obj); 148 | }else{ 149 | JSONArray contentArray=new JSONArray(); 150 | contentArray.put(obj); 151 | jsonObject1.put(useType,contentArray); 152 | } 153 | String identity_in = coms.get(name); 154 | 155 | JSONObject one_edges = new JSONObject(); 156 | one_edges.put("root",root); 157 | one_edges.put("from",identity); 158 | one_edges.put("to",identity_in); 159 | one_edges.put("name",type); 160 | if(identity_in==null) 161 | System.out.println(); 162 | edges.add(one_edges); 163 | }else{ 164 | String identity_guanlian = UUID.randomUUID().toString(); 165 | JSONArray contentArray=new JSONArray(); 166 | contentArray.put(obj); 167 | JSONObject contentObj= new JSONObject(); 168 | contentObj.put(useType,contentArray); 169 | JSONObject node_COM = new JSONObject(); 170 | node_COM.put("identity",identity_guanlian); 171 | node_COM.put("root",root); 172 | node_COM.put("name",name.trim()); 173 | node_COM.put("type",getNameType(name)); 174 | node_COM.put("content",contentObj.toString()); 175 | vertexs.put(name,node_COM); 176 | 177 | coms.put(name,identity_guanlian); 178 | 179 | JSONObject one_edges = new JSONObject(); 180 | one_edges.put("root",root); 181 | one_edges.put("from",identity); 182 | one_edges.put("to",identity_guanlian); 183 | one_edges.put("name",type); 184 | edges.add(one_edges); 185 | } 186 | 187 | }else{ 188 | try { 189 | throw new Exception("没有名字:"+obj); 190 | } catch (Exception e) { 191 | e.printStackTrace(); 192 | } 193 | } 194 | 195 | 196 | } 197 | private String getNameType(String name){ 198 | if(ChineseNameTest(name)){ 199 | return "per"; 200 | }else{ 201 | return "com"; 202 | } 203 | } 204 | static boolean ChineseNameTest(String name) { 205 | if (!name.matches("[\u4e00-\u9fa5]{2,4}")) { 206 | // System.out.println("只能输入2到4个汉字"); 207 | return false; 208 | }else return true; 209 | } 210 | private void printEdgeData(StringBuffer sb_neo4j,List edges){ 211 | 212 | for (int i = 0;i< edges.size();i++){ 213 | try { 214 | JSONObject obj = edges.get(i); 215 | String root = obj.getString("root").replace(",", ","); 216 | String name = obj.getString("name").replace(",", ","); 217 | String from = obj.getString("from").replace(",", ","); 218 | String to = obj.getString("to").replace(",", ","); 219 | int weight = 1; 220 | obj.put("weight", weight); 221 | if (from.equals(to)) { 222 | continue; 223 | } 224 | 225 | String identity = UUID.randomUUID().toString(); 226 | obj.put("identity", identity); 227 | sb_neo4j.append(obj.toString()+"\n"); 228 | }catch (Exception e) 229 | { 230 | e.printStackTrace(); 231 | } 232 | 233 | } 234 | CommonTool.printFile(sb_neo4j.toString(),edge_txt,true); 235 | sb_neo4j.delete(0,sb_neo4j.length()); 236 | } 237 | private void printVertexData(StringBuffer sb_neo4j,StringBuffer sb_ela, Map vertexs ) { 238 | for (int i = 0; i < vertexs.size(); i++) { 239 | JSONObject obj = vertexs.get(i); 240 | String type = obj.getString("type").replace(",", ","); 241 | String root = obj.getString("root").replace(",", ","); 242 | String name = obj.getString("name").replace(",", ","); 243 | String content = obj.getString("content").replace(",", ","); 244 | String identity = obj.getString("identity").replace(",", ","); 245 | 246 | // if(!type.equals("com")){ 247 | // continue; 248 | // } 249 | 250 | if (type.equals("com")) { 251 | System.out.println(name); 252 | } 253 | 254 | sb_neo4j.append(root + "," + name + "," + type + "," + content + "," + identity + "\n"); 255 | 256 | sb_ela.append(obj.toString() + "\n"); 257 | 258 | 259 | } 260 | CommonTool.printFile(sb_neo4j.toString(), ve_csv, true); 261 | sb_neo4j.delete(0, sb_neo4j.length()); 262 | 263 | CommonTool.printFile(sb_ela.toString(), ve_txt, true); 264 | sb_ela.delete(0, sb_ela.length()); 265 | } 266 | } 267 | -------------------------------------------------------------------------------- /src/test/test/com/qdcz/other/StuctData/cutFile.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.other.StuctData; 2 | 3 | import org.json.JSONObject; 4 | 5 | import java.io.File; 6 | import java.io.FileNotFoundException; 7 | import java.util.HashMap; 8 | import java.util.List; 9 | import java.util.Map; 10 | import java.util.Scanner; 11 | 12 | /** 13 | * Created by star on 17-8-24. 14 | */ 15 | public class cutFile { 16 | 17 | public static void main(String[] args) throws FileNotFoundException { 18 | xx(); 19 | } 20 | 21 | private static void xx() throws FileNotFoundException { 22 | String[] yearsArr = new String[]{"2012", "2013", "2014", "2015", "2016", "2017"}; 23 | Map> map = new HashMap<>(); 24 | 25 | 26 | String filePath = ""; 27 | Scanner scanner = null; 28 | scanner = new Scanner(new File(filePath)); 29 | while (scanner.hasNext()) { 30 | String strLine = scanner.nextLine(); 31 | JSONObject jsonObject = new JSONObject(strLine); 32 | String year = jsonObject.getString("year"); 33 | String quarterStr = jsonObject.getString("quarterStr"); 34 | for (int i = 0; i < yearsArr.length; i++) { 35 | 36 | } 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/test/com/qdcz/other/hanlp/DemoDependencyParser.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.other.hanlp; 2 | 3 | /** 4 | * Created by hadoop on 17-7-18. 5 | */ 6 | 7 | import com.hankcs.hanlp.HanLP; 8 | import com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLSentence; 9 | import com.hankcs.hanlp.corpus.dependency.CoNll.CoNLLWord; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | * 依存句法分析(神经网络句法模型需要-Xms1g -Xmx1g -Xmn512m) 15 | * @author hankcs 16 | */ 17 | public class DemoDependencyParser 18 | { 19 | public static void main(String[] args) 20 | 21 | { 22 | DemoDependencyParser dp=new DemoDependencyParser(); 23 | dp.dependencyParser(); 24 | // String content = " 回购协议市场的定义"; 25 | // List keywordList = HanLP.extractKeyword(content, 2); 26 | // System.out.println(keywordList.toString()); 27 | // CommonSynonymDictionary.SynonymItem synonymItem = CoreSynonymDictionary.get("职责"); 28 | // List synonymList = synonymItem.synonymList; 29 | // for(Synonym synonym:synonymList){ 30 | // System.out.println(synonym.getIdString()); 31 | // System.out.println(synonym.getRealWord()); 32 | // System.out.println(synonym.getId()); 33 | // System.out.println( synonym.toString()); 34 | // Synonym.Type type = synonym.type; 35 | // } 36 | // Suggester suggester = new Suggester(); 37 | // String[] titleArray = 38 | // ( 39 | // "威廉王子发表演说 呼吁保护野生动物\n" + 40 | // "《时代》年度人物最终入围名单出炉 普京马云入选\n" + 41 | // "“黑格比”横扫菲:菲吸取“海燕”经验及早疏散\n" + 42 | // "日本保密法将正式生效 日媒指其损害国民知情权\n" + 43 | // "英报告说空气污染带来“公共健康危机”\n"+ 44 | // "关于银团贷款的定义是什么?\n"+ 45 | // "代理行负责哪些事情?\n" 46 | // ).split("\\n"); 47 | // for (String title : titleArray) 48 | // { 49 | // suggester.addSentence(title); 50 | // } 51 | // 52 | // System.out.println(suggester.suggest("发言", 1)); // 语义 53 | // System.out.println(suggester.suggest("危机公共", 1)); // 字符 54 | // System.out.println(suggester.suggest("mayun", 1)); // 拼音 55 | // System.out.println(suggester.suggest("职责", 1)); 56 | 57 | // String content = "程序员(英文Programmer)是从事程序开发、维护的专业人员。一般将程序员分为程序设计人员和程序编码人员,但两者的界限并不非常清楚,特别是在中国。软件从业人员分为初级程序员、高级程序员、系统分析员和项目经理四大类。"; 58 | // List keywordList = HanLP.extractKeyword(content, 10); 59 | // System.out.println(keywordList); 60 | // System.out.printf("%-5s\t\t%-5s\t\t%-10s\t\t%-5s\n", "词A", "词B", "语义距离", "语义相似度"); 61 | // for (String a : keywordList) 62 | // { 63 | // 64 | // for (String b : keywordList) 65 | // { 66 | // System.out.printf("%-5s\t\t%-5s\t%-15d\t%-5.10f\n", a, b, CoreSynonymDictionary.distance(a, b), CoreSynonymDictionary.similarity(a, b)); 67 | // } 68 | // } 69 | // //徐先生还具体帮助他确定了把画雄鹰、松鼠和麻雀作为主攻目标。 70 | // CoNLLSentence sentence = HanLP.parseDependency("股权转让的定义"); 71 | // System.out.println(sentence); 72 | // // 可以方便地遍历它 73 | // for (CoNLLWord word : sentence) 74 | // { 75 | // System.out.printf("%s --(%s)--> %s\n", word.LEMMA, word.DEPREL, word.HEAD.LEMMA); 76 | // } 77 | // // 也可以直接拿到数组,任意顺序或逆序遍历 78 | // CoNLLWord[] wordArray = sentence.getWordArray(); 79 | // for (int i = wordArray.length - 1; i >= 0; i--) 80 | // { 81 | // CoNLLWord word = wordArray[i]; 82 | // System.out.printf("%s --(%s)--> %s\n", word.LEMMA, word.DEPREL, word.HEAD.LEMMA); 83 | // } 84 | // // 还可以直接遍历子树,从某棵子树的某个节点一路遍历到虚根 85 | // CoNLLWord head = wordArray[wordArray.length-1]; 86 | // while ((head = head.HEAD) != null) 87 | // { 88 | // if (head == CoNLLWord.ROOT) System.out.println(head.LEMMA); 89 | // else System.out.printf("%s --(%s)--> ", head.LEMMA, head.DEPREL); 90 | // } 91 | } 92 | 93 | public void dependencyParser(){ 94 | CoNLLSentence sentence = HanLP.parseDependency("所属中科大的有哪些人?"); 95 | System.out.println(sentence); 96 | // 可以方便地遍历它 97 | for (CoNLLWord word : sentence) 98 | { 99 | System.out.printf("%s --(%s)--> %s\n", word.LEMMA, word.DEPREL, word.HEAD.LEMMA); 100 | 101 | System.out.printf("%s --%s --(%s)--> %s\n",word.CPOSTAG,word.NAME, word.POSTAG, word.toString()); 102 | 103 | } 104 | } 105 | } -------------------------------------------------------------------------------- /src/test/test/com/qdcz/other/parse/BytesToHexString.java: -------------------------------------------------------------------------------- 1 | package com.qdcz.other.parse; 2 | 3 | import com.qdcz.common.CommonTool; 4 | 5 | import java.io.File; 6 | import java.io.FileInputStream; 7 | import java.io.IOException; 8 | 9 | /** 10 | * Created by hadoop on 17-7-22. 11 | */ 12 | public class BytesToHexString { 13 | public static final String TYPE_RTF = "rtf"; 14 | public static final String TYPE_JPG = "jpg"; 15 | public static final String TYPE_GIF = "gif"; 16 | public static final String TYPE_PNG = "png"; 17 | public static final String TYPE_BMP = "bmp"; 18 | public static final String TYPE_UNKNOWN = "unknown"; 19 | public static final String TYPE_DOC = "doc"; 20 | public static final String TYPE_DOCX = "docx"; 21 | public static final String TYPE_XLS = "xls"; 22 | public static final String TYPE_XLSX = "xlsx"; 23 | public static final String TYPE_PDF = "pdf"; 24 | /** 25 | * byte数组转换成16进制字符串 26 | * @param src 27 | * @return 28 | */ 29 | public static String bytesToHexString(byte[] src){ 30 | StringBuilder stringBuilder = new StringBuilder(); 31 | if (src == null || src.length <= 0) { 32 | return null; 33 | } 34 | for (int i = 0; i < src.length; i++) { 35 | int v = src[i] & 0xFF; 36 | String hv = Integer.toHexString(v); 37 | if (hv.length() < 2) { 38 | stringBuilder.append(0); 39 | } 40 | stringBuilder.append(hv); 41 | } 42 | return stringBuilder.toString(); 43 | } 44 | 45 | public static final String OUT_PATH = "/mnt/vol_0/wnd/usr/leagal/一行三会数据/金融法律法规/exchange/"; 46 | /** 47 | * 根据文件流判断图片类型 48 | * @param fis 49 | * @return jpg/png/gif/bmp 50 | */ 51 | public static String getPicType(byte[] fis,String filePath) { 52 | 53 | String[] splits=filePath.split("/"); 54 | String filename =splits[splits.length-1].split("\\.")[0]; 55 | 56 | String outFilePath=OUT_PATH+filename+".txt"; 57 | //读取文件的前几个字节来判断图片格式 58 | byte[] b = new byte[4]; 59 | for(int i=0;i map = new HashMap<>(); 40 | int m = 0; 41 | while(sc.hasNext()){ 42 | String line = sc.nextLine(); 43 | JSONObject obj = null; 44 | try { 45 | obj = new JSONObject(line); 46 | }catch (Exception e){ 47 | continue; 48 | } 49 | if(!obj.has("label")){ 50 | continue; 51 | } 52 | 53 | 54 | 55 | String label = obj.getString("label").replace(",",",").replace("\"","").trim(); 56 | if(label.isEmpty()){ 57 | continue; 58 | } 59 | // if(label.replaceAll("(,| |'|:|\\(|\\))","").matches("\\w+") || label.matches(".*\\d+.*")){ 60 | // System.out.println("过滤一条"); 61 | // continue; 62 | // } 63 | 64 | 65 | if(label.matches(".*\\w+.*") || label.matches(".*\\d+.*")){ 66 | // System.out.println("过滤一条"); 67 | continue; 68 | } 69 | System.out.println(m++); 70 | 71 | String identity = ""; 72 | if(map.containsKey(label)){ 73 | identity = map.get(label); 74 | }else{ 75 | identity = UUID.randomUUID().toString(); 76 | map.put(label,identity); 77 | } 78 | 79 | 80 | JSONObject node_label = new JSONObject(); 81 | node_label.put("identity",identity); 82 | node_label.put("root","wiki数据"); 83 | node_label.put("name",label); 84 | node_label.put("type","com"); 85 | node_label.put("content",new JSONObject().toString()); 86 | 87 | vertexWriter.write(root+","+label+","+"com"+","+new JSONObject().toString()+","+identity+"\n"); 88 | 89 | 90 | 91 | 92 | if(obj.has("id")){ 93 | String id = obj.getString("id").replace(",",",").replace("\"",""); 94 | if(id.length() > 2) { 95 | JSONObject node_id = new JSONObject(); 96 | String id_identity = UUID.randomUUID().toString(); 97 | node_id.put("identity", id_identity); 98 | node_id.put("root", "wiki数据"); 99 | node_id.put("name", id); 100 | node_id.put("type", "id"); 101 | node_id.put("content", new JSONObject().toString()); 102 | 103 | vertexWriter.write(root + "," + id + "," + "id" + "," + new JSONObject().toString() + "," + id_identity + "\n"); 104 | 105 | JSONObject edges = new JSONObject(); 106 | edges.put("root", root); 107 | edges.put("from", identity); 108 | edges.put("weight", 1); 109 | edges.put("to", id_identity); 110 | edges.put("identity", UUID.randomUUID().toString()); 111 | edges.put("name", "id"); 112 | 113 | 114 | edgesWriter.write(edges.toString() + "\n"); 115 | } 116 | } 117 | 118 | 119 | if(obj.has("desc")){ 120 | String desc = obj.getString("desc").replace(",",",").replace("\"",""); 121 | if(desc.length()>2) { 122 | JSONObject node_desc = new JSONObject(); 123 | String desc_identity = UUID.randomUUID().toString(); 124 | node_desc.put("identity", desc_identity); 125 | node_desc.put("root", "wiki数据"); 126 | node_desc.put("name", desc); 127 | node_desc.put("type", "desc"); 128 | node_desc.put("content", new JSONObject().toString()); 129 | 130 | 131 | vertexWriter.write(root + "," + desc + "," + "desc" + "," + new JSONObject().toString() + "," + desc_identity + "\n"); 132 | 133 | 134 | JSONObject edges = new JSONObject(); 135 | edges.put("root", root); 136 | edges.put("from", identity); 137 | edges.put("weight", 1); 138 | edges.put("to", desc_identity); 139 | edges.put("identity", UUID.randomUUID().toString()); 140 | edges.put("name", "desc"); 141 | 142 | 143 | edgesWriter.write(edges.toString() + "\n"); 144 | 145 | } 146 | 147 | } 148 | 149 | if(obj.has("aliases")){ 150 | JSONArray arr = obj.getJSONArray("aliases"); 151 | for (int i = 0;i2) { 155 | 156 | JSONObject node_value = new JSONObject(); 157 | String value_identity = UUID.randomUUID().toString(); 158 | node_value.put("identity", value_identity); 159 | node_value.put("root", "wiki数据"); 160 | node_value.put("name", value); 161 | node_value.put("type", "aliases"); 162 | node_value.put("content", new JSONObject().toString()); 163 | 164 | 165 | vertexWriter.write(root + "," + value + "," + "aliases" + "," + new JSONObject().toString() + "," + value_identity + "\n"); 166 | 167 | 168 | JSONObject edges = new JSONObject(); 169 | edges.put("root", root); 170 | edges.put("from", identity); 171 | edges.put("weight", 1); 172 | edges.put("to", value_identity); 173 | edges.put("identity", UUID.randomUUID().toString()); 174 | edges.put("name", "aliases"); 175 | 176 | 177 | edgesWriter.write(edges.toString() + "\n"); 178 | } 179 | 180 | } 181 | 182 | } 183 | 184 | 185 | } 186 | sc.close(); 187 | edgesWriter.close(); 188 | vertexWriter.close(); 189 | } 190 | } 191 | --------------------------------------------------------------------------------