是否有直接有效的方式转换std::sub_match为std::basic_string_view(无需构建中间std::basic_string和没有中间堆分配)?或者进一步的抽象级别,是否有替代方法来std::regex_token_iterator迭代表示为std::basic_string_view而不是std::sub_match使用std(C++17)的正则表达式子匹配?
我宁愿使用std::basic_string_viewover的原因std::sub_match是:
std::basic_string_view指的是一个连续连续的类似字符的对象序列,该序列的第一个元素位于零位置。这允许使用charconv's std::from_chars(令人惊讶的是,它没有使用ForwardIterators实现)。对于std::sub_match,情况似乎并非如此,因为它表示为一对BidirectionalIterators。std::basic_string_view 具有更丰富的类似字符串的接口,在某些文件格式的某些特殊情况下促进额外的上下文敏感标记化。我正在使用“tm”包在 R 中创建 DocumentTermMatrix。它适用于 1-gram,但我正在尝试使用 tm 包和“tokenizers”包中的 tokenize_ngrams 函数创建 N-Grams(现在 N = 3)的 DocumenttermMatrix 。但我无法创建它。
我搜索了可能的解决方案,但没有得到太多帮助。出于隐私原因,我无法共享数据。这是我尝试过的,
library(tm)
library(tokenizers)
Run Code Online (Sandbox Code Playgroud)
data 是一个大约有 4.5k 行和 2 列的数据框,即“doc_id”和“text”
data_corpus = Corpus(DataframeSource(data))
Run Code Online (Sandbox Code Playgroud)
n-gram 标记化的自定义函数:
ngram_tokenizer = function(x){
temp = tokenize_ngrams(x, n_min = 1, n = 3, stopwords = FALSE, ngram_delim = "_")
return(temp)
}
Run Code Online (Sandbox Code Playgroud)
用于 DTM 创建的控制列表:
1-gram
control_list_unigram = list(tokenize = "words",
removePunctuation = FALSE,
removeNumbers = FALSE,
stopwords = stopwords("english"),
tolower = T,
stemming = T,
weighting = function(x)
weightTf(x)
)
Run Code Online (Sandbox Code Playgroud)
用于 N-gram …
我是 Word2Vec 的新手,我正在尝试根据单词的相似性对单词进行聚类。首先,我使用 nltk 来分隔句子,然后使用生成的句子列表作为 Word2Vec 的输入。然而,当我打印词汇时,它只是一堆字母、数字和符号,而不是单词。具体来说,其中一个字母的示例是“< gensim.models.keyedvectors.Vocab object at 0x00000238145AB438>, 'L':”
# imports needed and logging
import gensim
from gensim.models import word2vec
import logging
import nltk
#nltk.download('punkt')
#nltk.download('averaged_perceptron_tagger')
with open('C:\\Users\\Freddy\\Desktop\\Thesis\\Descriptions.txt','r') as f_open:
text = f_open.read()
arr = []
sentences = nltk.sent_tokenize(text) # this gives a list of sentences
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s',level=logging.INFO)
model = word2vec.Word2Vec(sentences, size = 300)
print(model.wv.vocab)
Run Code Online (Sandbox Code Playgroud) 以下为答案提供的代码问题;
import spacy
from spacy.tokenizer import Tokenizer
from spacy.util import compile_prefix_regex, compile_infix_regex, compile_suffix_regex
import re
nlp = spacy.load('en')
infixes = nlp.Defaults.prefixes + (r"[./]", r"[-]~", r"(.'.)")
infix_re = spacy.util.compile_infix_regex(infixes)
def custom_tokenizer(nlp):
return Tokenizer(nlp.vocab, infix_finditer=infix_re.finditer)
nlp.tokenizer = custom_tokenizer(nlp)
s1 = "Marketing-Representative- won't die in car accident."
s2 = "Out-of-box implementation"
for s in s1,s2:
doc = nlp("{}".format(s))
print([token.text for token in doc])
Run Code Online (Sandbox Code Playgroud)
结果
$python3 /tmp/nlp.py
['Marketing-Representative-', 'wo', "n't", 'die', 'in', 'car', 'accident', '.']
['Out-of-box', 'implementation']
Run Code Online (Sandbox Code Playgroud)
下面使用的第一个 (r"[./]") 和最后一个 (r"(.'.)") 模式是什么? …
我正在使用 Keras 解决多类分类问题。尝试使用 Keras tokenize 但认为 nltk.tokenizer 会更好地解决我的问题。我没有找到任何文章可以描述这两个标记器的区别,它们之间文本预处理的区别和准确性是什么?
Spacy 自动将诸如“dont”和“don't”之类的单词缩写标记为“do”和“nt”/“n't”。例如,像“我不明白”这样的句子将被标记为:[“I”, “do”, “nt”, “understand”]。
我知道这在许多 NLP 任务中通常很有帮助,但是有没有办法在 Spacy 中抑制这种特殊的标记化规则,从而使结果变为[“I”、“dont”、“understand”]?
这是因为我正在尝试评估我的自定义 Spacy NER 模型的性能(BIO 标记方案的 f1-score),并且输入句子中的标记数量与谓词标记标记数量的不匹配导致了我的问题评估代码如下:
输入(3 个标记):[("I", "O"), ("dont", "O"), ("understand", "O")]
预测(4 个标记):[("I", "O"), ("do", "O"), ("nt", "O"), ("understand", "O")]
当然,如果有人对 Spacy 中的顺序标记任务执行评估有任何建议(可能类似于seqeval包,但与 Spacy 的标记格式更兼容),我们也将不胜感激。
我正在尝试标记和解析已经分成句子并且已经被标记化的文本。举个例子:
sents = [['I', 'like', 'cookies', '.'], ['Do', 'you', '?']]
Run Code Online (Sandbox Code Playgroud)
处理批量文本的最快方法是.pipe()。但是,我不清楚如何将其与预标记和预分段的文本一起使用。性能是这里的关键。我尝试了以下操作,但这引发了错误
docs = [nlp.tokenizer.tokens_from_list(sentence) for sentence in sents]
nlp.tagger(docs)
nlp.parser(docs)
Run Code Online (Sandbox Code Playgroud)
痕迹:
Traceback (most recent call last):
File "C:\Python\Python37\Lib\multiprocessing\pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "C:\Python\projects\PreDicT\predicting-wte\build_id_dictionary.py", line 204, in process_batch
self.nlp.tagger(docs)
File "pipes.pyx", line 377, in spacy.pipeline.pipes.Tagger.__call__
File "pipes.pyx", line 396, in spacy.pipeline.pipes.Tagger.predict
File "C:\Users\bmvroy\.virtualenvs\predicting-wte-YKqW76ba\lib\site-packages\thinc\neural\_classes\model.py", line 169, in __call__
return self.predict(x)
File "C:\Users\bmvroy\.virtualenvs\predicting-wte-YKqW76ba\lib\site-packages\thinc\neural\_classes\feed_forward.py", line 40, in predict
X = layer(X)
File "C:\Users\bmvroy\.virtualenvs\predicting-wte-YKqW76ba\lib\site-packages\thinc\neural\_classes\model.py", line 169, in …Run Code Online (Sandbox Code Playgroud) 我有一个句子,希望看到如下预期的标记。
Sentence: "[x] works for [y] in [z]."
Tokens: ["[", "x", "]", "works", "for", "[", "y", "]", "in", "[", "z", "]", "."]
Expected: ["[x]", "works", "for", "[y]", "in", "[z]", "."]
Run Code Online (Sandbox Code Playgroud)
如何通过自定义分词器函数来做到这一点?
有没有办法切片字符串让我说我有这个变量
$output=Country=UNITED STATES (US) &City=Scottsdale, AZ &Latitude=33.686 &Longitude=-111.87
Run Code Online (Sandbox Code Playgroud)
我想以某种方式切片,我想将纬度和经度值拉入单独的变量,subok不能达到目的
可能重复:
如何在C++中对字符串进行标记?
strtok函数不是线程安全的.Microsoft具有特定于Windows的strtok_s和CString :: Tokenize安全功能.有没有手动编码的跨平台CRT/C++库方法呢?