标签: text-processing

移动单词中的字母位置

我想要一个命令/函数,最好是 bash,它接受一个单词/字符串和一个数字,并将单词中的字母位置移动该数字,将溢出旋转回开头。

例如输入stack2输出将是cksta

我曾考虑过使用,tr但我不太清楚如何使其通用以适用于任何单词,而不仅仅是翻译目标单词中的特定字母。

bash text-processing tr

1
推荐指数
1
解决办法
1705
查看次数

结合Word embedding来获得句子嵌入的更好方法

我在许多kaggle内核和教程中看到,平均词嵌入被认为是获得句子的嵌入。但是,我想知道这是否是正确的方法。因为它丢弃了句子中单词的位置信息。有没有更好的方法来结合嵌入?也许以特定的方式将它们分层组合?

text-processing nlp deep-learning word-embedding

1
推荐指数
1
解决办法
1694
查看次数

Perl在HTML / XML标记内的单词周围添加<a> </a>

我有一个格式像这样的文件:

Eye color
<p class="ul">Eye color, color</p> <p class="ul1">blue, cornflower blue, steely blue</p> <p class="ul1">velvet brown</p> <link rel="stylesheet" href="a.css">
</>
weasel
<p class="ul">weasel</p> <p class="ul1">musteline</p> <link rel="stylesheet" href="a.css">
</>
Run Code Online (Sandbox Code Playgroud)

<p class="ul1">分隔符内的每个单词,都应包裹在一个<a>标签中,如下所示:

Eye color
<p class="ul">Eye color, color</p> <p class="ul1"><a href="entry://blue">blue</a>, <a href="entry://cornflower blue">cornflower blue</a>, <a href="entry://steely blue">steely blue</a></p> <p class="ul1"><a href="entry://velvet brown">velvet brown</a></p> <link rel="stylesheet" href="a.css">
</>
weasel
<p class="ul">weasel</p> <p class="ul1"><a href="entry://musteline">musteline</a></p> <link rel="stylesheet" href="a.css">
</>
Run Code Online (Sandbox Code Playgroud)

<p class="ul1">标签内可能有一个或几个单词。

单线可能Perl吗?

提前致谢。任何帮助表示赞赏。

html xml perl text-processing

1
推荐指数
1
解决办法
214
查看次数

与第二个 awk 比较后无法替换文件

我试图比较两个文件,然后用第二个文件中的匹配内容替换第一个文件的内容。

第一个文件:

14523 : NOT
98765 : OTH
23145 : UNT
65743 : NOT
Run Code Online (Sandbox Code Playgroud)

第二个文件:

23145vec#1
14523vec#2
65743vec#3
98765vec#4
Run Code Online (Sandbox Code Playgroud)

输出应该是这样的:

23145vec#1 : UNT
14523vec#2 : NOT
65743vec#3 : NOT
98765vec#4 : OTH
Run Code Online (Sandbox Code Playgroud)

我试图做的是:

awk 'NR==FNR { F2[$1] = $1 } ($1 in F2){print F2[$1] = $1" : "$2; next} ' file2 file1 > final.txt
Run Code Online (Sandbox Code Playgroud)

结果 :

23145vec#1 :
14523vec#2 :
65743vec#3 :
98765vec#4 :
Run Code Online (Sandbox Code Playgroud)

regex awk text-processing

1
推荐指数
1
解决办法
67
查看次数

如何使用perl中的grep从一个数组中选择元素到另一个数组?

我想捕获 中的文件CWD和 中的文件之间的差异@files

#!/usr/bin/perl -w
use Cwd qw[getcwd abs_path];
opendir CWD, getcwd;
@files=grep{!/^\./}readdir CWD;

push @files, ("foo.txt", "bar.txt");

for my $i (@files){
    @difference=grep { !/^\./ and $i!=$_ } readdir CWD;
}
print "$_\n" for @differenc 
Run Code Online (Sandbox Code Playgroud)

现在当前目录有这个文件:

$ls
a.txt  e.txt  getopt.html
Run Code Online (Sandbox Code Playgroud)

使用这个表达式push @files, ("foo.txt", "bar.txt"); ,数组有这些元素:("foo.txt", "bar.txt", "a.txt", "e.txt", "getopt.html"),这是正确的,但现在我只想选择那些不在CWD: 中的文件 @difference=grep { !/^\./ and $i!=$_ } readdir CWD;,所以我希望数组@difference再次("foo.txt", "bar.txt")只有(我现在这没有意义,它是例如)。但是打印出来什么都不输出,怎么回事?

arrays perl grep text-processing

1
推荐指数
1
解决办法
83
查看次数

为什么 Perl 正则表达式捕获组在“打印”和算术运算之间的行为不同?

在 Perl (v5.30.0) 中,正则表达式被评估为捕获,当用作参数时print()

# Simplified example; the real case has more text, and the capture covers only part of it.

