Skip to content

Commit

Permalink
修复 #4 无法添加自定义地址问题并更新jar包
Browse files Browse the repository at this point in the history
  • Loading branch information
fjklqq committed Mar 6, 2023
1 parent 282f0f6 commit 3d4d01d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
36 changes: 19 additions & 17 deletions GeocodingCHN/Geocoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@
# @About :
__all__ = ['Geocoding']

import jpype
import re
import os
import re
import traceback
import warnings

import jpype

from .model import Address
from .model import RegionType
from .model import Version
from .model import Document
from .model import MatchedResult
from .model import RegionType
from .model import Version


class Geocoding:
def __init__(self, data_class_path='core/region.dat', strict: bool = False, jvm_path: str = None):
'''
"""
:param data_class_path:自定义地址文件路径
:param strict:模式设置
:param jvm_path:JVM路径
'''
"""
class_path = os.path.join(os.path.split(os.path.abspath(__file__))[0],
'geocoding.jar'
)
Expand All @@ -49,18 +51,19 @@ def __init__(self, data_class_path='core/region.dat', strict: bool = False, jvm_
jpype.startJVM(jvm_path, "-ea", "-Djava.class.path=" + class_path) # classpath=class_paths)#
else:
try:
jpype.JClass('org.bitlap.geocoding.Geocoding')
warnings.warn("Geocoding 已被创建,正在尝试重新加载(该过程在Windows环境下可能会出现异常)", category=RuntimeWarning)
jpype.JClass('org.bitlap.geocoding.GeocodingX')
warnings.warn("Geocoding 已被创建,正在尝试重新加载(该过程在Windows环境下可能会出现异常)",
category=RuntimeWarning)
except:
warnings.warn("JVM 已经在运行", category=RuntimeWarning)
jpype.addClassPath(class_path)
self._jar_version = '1.3.0'
self._jar_version = '1.3.1'
self.geocoding = jpype.JClass('org.bitlap.geocoding.GeocodingX')(data_name, strict=strict)
self.RegionType = RegionType(jpype.JClass('org.bitlap.geocoding.model.RegionType'))

@property
def __version__(self):
return Version(package='v1.4.1', jar=self._jar_version)
return Version(package='v1.4.2', jar=self._jar_version)

def normalizing(self, address: str) -> Address:
"""
Expand Down Expand Up @@ -169,34 +172,34 @@ def similarity(self, address_1: [Address, str], address_2: [Address, str]) -> fl
type(address_1), type(address_2)))
return result

def addRegionEntry(self, Id: int, parentId: int, name: str, RegionType: RegionType, alias: str = '',
def addRegionEntry(self, Id: int, parentId: int, name: str, region_type: RegionType, alias: str = '',
replace: bool = True) -> bool:
"""
添加自定义地址信息
:param Id: 地址的ID
:param parentId: 地址的父ID, 必须存在
:param name: 地址的名称
:param RegionType: 地址类型,RegionType,
:param region_type: 地址类型,RegionType,
:param alias: 地址的别名, default=''
:param replace: 是否替换旧地址, 当除了[id]之外的字段, 如果相等就替换
:return:
"""
try:
self.geocoding.addRegionEntry(id=Id, parentId=parentId, name=name,
RegionType=RegionType, alias=alias, replace=replace)
self.geocoding.addRegionEntry(Id, parentId, name, region_type, alias, replace)
return True
except:
traceback.print_exc()
return False

def segment(self, text: str, seg_type: str = 'ik') -> list:
'''
"""
分词
:param text: input
:param seg_type: ['ik', 'simple', 'smart', 'word']
:return:
'''
"""
if seg_type == 'ik':
seg_class = jpype.JClass('org.bitlap.geocoding.core.segment.IKAnalyzerSegmenter')()
elif seg_type == 'simple':
Expand All @@ -208,4 +211,3 @@ def segment(self, text: str, seg_type: str = 'ik') -> list:
else:
raise AttributeError("'seg_type' 只可以是 ['ik', 'simple', 'smart', 'word'] 中的一种")
return list(seg_class.segment(text))

Binary file modified GeocodingCHN/geocoding.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
* 安装命令 `pip install GeocodingCHN`

## 更新信息:

## 1.4.1

修复 无法添加自定义地址问题,并更新jar包至1.3.1

## 1.4.0

[原项目](https://github.com/bitlap/geocoding)更新jar包,并适配新增功能。 [新增功能](https://github.com/bitlap/geocoding/releases/tag/v1.3.0)
- [x] `GeocodingCHN.Geocoding`新增参数设定(为适配`org.bitlap.geocoding.GeocodingX`类)
* 新增`data_class_path`参数,支持自定义地址文件路径,要求该路径为文件绝对路径,默认使用内置地址`core/region.dat`
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def read_file(filename):
setup(
# name='GeocodingCHN',
name='GeocodingCHN',
version='1.4.1',
version='1.4.2',
author='ZhouHang',
author_email='fjkl@vip.qq.com',
description='地址标准化',
Expand Down

0 comments on commit 3d4d01d

Please sign in to comment.