Skip to content

Commit

Permalink
bump version to v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
leovan committed Oct 19, 2023
1 parent 5939c8c commit 8df414e
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 24 deletions.
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Changelog

## [1.1.0](https://github.com/leovan/duckling-chinese/compare/v1.0.0...v1.1.0) (2023-10-19)

Duckling Core 版本:[1.5.3](https://mvnrepository.com/artifact/com.xiaomi.duckling/duckling-core_2.13/1.5.3)

### 功能

- 添加 `remove_duplicate` 参数用于删除重复结果

### 修复

- 修复 JDK 9+ 环境下序列化 JSON 结果时 `java.*` API 中非公有成员的反射问题

## 1.0.0 (2023-10-15)

Duckling Core 版本:[1.5.2](https://mvnrepository.com/artifact/com.xiaomi.duckling/duckling-core_2.13/1.5.2)

### 功能

- 第一个发布版本
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2023 Leo Van
Copyright (c) 2023 范叶亮

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
21 changes: 12 additions & 9 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ duckling = Duckling()
```python
def analyze(
self,
text: str,
context=None,
options=None
text: Text,
context: JavaDucklingTypes.Context = None,
options: JavaDucklingTypes.Options = None,
remove_duplicate=True
) -> List[Dict[Text, Any]]
```

Expand All @@ -32,7 +33,8 @@ def parse_entities(
self,
text: Text,
context: JavaDucklingTypes.Context = None,
options: JavaDucklingTypes.Options = None
options: JavaDucklingTypes.Options = None,
remove_duplicate=True
) -> List[Dict[Text, Any]]
```

Expand All @@ -54,11 +56,12 @@ def lunar_to_solar(lunar_datetime: Text)

参数列表如下:

| 参数 | 类型 | 默认值 | 说明 |
| ------- | ----------------------------------- | ------ | ---------- |
| text | `str` | | 待解析文本 |
| context | `com.xiaomi.duckling.Types.Context` | `None` | 解析上下文 |
| options | `com.xiaomi.duckling.Types.Options` | `None` | 解析选项 |
| 参数 | 类型 | 默认值 | 说明 |
| ---------------- | ----------------------------------- | ------ | ---------------- |
| text | `str` | | 待解析文本 |
| context | `com.xiaomi.duckling.Types.Context` | `None` | 解析上下文 |
| options | `com.xiaomi.duckling.Types.Options` | `None` | 解析选项 |
| remove_duplicate | `bool` | `True` | 是否删除重复结果 |

### `context`

Expand Down
31 changes: 22 additions & 9 deletions duckling_chinese/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
def _start_jvm():
jvm_options = [
'-Xms128m',
'-Xmx2048m'
'-Xmx2048m',
'--illegal-access=permit',
'--add-opens=java.base/java.lang=ALL-UNNAMED',
'--add-opens=java.base/java.util=ALL-UNNAMED',
'--add-opens=java.base/java.util.regex=ALL-UNNAMED'
]

jars_dir = Path(os.path.dirname(__file__)) / 'jars'
Expand All @@ -43,6 +47,7 @@ def _start_jvm():
from duckling_chinese.ranker import *
from duckling_chinese.utils import *

from java.lang import Thread as JavaThread
from java.time import ZonedDateTime as JavaZonedDateTime
from java.time import ZoneId as JavaZoneId
from java.time import ZoneOffset as JavaZoneOffset
Expand All @@ -53,8 +58,6 @@ def _start_jvm():

from com.xiaomi.duckling import Api as JavaDucklingApi
from com.xiaomi.duckling import Types as JavaDucklingTypes
from com.xiaomi.duckling.dimension import EnumeratedDimension \
as JavaDucklingEnumeratedDimension
from com.xiaomi.duckling.dimension.time import TimeOptions \
as JavaDucklingTimeOptions
from com.xiaomi.duckling.dimension.numeral import NumeralOptions \
Expand All @@ -70,8 +73,8 @@ def __init__(self):

try:
if threading.active_count() > 1:
if not jpype.isThreadAttachedToJVM():
jpype.attachThreadToJVM()
if not JavaThread.isAttached():
JavaThread.attach()
self._lock.acquire()
self._api = JavaDucklingApi
finally:
Expand Down Expand Up @@ -211,15 +214,20 @@ def analyze(
self,
text: Text,
context: JavaDucklingTypes.Context = None,
options: JavaDucklingTypes.Options = None
options: JavaDucklingTypes.Options = None,
remove_duplicate=True
) -> List[Dict[Text, Any]]:
if context is None:
context = self.gen_context()

if options is None:
options = self.gen_options()

answers_java = self._api.analyzeJ(text, context, options)
answers_java = self._api.analyze(text, context, options)

if remove_duplicate:
answers_java = answers_java.distinct()

answers_json = JavaJsonSerialization.write(
answers_java,
JavaDucklingJsonSerde.formats()
Expand All @@ -231,7 +239,8 @@ def parse_entities(
self,
text: Text,
context: JavaDucklingTypes.Context = None,
options: JavaDucklingTypes.Options = None
options: JavaDucklingTypes.Options = None,
remove_duplicate=True
) -> List[Dict[Text, Any]]:
if context is None:
context = self.gen_context()
Expand All @@ -240,6 +249,10 @@ def parse_entities(
options = self.gen_options()

entities_java = self._api.parseEntities(text, context, options)

if remove_duplicate:
entities_java = entities_java.distinct()

entities_json = JavaJsonSerialization.write(
entities_java,
JavaDucklingJsonSerde.formats()
Expand All @@ -256,4 +269,4 @@ def parse_entities(
]


__version__ = '1.0.0'
__version__ = '1.1.0'
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<groupId>tech.leovan</groupId>
<artifactId>duckling-chinese</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<version>1.1.0</version>

<dependencies>
<dependency>
<groupId>com.xiaomi.duckling</groupId>
<artifactId>duckling-core_2.13</artifactId>
<version>1.5.2</version>
<version>1.5.3</version>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion test/dimension/test_currency.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test_area(self):
]

print(json.dumps(entities, indent=2, ensure_ascii=False))
self.assertEqual(2, len(entities))
self.assertEqual(1, len(entities))
self.assertEqual('RMB两块九毛九', entities[0]['body'])
self.assertEqual('Currency', entities[0]['dim'])
self.assertEqual(0, entities[0]['start'])
Expand Down
2 changes: 1 addition & 1 deletion test/dimension/test_lyric.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_lyric(self):
text, self._default_context, options)

print(json.dumps(entities, indent=2, ensure_ascii=False))
self.assertEqual(2, len(entities))
self.assertEqual(1, len(entities))
self.assertEqual('编曲:墨辞 词: 刀郎 曲 周杰伦', entities[0]['body'])
self.assertEqual('Lyric', entities[0]['dim'])
self.assertEqual(0, entities[0]['start'])
Expand Down
2 changes: 1 addition & 1 deletion test/dimension/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def test_time(self):
text, self._default_context, options)

print(json.dumps(entities, indent=2, ensure_ascii=False))
self.assertEqual(2, len(entities))
self.assertEqual(1, len(entities))
self.assertEqual('明年的11月份第二个周日', entities[0]['body'])
self.assertEqual('Time', entities[0]['dim'])
self.assertEqual(0, entities[0]['start'])
Expand Down
32 changes: 32 additions & 0 deletions test/test_analyze.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-

import unittest

from duckling_chinese import Duckling
from duckling_chinese.dimension import DucklingDimension


class AnalyzeTestCase(unittest.TestCase):
def setUp(self):
self._duckling = Duckling()
self._default_context = self._duckling.gen_context()

def test_analyze(self):
options = self._duckling.gen_options(
targets={
DucklingDimension.LYRIC
}
)

text = '编曲:墨辞 词: 刀郎 曲 周杰伦'
answers = self._duckling.analyze(
text, self._default_context, options)
entities = self._duckling.parse_entities(
text, self._default_context, options)

self.assertEqual(2, len(answers))
self.assertEqual(1, len(entities))


if __name__ == '__main__':
unittest.main()

0 comments on commit 8df414e

Please sign in to comment.