nlp scapy use
Published in:2024-11-21 |
Words: 303 | Reading time: 1min | reading:

nlp scapy use

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pip install spacy
python -m spacy download zh_core_web_sm
# z中等
python -m spacy download zh-core-web-md
#大语言模型
python -m spacy download zh-core-web-lg
# 插件 支持
pip install "spacy-pkuseg<2.0.0,>=1.0.0"


# add run
pip install "spacy-pkuseg>=0.0.27,<0.1.0"
# or
pip uninstall spacy-pkuseg
pip install spacy-pkuseg==1.0.0

# or
pip uninstall spacy zh-core-web-sm spacy-pkuseg
pip install spacy
pip install zh-core-web-sm
pip install "spacy-pkuseg>=1.0.0,<2.0.0"
1
2
3
4
python -m spacy download en_core_web_sm



use

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# english
import spacy

# 加载下载的模型
nlp = spacy.load('en_core_web_sm')

# 处理文本
doc = nlp("Hello, how are you?")
for token in doc:
print(token.text, token.pos_)


# chinese

import spacy

# 加载中文模型
nlp = spacy.load('zh_core_web_sm')

def extract_dialogue_with_spacy_chinese(text):
doc = nlp(text)
dialogues = []
current_speaker = None
current_dialogue = []

for sent in doc.sents:
for ent in sent.ents:
if ent.label_ == "PERSON":
# 假设实体标签为“PERSON”表示人物
if current_speaker: # 如果之前有一个人物,记录对话
dialogues.append((current_speaker, " ".join(current_dialogue)))
current_speaker = ent.text
current_dialogue = []
break # 跳到下一个句子
if current_speaker:
current_dialogue.append(sent.text.strip())

if current_dialogue:
dialogues.append((current_speaker, " ".join(current_dialogue)))

return dialogues

# 示例文本
script = """
张三: 你好吗?
李四: 我很好,谢谢!
张三: 太好了!
"""

dialogues = extract_dialogue_with_spacy_chinese(script)
for character, line in dialogues:
print(f"人物: {character}, 对话: {line}")

Prev:
nltk lib use
Next:
yolov5s 模型识别人脸