gpt4 book ai didi

python - 如何枚举单词的组合?

转载 作者:行者123 更新时间:2023-12-01 00:47:36 24 4
gpt4 key购买 nike

给定带有斜杠的文本确定性/确定性算术/计算,目标是枚举单词的可能组合,例如预期输出:

definitive arithmetic
deterministic arithmetic
definitive calculation
deterministic calculation

再比如,输入语音/语音波信息处理方法/技术,期望输出:

voice wave information processing method
voice wave information processing technique
speech wave information processing method
speech wave information processing technique

有时会有括号,预期的输出将枚举包含或不包含括号内的术语的输出,例如输入杀菌/微生物(性质/性质),预期输出:

bactericidal
microbidical
bactericidal nature
bactericidal properties
microbidical nature
microbidical properties

我已经尝试过用单斜杠解决文本问题,但它太老套了,有没有更简单的方法?

for english in inputs:
if sum([1 for tok in english.split(' ') if '/' in tok]) == 1:
x = [1 if '/' in tok else 0 for tok in english.split(' ') ]

left = english.split(' ')[:x.index(1)]
word = english.split(' ')[x.index(1)].split('/')
right = english.split(' ')[x.index(1)+1:]

for tok in word:
print(' '.join([left + [tok] + right][0]))

如何捕获具有多个斜杠的案例?

以下是可能输入的列表:

definitive/deterministic arithmetic/calculation
random/stochastic arithmetic/calculation
both ends/edges/terminals
to draw/attract/receive attention
strict/rigorous/exact solution
both ends/edges/terminals
easy to conduct/perform/carry out
easy to conduct/perform/carry out
between/among (equals/fellows)
reference/standard/nominal value
one kind/type/variety/species
primary cause/source/origin
to be disordered/disturbed/chaotic
same category/class/rank
while keeping/preserving/maintaining/holding
driving/operating in the reverse/opposite direction
only/just that portion/much
cannot doubt/question/suspect
does not reach/attain/match
tube/pipe/duct axis
recatangular/Cartesian/orthogonal coordinates
tube/pipe/duct wall
acoustic duct/conduit/channel
site of damage/failure/fault
voice/speech wave information processing method/technique
fundamental/basic theorem/proposition
single/individual item/product
one body/unit/entity
first stage/grade/step
time/era of great leaps/strides
one form/shape/figure
reserve/spare circuit/line
basic/base/backing material
set/collection/group of tables
in the form of a thin sheet/laminate/veneer
minute/microscopic pore/gap
forming/molding and working/machining
small amount/quantity/dose
liquid crystal form/state/shape
to rub/grind/chafe the surface
the phenomenon of fracture/failure/collapse
compound/composite/combined effect
molecular form/shape/structure
…st/…nd/….rd/…th group (periodic table)
the architectural/construction world/realm
to seal/consolidate a material by firing/baking
large block/clump/clod
bruned/baked/fired brick
unbruned/unbaked/unfired brick
kiln/furnance/oven surface
stationary/stator vane/blade
moving/rotor vane/blade
industrial kiln/furnance/oven
mean/average pore size/diameter
hardened/cured/set material
kiln/oven/furnance lining
piping (layout/arrangement/system)
metallic luster/brilliance/shine
mechanical treatment/working/processing
thin-sheet/laminate/veneer manufacture
thin sheet/laminate/veneer
vehicle (cars/trucks/trains) field
sheet/panel/plate thickness
corrosion prevention/resistance/protection
wriggling/squirming/slithering motion
method for forming/molding/shaping
object to be molded/formed/shaped
pressurized molding/forming/shaping equipment
premolded/preformed object/body
to seal/consolidate a material by firing/baking
furnance/kiln/oven wall
slipping/sliding/gliding mode
bactericidal/microbidical (nature/properties)
secondary/rechargeable cell/battery
new region/domain/area

最佳答案

看来您应该使用 itertools.product() 。您可以按空格和 '/' 进行拆分,这适用于单个单词和组。例如:

from itertools import product

s = "definitive/deterministic arithmetic/calculation"
l = [g.split('/') for g in s.split(' ')]
[" ".join(words) for words in product(*l)]

结果:

['definitive arithmetic',
'definitive calculation',
'deterministic arithmetic',
'deterministic calculation']

或者:

s = "voice/speech wave information processing method/technique"
l = [g.split('/') for g in s.split(' ')]
[" ".join(words) for words in product(*l)]

结果:

['voice wave information processing method',
'voice wave information processing technique',
'speech wave information processing method',
'speech wave information processing technique']

关于python - 如何枚举单词的组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56831489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com