echo $'1\n2\n3' | perl -ne 'print /(.)/'
# 123
Run Code Online (Sandbox Code Playgroud)

这对于文本提取非常有用。我想利用算术运算的相同便利,但这并不能按预期工作:

# Attempt to compute a sum of the int value of the captures
#
echo $'1\n2\n3' | perl -ne '$tot += /(.)/; END { print $tot }'
# 3

# Attempt to print twice the int value of each capture
#
echo $'1\n2\n3' | perl -ne 'print(/(.)/ …
Run Code Online (Sandbox Code Playgroud)

regex perl text-processing capturing-group

1
推荐指数
1
解决办法
83
查看次数

使用 awk 将多行文本转换为 CSV

我正在查看一些评论,并试图确定购买苹果的最佳公司(例如)。我复制并粘贴了下面的文本,我想使用 Linux 命令对其进行一些文本处理。根据我在网上阅读的内容,awk 是一个不错的选择,但我无法让它工作。

\n

我尝试将具有评级的行并将其附加到上面的行并用逗号分隔。例如:Abes Apples\\n 4.1将成为Abes Apples, 4.1并且这将被重复。我测试的 awk 命令是 awk 'BEGIN {RS=""}{gsub(/\\n[0-9]/, ", ", $0); print $0}' test.text,它给出了下面的结果,但它正在替换数字..

\n
Abes Apples, .1,\n(138) \xc2\xb7 apple company, + years in business (123) 456-7890\nAdams Apples, .9,\n(105) \xc2\xb7 apple company, 0+ years in business (234) 567-8901\nApples are Amazing, .9,\n(13) apple company, 0+ years in business (345) 678-9012\n
Run Code Online (Sandbox Code Playgroud)\n

文本文件模式如下所示,并对文本文件中的所有行重复:

\n
    \n
  1. 公司名称
  2. \n
  3. 评分
  4. \n
  5. 评论数量和公司类型
  6. \n
  7. 经营年限和电话号码
  8. \n
\n

我的目标是将此文本文件转换为类似 csv 的格式,其中包含公司名称、评级、评论数量(忽略“苹果公司”文本)、业务年限和电话号码的列标题。这是可以用 awk 和其他 Linux …

regex linux awk text-processing

1
推荐指数
1
解决办法
156
查看次数

如何删除换行符以外的所有非单词字符?

我有这样一个文件:

my line - some words & text
oh lóok i've got some characters
Run Code Online (Sandbox Code Playgroud)

我想"标准化"它并删除所有非单词字符.我想最终得到这样的东西:

mylinesomewordstext
ohlóokivegotsomecharacters
Run Code Online (Sandbox Code Playgroud)

我现在正在命令行上使用Linux,我希望我可以使用一些单行程.

我试过这个:

cat file | perl -pe 's/\W//'
Run Code Online (Sandbox Code Playgroud)

但是,这删除了所有换行符并将所有内容都排成一行.有没有我可以告诉Perl不包括新行\W?或者还有其他方式吗?

perl text text-processing

0
推荐指数
1
解决办法
5081
查看次数

如何将大文件过滤为两个单独的文件?

我有一个巨大的文件(500 MB)组织如下:

<link type="1-1" xtargets="1;1">
    <s1>bunch of text here</s1>
    <s2>some more here</s2>
</link>
<link type="1-1" xtargets="1;1">
    <s1>bunch of text here</s1>
    <s2>some more here</s2>
</link>
<link type="1-1" xtargets="1;1">
    <s1>bunch of text here</s1>
    <s2>some more here</s2>
</link>
Run Code Online (Sandbox Code Playgroud)

我想把它转换成一种新格式,其中s1转到一个新文件,每个s1在它自己的行上有一个换行符,而s2转到一个新文件,每个s2在它自己的行上.

Perl是去这里的方式吗?如果是这样,有人可以告诉我如何实现这一目标吗?

xml perl search text-processing

0
推荐指数
2
解决办法
469
查看次数

用bash变量替换文本文件行中的第5和第6个"单词"

这个问题扩展了我的上一个问题(搜索特定的文本文件行,替换为某个字符).在@anubhava用户的帮助下,我能够搜索以dataspec开头的行,并从中进行转换

dataspecs v1 v2 v3 v4 v5 ! blah

dataspecs $new_val ! blah

不过,我现在需要能够保持变量v1 v2 v3(我不知道他们的实际值),并更换v4v5使用bash变量$new_v4$new_v5.所以我的新行现在看起来像这样:

dataspecs v1 v2 v3 new_v4 new_v5 ! blah

bash awk text-processing sed

0
推荐指数
1
解决办法
526
查看次数

标签 统计

text-processing ×10

perl ×5

awk ×3

regex ×3

bash ×2

xml ×2

arrays ×1

capturing-group ×1

deep-learning ×1

grep ×1

html ×1

linux ×1

nlp ×1

search ×1

sed ×1

text ×1

tr ×1

word-embedding ×1