除此之外,还需要通过 python -m spacy download zh_core_web_sm 和 python -m spacy download en_core_web_sm 来安装 zh_core_web_sm==3.7.0和en_core_web_sm==3.7.1
1from huggingface_hub import hf_hub_download
2import importlib.util
3
4# 替换为你的 Hugging Face 用户名和仓库名
5def nlp(content, function, method):
6 repo_id = "epetery/my-new-model"
7 filename = "divide_corpus.py"
8 stopwords_filename = "stopwords-master/baidu_stopwords.txt"
9
10 # 下载文件到当前工作目录
11 file_path = hf_hub_download(repo_id=repo_id, filename=filename)
12 stopwords_file_path = hf_hub_download(repo_id=repo_id, filename=stopwords_filename)
13
14 # 导入模块
15 spec = importlib.util.spec_from_file_location("divide_corpus", file_path)
16 divide_corpus = importlib.util.module_from_spec(spec)
17 spec.loader.exec_module(divide_corpus)
18
19 divide_corpus.STOPWORDS_FILE_PATH = stopwords_file_path
20
21 # 使用模块中的类和方法
22 text_divider = getattr(divide_corpus, "NLP_Class")(content)
23 if function != 'count_word_frequency':
24 divided_text = getattr(text_divider, function)(method=method)
25 else:
26 seg_text = getattr(text_divider, 'segment')(method=method)
27 freq_counter = getattr(divide_corpus, "NLP_Class")(seg_text)
28 divided_text = freq_counter.count_word_frequency()
29 return divided_text
30
31# 使用模块中的函数
32
33text = "This is a test text."
34divided_text=nlp(text,'remove_stopword','nltk')
35print(divided_text)